1、 . . . . /** * 作品:记事本 * *** * 功能:简单的文字编辑 */ import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; classNotePadextends JFrame{
2、 private JMenuBar menuBar; private JMenu fielMenu,editMenu,formMenu,aboutMenu; private JMenuItem newMenuItem,openMenuItem,saveMenuItem,exitMenuItem; private JMenuItem cutMenuItem,copyMenuItem,pasteMenuItem,foundItem,replaceItem,selectAll; private JMenuItem font,about; private JTextArea textAre
3、a; private JFrame foundFrame,replaceFrame; private JCheckBoxMenuItem wrapline; private JTextField textField1=new JTextField(15); private JTextField textField2=new JTextField(15); private utton startButton,replaceButton,reallButton; intstart=0; String value; File file=null; JFileChooser
4、 fileChooser=new JFileChooser(); booleanwrap=false; public NotePad(){ //创建文本域 textArea=new JTextArea(); add(new JScrollPane(textArea),BorderLayout.CENTER); //创建文件菜单与文件菜单项 fielMenu=new JMenu("文件"); fielMenu.setFont(new Font("微软雅黑",0,15)); newMenuItem=new JMenuItem("新建",new ImageIcon("i
5、cons\\new24.gif")); newMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); newMenuItem.addActionListener(listener); openMenuItem=new JMenuItem("打开",new ImageIcon("icons\\open24.gif")); openMenuItem.setFont(ne
6、w Font("微软雅黑",Font.BOLD,13)); openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); openMenuItem.addActionListener(listener); saveMenuItem=new JMenuItem("保存",new ImageIcon("icons\\save.gif")); saveMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); saveMenuItem
7、setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); saveMenuItem.addActionListener(listener); exitMenuItem=new JMenuItem("退出",new ImageIcon("icons\\exit24.gif")); exitMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(Ke
8、yEvent.VK_E,InputEvent.CTRL_MASK)); exitMenuItem.addActionListener(listener); //创建编辑菜单与菜单项 editMenu=new JMenu("编辑"); editMenu.setFont(new Font("微软雅黑",0,15)); cutMenuItem=new JMenuItem("剪切",new ImageIcon("icons\\cut24.gif")); cutMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); cutMenuItem.s
9、etAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); cutMenuItem.addActionListener(listener); copyMenuItem=new JMenuItem("复制",new ImageIcon("icons\\copy24.gif")); copyMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEv
10、ent.VK_C,InputEvent.CTRL_MASK)); copyMenuItem.addActionListener(listener); pasteMenuItem=new JMenuItem("粘贴",new ImageIcon("icons\\paste24.gif")); pasteMenuItem.setFont(new Font("微软雅黑",Font.BOLD,13)); pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); pas
11、teMenuItem.addActionListener(listener); foundItem=new JMenuItem("查找"); foundItem.setFont(new Font("微软雅黑",Font.BOLD,13)); foundItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK)); foundItem.addActionListener(listener); replaceItem=new JMenuItem("替换"); replaceItem
12、setFont(new Font("微软雅黑",Font.BOLD,13)); replaceItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,InputEvent.CTRL_MASK)); replaceItem.addActionListener(listener); selectAll=new JMenuItem("全选"); selectAll.setFont(new Font("微软雅黑",Font.BOLD,13)); selectAll.setAccelerator(KeyStroke.getKeyS
13、troke(KeyEvent.VK_A,InputEvent.CTRL_MASK)); selectAll.addActionListener(listener); //创建格式菜单与菜单项 formMenu=new JMenu("格式"); formMenu.setFont(new Font("微软雅黑",0,15)); wrapline=new JCheckBoxMenuItem("自动换行"); wrapline.setFont(new Font("微软雅黑",Font.BOLD,13)); wrapline.addActionListener(listener);
14、wrapline.addChangeListener(new ChangeListener() { publicvoid stateChanged(ChangeEvent e) { if(wrapline.isSelected()){ textArea.setLineWrap(true); } else textArea.setLineWrap(false); } }); font=new JMenuItem("字体"); font.setFont(new Font("微软雅黑",Font.BOLD,13)); font.addActionListe
15、ner(listener); //创建关于菜单 aboutMenu=new JMenu("关于"); aboutMenu.setFont(new Font("微软雅黑",0,15)); about=new JMenuItem("记事本……"); about.setFont(new Font("微软雅黑",Font.BOLD,13)); about.addActionListener(listener); //添加文件菜单项 fielMenu.add(newMenuItem); fielMenu.add(openMenuItem); fielMenu.add(saveMe
16、nuItem); fielMenu.addSeparator(); fielMenu.add(exitMenuItem); //添加编辑菜单项 editMenu.add(cutMenuItem); editMenu.add(copyMenuItem); editMenu.add(pasteMenuItem); editMenu.add(foundItem); editMenu.add(replaceItem); editMenu.addSeparator(); editMenu.add(selectAll); //添加格式菜单项 formMenu.add(wraplin
17、e); formMenu.add(font); //添加关于菜单项 aboutMenu.add(about); //添加菜单 menuBar=new JMenuBar(); menuBar.add(fielMenu); menuBar.add(editMenu); menuBar.add(formMenu); menuBar.add(aboutMenu); setJMenuBar(menuBar); //创建两个框架,用作查找和替换 foundFrame=new JFrame(); replaceFrame=new JFrame(); //创建两个文本框 te
18、xtField1=new JTextField(15); textField2=new JTextField(15); startButton=new utton("开始"); startButton.addActionListener(listener); replaceButton=new utton("替换为"); replaceButton.addActionListener(listener); reallButton=new utton("全部替换"); reallButton.addActionListener(listener); } //创建菜单项事
19、件监听器 ActionListener listener=new ActionListener() { publicvoid actionPerformed(ActionEvent e) { String name=e.getActionCommand(); if(e.getSource() instanceof JMenuItem){ if("新建".equals(name)){ textArea.setText(""); file=null; } if("打开".equals(name)){ if(file!=null){ fileChooser.se
20、tSelectedFile(file); } int returnVal=fileChooser.showOpenDialog(NotePad.this); if(returnVal==JFileChooser.APPROVE_OPTION){ file=fileChooser.getSelectedFile(); } try{ FileReader reader=new FileReader(file); int len=(int)file.length(); char[] array=newchar[len]; read
21、er.read(array,0,len); reader.close(); textArea.setText(new String(array)); } catch(Exception e_open){ e_open.printStackTrace(); } } } if("保存".equals(name)){ if(file!=null){ fileChooser.setSelectedFile(file); } int returnVal=fileChooser.showSaveDia
22、log(NotePad.this); if(returnVal==JFileChooser.APPROVE_OPTION){ file=fileChooser.getSelectedFile(); } try{ FileWriter writer=new FileWriter(file); writer.write(textArea.getText()); writer.close(); } catch (Exception e_save) { e_save.getStackTrace(); }
23、 } if("退出".equals(name)){ System.exit(0); } if("剪切".equals(name)){ textArea.cut(); } if("复制".equals(name)){ textArea.copy(); } if("粘贴".equals(name)){ textArea.paste(); } if("查找".equals(name)){ value=textArea.getText(); foundFrame.add(textField1,BorderLayout.C
24、ENTER); foundFrame.add(startButton,BorderLayout.SOUTH); foundFrame.setLocation(300,300); foundFrame.setTitle("查找"); foundFrame.pack(); foundFrame.setVisible(true); foundFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } if("替换".equals(name)){ value=textArea.getText(); JLab
25、el label1=new JLabel("查找容:"); JLabel label2=new JLabel("替换为:"); JPanel panel1=new JPanel(); panel1.setLayout(new GridLayout(2,2)); JPanel panel2=new JPanel(); panel2.setLayout(new GridLayout(1,3)); replaceFrame.add(panel1,BorderLayout.NORTH); replaceFrame.add(panel2,B
26、orderLayout.CENTER); panel1.add(label1); panel1.add(textField1); panel1.add(label2); panel1.add(textField2); panel2.add(startButton); panel2.add(replaceButton); panel2.add(reallButton); replaceFrame.setTitle("替换"); replaceFrame.setLocation(300,300); re
27、placeFrame.pack(); replaceFrame.setVisible(true); replaceFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } if("开始".equals(name)||"下一个".equals(name)){ String temp=textField1.getText(); int s=value.indexOf(temp,start); if(value.indexOf(temp,start)!=-1){ textArea.setSelection
28、Start(s); textArea.setSelectionEnd(s+temp.length()); textArea.setSelectedTextColor(Color.GREEN); start=s+1; startButton.setText("下一个"); }else { JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif")); foundFrame.dispose(); } } if("
29、替换为".equals(name)){ String temp=textField1.getText(); int s=value.indexOf(temp,start); if(value.indexOf(temp,start)!=-1){ textArea.setSelectionStart(s); textArea.setSelectionEnd(s+temp.length()); textArea.setSelectedTextColor(Color.GREEN); start=s+1; textArea.replaceSelection(textField
30、2.getText()); }else { JOptionPane.showMessageDialog(foundFrame, "查找完毕!", "提示", 0,new ImageIcon("icons\\search.gif")); foundFrame.dispose(); } } if("全部替换".equals(name)){ String temp=textArea.getText(); temp=temp.replaceAll(textField1.getText(),textField2.getTe
31、xt()); textArea.setText(temp); } if("全选".equals(name)){ textArea.selectAll(); } if("字体".equals(name)){ FontDialog fontDialog=new FontDialog(NotePad.this); fontDialog.setVisible(true); if(textArea.getFont()!=fontDialog.getFont()){ textArea.setFont(fontDialog.getFont());
32、 } } if("记事本……".equals(name)){ AboutDialog aboutDialog=new AboutDialog(NotePad.this); aboutDialog.setVisible(true); } } }; //创建字体设置对话面板,并添加相应事件监听器 classFontDialogextends JDialog implements ItemListener, ActionListener, WindowListener{ public JCheckBox Bold=
33、new JCheckBox("Bold",false); public JCheckBox Italic=new JCheckBox("Italic",false); public List Size,Name; publicintFontName; publicintFontStyle; publicintFontSize; public utton OK=new utton("OK"); public utton Cancel=new utton("Cancel"); public JTextArea Text=new JTextArea("字体预览文本域\n0123456
34、789\nAaBbCcXxYyZz"); public FontDialog(JFrame owner) { super(owner,"字体设置",true); GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment(); String name[]=g.getAvailableFontFamilyNames(); Name=new List(); Size=new List(); FontName=0; FontStyle=0; FontSize=8; int i=0; N
35、ame.add("Default Value");
for(i=0;i 36、alic.setFocusable(false);
Name.setFocusable(false);
Size.setFocusable(false);
Name.setBounds(10, 10, 212, 259);
this.add(Name);
Bold.setBounds(314, 10, 64, 22);
this.add(Bold);
Italic.setBounds(388, 10, 64, 22);
this.add(Italic);
Size.setBounds(232, 10, 64, 259);
this.add(Size);
Text.se 37、tBounds(306, 40, 157, 157);
this.add(Text);
OK.setBounds(306, 243, 74, 26);
this.add(OK);
Cancel.setBounds(390, 243, 74, 26);
this.add(Cancel);
Name.select(FontName);
Size.select(FontSize);
Text.setFont(getFont());
Name.addItemListener(this);
Size.addItemListener(this);
Bold.addItemLi 38、stener(this);
Italic.addItemListener(this);
OK.addActionListener(this);
Cancel.addActionListener(this);
this.addWindowListener(this);
}
publicvoid itemStateChanged(ItemEvent e) {
Text.setFont(getFont());
}
publicvoid actionPerformed(ActionEvent e) {
if(e.getSource()==OK){
FontName=Nam 39、e.getSelectedIndex();
FontStyle=getStyle();
FontSize=Size.getSelectedIndex();
this.setVisible(false);
}
else cancel();
}
publicvoid windowClosing(WindowEvent e) {
cancel();
}
public Font getFont(){
if(Name.getSelectedIndex()==0) returnnew Font("新宋体",getStyle(),Size.getSelectedIn 40、dex()+8);
elsereturnnew Font(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex()+8);
}
publicvoid cancel(){
Name.select(FontName);
Size.select(FontSize);
setStyle();
Text.setFont(getFont());
this.setVisible(false);
}
publicvoid setStyle(){
if(FontStyle==0 || FontStyle==2) Bol 41、d.setSelected(false);
elseBold.setSelected(true);
if(FontStyle==0 || FontStyle==1) Italic.setSelected(false);
elseItalic.setSelected(true);
}
publicint getStyle(){
int bold=0,italic=0;
if(Bold.isSelected()) bold=1;
if(Italic.isSelected()) italic=1;
return bold+italic*2;
}
publicvoid w 42、indowActivated(WindowEvent arg0) {}
publicvoid windowClosed(WindowEvent arg0) {}
publicvoid windowDeactivated(WindowEvent arg0) {}
publicvoid windowDeiconified(WindowEvent arg0) {}
publicvoid windowIconified(WindowEvent arg0) {}
publicvoid windowOpened(WindowEvent arg0) {}
}
//创建关于对话框
cla 43、ssAboutDialogextends JDialog implements ActionListener{
public utton OK,Icon;
public JLabel Name,Version,Author,Java;
public JPanel Panel;
AboutDialog(JFrame owner) {
super(owner,"关于",true);
OK=new utton("OK");
Icon=new utton(new ImageIcon("icons\\edit.gif"));
Name=new JLabel("Notepad");
44、
Version=new JLabel("Version 1.0");
Java=new JLabel("JRE Version 6.0");
Author=new JLabel("Copyright (c) 11-5-2012 By Jianmin Chen");
Panel=new JPanel();
Color c=new Color(0,95,191);
Name.setForeground(c);
Version.setForeground(c);
Java.setForeground(c);
Author.setForeground(c);
Panel.se 45、tBackground(Color.white);
OK.setFocusable(false);
this.setBounds(250,200,280, 180);
this.setResizable(false);
this.setLayout(null);
Panel.setLayout(null);
OK.addActionListener(this);
Icon.setFocusable(false);
Icon.setBorderPainted(false);
Author.setFont(new Font(null,Font.PLAIN,11));
Pan 46、el.add(Icon);
Panel.add(Name);
Panel.add(Version);
Panel.add(Author);
Panel.add(Java);
this.add(Panel);
this.add(OK);
Panel.setBounds(0, 0, 280, 100);
OK.setBounds(180, 114, 72, 26);
Name.setBounds(80, 10, 160, 20);
Version.setBounds(80, 27, 160, 20);
Author.setBounds(15, 70, 250, 20);
J 47、ava.setBounds(80, 44, 160, 20);
Icon.setBounds(16, 14, 48, 48);
}
publicvoid actionPerformed(ActionEvent e) {
this.setVisible(false);
}
}
}
//创建记事本
publicclass ChenJianmin {
publicstaticvoid main(String[] args){
EventQueue.invokeLater(new Runnable() {
publicvoid run() {
NotePad notePad=new NotePad();
notePad.setTitle("记事本");
notePad.setVisible(true);
notePad.setBounds(100,100,800,600);
notePad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
12 / 12






