简易记事本代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Gui extends JFrame{
JToolBar toolBar = new JToolBar();
ImageIcon iconCut = new ImageIcon("QQ截图20120520233636.jpg");
ImageIcon iconCopy = new ImageIcon("QQ截图20120520233636.jpg");
ImageIcon iconPaste = new ImageIcon("QQ截图20120520233636.jpg");
JButton buttonCut = new JButton("剪切",iconCut);
JButton buttonCopy = new JButton("复制",iconCopy);
JButton buttonPaste = new JButton("粘贴",iconPaste);
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("文件(F)");
JMenu menuEdit = new JMenu("编辑");
JMenu menuHelp = new JMenu("帮助(H)");
JMenuItem menuItemFileNew = new JMenuItem("新建");
JMenuItem menuItemFileOpen = new JMenuItem("打开");
JMenuItem menuItemFileSaveAs = new JMenuItem("另存为");
JMenuItem menuItemFileExit = new JMenuItem("退出(X)");
JCheckBoxMenuItem checkBoxMenuItemEditAutoWrap =
new JCheckBoxMenuItem("自动换行");
JMenuItem menuItemEditCut = new JMenuItem("剪切");
JMenuItem menuItemEditCopy = new JMenuItem("复制");
JMenuItem menuItemEditPaste = new JMenuItem("粘贴");
JMenuItem menuItemHelpAbout = new JMenuItem("关于(A)");
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem popupMenuItemCut = new JMenuItem("剪切");
JMenuItem popupMenuItemCopy = new JMenuItem("复制");
JMenuItem popupMenuItemPaste = new JMenuItem("粘贴");
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter fileFilTer =
new FileNameExtensionFilter("文本文件","txt");
File file;
public Gui(){
this.setTitle("简易记事本");
this.setBounds(100,200,550,440);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initialize();
this.setVisible(true);
}
private void initialize(){
toolBar.add(buttonCut);
toolBar.add(buttonCopy);
toolBar.add(buttonPaste);
this.add(toolBar,BorderLayout.NORTH);
this.add(scrollPane,BorderLayout.CENTER);
menuFile.setMnemonic(KeyEvent.VK_F);
menuHelp.setMnemonic(KeyEvent.VK_H);
menuItemFileExit.setMnemonic(KeyEvent.VK_X);
menuItemHelpAbout.setMnemonic(KeyEvent.VK_A);
menuItemFileExit.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_E,KeyEvent.CTRL_DOWN_MASK,true));
menuFile.add(menuItemFileNew);
menuFile.add(menuItemFileOpen);
menuFile.add(menuItemFileSaveAs);
menuFile.addSeparator();
menuFile.add(menuItemFileExit);
menuEdit.add(checkBoxMenuItemEditAutoWrap);
menuEdit.addSeparator();
menuEdit.add(menuItemEditCut);
menuEdit.add(menuItemEditCopy);
menuEdit.add(menuItemEditPaste);
menuHelp.add(menuItemHelpAbout);
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuHelp);
this.setJMenuBar(menuBar);
menuIt
emFileExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
popupMenu.add(popupMenuItemCut);
popupMenu.add(popupMenuItemCopy);
popupMenu.add(popupMenuItemPaste);
textArea.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e){
if(e.isPopupTrigger()){
popupMenu.show(textArea,e.getX(),e.getY());
}
}
});
buttonCut.addActionListener(new ActionHandler());
buttonCopy.addActionListener(new ActionHandler());
buttonPaste.addActionListener(new ActionHandler());
menuItemFileNew.addActionListener(new ActionHandler());
menuItemFileOpen.addActionListener(new ActionHandler());
menuItemFileSaveAs.addActionListener(new ActionHandler());
checkBoxMenuItemEditAutoWrap.addActionListener(new ActionHandler());
menuItemEditCut.addActionListener(new ActionHandler());
menuItemEditCopy.addActionListener(new ActionHandler());
menuItemEditPaste.addActionListener(new ActionHandler());
menuItemHelpAbout.addActionListener(new ActionHandler());
popupMenuItemCut.addActionListener(new ActionHandler());
popupMenuItemCopy.addActionListener(new ActionHandler());
popupMenuItemPaste.addActionListener(new ActionHandler());
fileChooser.setFileFilter(fileFilTer);
}
class ActionHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == buttonCut
||e.getSource() == menuItemEditCut
||e.getSource() == popupMenuItemCut){
textArea.cut();
}
if(e.getSource() == buttonCopy
||e.getSource() == menuItemEditCopy
||e.getSource() == popupMenuItemCopy){
textArea.copy();
}
if(e.getSource() == buttonPaste
||e.getSource() == menuItemEditPaste
||e.getSource() == popupMenuItemPaste){
textArea.paste();
}
else if(e.getSource()==menuItemFileNew){
newFile();
}
else if(e.getSource()==menuItemFileOpen){
openFile();
}
else if(e.getSource()==menuItemFileSaveAs){
saveAsFile();
}
else if(e.getSource()==checkBoxMenuItemEditAutoWrap){
if(checkBoxMenuItemEditAutoWrap.isSelected()){
textArea.setLineWrap(true);
}
else{textArea.setLineWrap(false);}
}
else if(e.getSource()==menuItemHelpAbout){
JOptionPane.showMessageDialog(null, "程序设计:王荣炜\n2012年5月");
}
}
}
void newFile(){
if(!textArea.getText().equals("")){
saveFile();
}
textArea.setText(null);
file=null;
this.setTitle("简易记事本");
}
void openFile(){
if(!textArea.getText().equals("")){
saveFile();
}
int returnVal=fileChooser.showOpenDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=fileChooser.getSelectedFile();
try{
FileReader fileReader=new FileReader(file);
textArea.read(fileReader, null);
this.setTitle(file.getName()+"—— 简易记事本");
fileReader.close();
}
catch(IOException e){
JOptionPane.showMessageDialog(
this, "异常:"+e.getMessage());
}
}
}
void saveFile(){
if(file!=null&&file.exists()){
try{
FileWriter filewriter=new FileWriter(file);
textArea.write(filewriter);
filewriter.close();
}
catch(IOException e){
JOptionPane.showMessageDialog(this, "异常:"+e.getMessage());
}
}
else{saveAsFile();}
}
void saveAsFile(){
int returnVal=fileChooser.showSaveDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=fileChooser.getSelectedFile();
try{
FileWriter fileWriter=new FileWriter(file);
textArea.write(fileWriter);
this.setTitle(file.getName()+"—— 简易记事本");
fileWriter.close();
}
catch(IOException e){
JOptionPane.showMessageDialog(this, "异常:"+e.getMessage());
}
}
}
}