在 Windows 操作系统中,记事本是一个小的应用程序,采用一个简单的文本编辑器进行文字信息的记
录和存储。请仿照 Windows 的记事本,开发一个属于自己的记事本(Notepad)功能
记事本的应该具备的功能,基本要求如下:
(1) 菜单栏中包含文件、编辑、查看和帮助菜单,具体如下图所示。
(2) 文件菜单中具有新建、打开、保存、另存为、打印和退出功能,具体如下图所示。
(3) 编辑菜单中具有的功能有撤销、重做、剪切、复制、粘贴、删除、全选以及查找和替换,具
体如下图所示。
(4) 查看菜单中具有的功能包括字体、颜色等,具体如下图所示。
(5) 帮助菜单中的“View Help…”可以查看帮助文档,“About Notepad…”可以查看记事本的当前
版本。帮助菜单中具有的功能具体如下图所示。
(6) 程序代码如下所示:
AboutDialog.java
1import java.awt.Color; 2import java.awt.Font; 3import java.awt.event.ActionEvent; 4import java.awt.event.ActionListener; 5import javax.swing.ImageIcon; 6import javax.swing.JButton; 7import javax.swing.JDialog; 8import javax.swing.JFrame; 9import javax.swing.JLabel; 10import javax.swing.JPanel; 11 12public class AboutDialog implements ActionListener { 13 public JDialog Dialog; 14 public JButton OK, Icon; 15 public JLabel Name, Version, Author, Java; 16 public JPanel Panel; 17AboutDialog(JFrame notepad, int x, int y) { 18 Dialog = new JDialog(notepad, "About Notepad...", true); 19 OK = new JButton("OK"); 20 Icon = new JButton(new ImageIcon("Notepad.jpg")); 21 Name = new JLabel("Notepad"); 22 Version = new JLabel("Version 1.0"); 23 Java = new JLabel("JRE Version 6.0"); 24 Author = new JLabel("Copyright (c) 2011 Sky. No rights reserved."); 25 Panel = new JPanel(); 26 Color c = new Color(0, 95, 191); 27 Name.setForeground(c); 28 Version.setForeground(c); 29 Java.setForeground(c); 30 Panel.setBackground(Color.WHITE); 31 OK.setFocusable(false); 32 Dialog.setSize(280, 180); 33 Dialog.setLocation(x, y); 34 Dialog.setResizable(false); 35 Dialog.setLayout(null); 36 Panel.setLayout(null); 37 OK.addActionListener(this); 38 Icon.setFocusable(false); 39 Icon.setBorderPainted(false); 40 Author.setFont(new Font(null, Font.PLAIN, 11)); 41 Panel.add(Icon); 42 Panel.add(Name); 43 Panel.add(Version); 44 Panel.add(Author); 45 Panel.add(Java); 46 Dialog.add(Panel); 47 Dialog.add(OK); 48 Panel.setBounds(0, 0, 280, 100); 49 OK.setBounds(180, 114, 72, 26); 50 Name.setBounds(80, 10, 160, 20); 51 Version.setBounds(80, 27, 160, 20); 52 Author.setBounds(15, 70, 250, 20); 53 Java.setBounds(80, 44, 160, 20); 54 Icon.setBounds(16, 14, 48, 48); 55} 56 57public void actionPerformed(ActionEvent e) { 58 Dialog.setVisible(false); 59} 60}
ColorDialog.java
1import java.awt.Color; 2import java.awt.Component; 3import java.awt.Font; 4import java.awt.GridLayout; 5import java.awt.event.ActionEvent; 6import java.awt.event.ActionListener; 7import java.awt.event.KeyEvent; 8import java.awt.event.KeyListener; 9import java.awt.event.WindowEvent; 10import java.awt.event.WindowListener; 11import javax.swing.JButton; 12import javax.swing.JDialog; 13import javax.swing.JFrame; 14import javax.swing.JLabel; 15import javax.swing.JPanel; 16import javax.swing.JTextArea; 17import javax.swing.JTextField; 18 19public class ColorDialog implements ActionListener, WindowListener{ 20 public JDialog Dialog; 21 public JLabel NFL,NBL,SFL,SBL; 22 public JTextArea Normal,Selected; 23 public JButton NFB,NBB,SFB,SBB,OK,Cancel,Reset; 24 public Color NFC,NBC,SFC,SBC; 25 public ColorChooser Chooser; 26 public ColorDialog(JFrame notepad, int x, int y){ 27 NFC=new Color(0,0,0); 28 NBC=new Color(249,249,251); 29 SFC=new Color(0,0,0); 30 SBC=new Color(191,207,223); 31 Dialog=new JDialog(notepad,"Color...",true); 32 NFL=new JLabel("Normal Foreground:"); 33 NBL=new JLabel("Normal Background:"); 34 SFL=new JLabel("Selected Foreground:"); 35 SBL=new JLabel("Selected Background:"); 36 Normal=new JTextArea("\n Normal 正常"); 37 Selected=new JTextArea("\n Selected 选中 "); 38 NFB=new JButton(""); 39 NBB=new JButton(""); 40 SFB=new JButton(""); 41 SBB=new JButton(""); 42 OK=new JButton("OK"); 43 Cancel=new JButton("Cancel"); 44 Reset=new JButton("Reset"); 45 Chooser=new ColorChooser(Dialog, x+65, y-15); 46 Normal.setEditable(false); 47 Normal.setFocusable(false); 48 Normal.setFont(new Font("新宋体", 0, 16)); 49 Normal.setForeground(NFC); 50 Normal.setBackground(NBC); 51 Selected.setEditable(false); 52 Selected.setFocusable(false); 53 Selected.setFont(Normal.getFont()); 54 Selected.setForeground(SFC); 55 Selected.setBackground(SBC); 56 NFB.setBackground(NFC); 57 NBB.setBackground(NBC); 58 SFB.setBackground(SFC); 59 SBB.setBackground(SBC); 60 Dialog.setLayout(null); 61 Dialog.setLocation(x, y); 62 Dialog.setSize(410, 220); 63 Dialog.setResizable(false); 64 Reset.setFocusable(false); 65 OK.setFocusable(false); 66 Cancel.setFocusable(false); 67 Dialog.add(Normal); 68 Dialog.add(Selected); 69 Dialog.add(NFL); 70 Dialog.add(NBL); 71 Dialog.add(SFL); 72 Dialog.add(SBL); 73 Dialog.add(SBB); 74 Dialog.add(SFB); 75 Dialog.add(NBB); 76 Dialog.add(NFB); 77 Dialog.add(OK); 78 Dialog.add(Cancel); 79 Dialog.add(Reset); 80 SBB.setBounds(144, 100, 60, 22); 81 SFB.setBounds(144, 70, 60, 22); 82 NBB.setBounds(144, 40, 60, 22); 83 NFB.setBounds(144, 10, 60, 22); 84 NFL.setBounds(10, 10, 130, 22); 85 NBL.setBounds(10, 40, 130, 22); 86 SFL.setBounds(10, 70, 130, 22); 87 SBL.setBounds(10, 100, 130, 22); 88 Normal.setBounds(220, 10, 174, 56); 89 Selected.setBounds(220, 66, 174, 56); 90 Reset.setBounds(10, 160, 74, 24); 91 OK.setBounds(236, 160, 74, 24); 92 Cancel.setBounds(320, 160, 74, 24); 93 Dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 94 Dialog.addWindowListener(this); 95 NFB.addActionListener(this); 96 NBB.addActionListener(this); 97 SFB.addActionListener(this); 98 SBB.addActionListener(this); 99 Reset.addActionListener(this); 100 OK.addActionListener(this); 101 Cancel.addActionListener(this); 102 } 103 public void setTextAreaColor(){ 104 Normal.setForeground(NFB.getBackground()); 105 Normal.setBackground(NBB.getBackground()); 106 Selected.setForeground(SFB.getBackground()); 107 Selected.setBackground(SBB.getBackground()); 108 } 109 public void cancel(){ 110 Normal.setForeground(NFC); 111 Normal.setBackground(NBC); 112 Selected.setForeground(SFC); 113 Selected.setBackground(SBC); 114 NFB.setBackground(NFC); 115 NBB.setBackground(NBC); 116 SFB.setBackground(SFC); 117 SBB.setBackground(SBC); 118 Dialog.setVisible(false); 119 } 120 public void actionPerformed(ActionEvent e) { 121 Object obj=e.getSource(); 122 if(obj==Reset){ 123 NFB.setBackground(new Color(0,0,0)); 124 NBB.setBackground(new Color(249,249,251)); 125 SFB.setBackground(new Color(0,0,0)); 126 SBB.setBackground(new Color(191,207,223)); 127 setTextAreaColor(); 128 } 129 else if(obj==OK){ 130 NFC=NFB.getBackground(); 131 NBC=NBB.getBackground(); 132 SFC=SFB.getBackground(); 133 SBC=SBB.getBackground(); 134 Dialog.setVisible(false); 135 } 136 else if(obj==Cancel) 137 cancel(); 138 else{ 139 Chooser.init(((Component) obj).getBackground()); 140 Chooser.Dialog.setVisible(true); 141 ((Component) obj).setBackground(Chooser.New.getBackground()); 142 setTextAreaColor(); 143 } 144 } 145 public void windowClosing(WindowEvent e) { 146 cancel(); 147 } 148 public void windowActivated(WindowEvent arg0) {} 149 public void windowClosed(WindowEvent arg0) {} 150 public void windowDeactivated(WindowEvent arg0) {} 151 public void windowDeiconified(WindowEvent arg0) {} 152 public void windowIconified(WindowEvent arg0) {} 153 public void windowOpened(WindowEvent arg0) {} 154} 155 156class ColorChooser implements ActionListener,WindowListener,KeyListener{ 157 JDialog Dialog; 158 JButton Choice[][],Old,New,OK,Cancel; 159 JPanel Panel; 160 JTextField R,G,B; 161 JLabel OldLabel,NewLabel,RL,GL,BL; 162 ColorChooser(JDialog color,int x, int y){ 163 Dialog=new JDialog(color,true); 164 Choice=new JButton[8][8]; 165 Panel=new JPanel(); 166 Old=new JButton(""); 167 New=new JButton(""); 168 OldLabel=new JLabel("Old:"); 169 NewLabel=new JLabel("New:"); 170 RL=new JLabel("R:"); 171 GL=new JLabel("G:"); 172 BL=new JLabel("B:"); 173 R=new JTextField(""); 174 G=new JTextField(""); 175 B=new JTextField(""); 176 OK=new JButton("OK"); 177 Cancel=new JButton("Cancel"); 178 Panel.setLayout(new GridLayout(8,8,0,0)); 179 int i=0,j=0; 180 Color c; 181 Choice[0][7]=new JButton("");Choice[0][7].setBackground(new Color(255,255,255)); 182 Choice[1][7]=new JButton("");Choice[1][7].setBackground(new Color(255,223,191)); 183 Choice[2][7]=new JButton("");Choice[2][7].setBackground(new Color(255,207,207)); 184 Choice[3][7]=new JButton("");Choice[3][7].setBackground(new Color(223,191,255)); 185 Choice[4][7]=new JButton("");Choice[4][7].setBackground(new Color(207,207,255)); 186 Choice[5][7]=new JButton("");Choice[5][7].setBackground(new Color(191,223,255)); 187 Choice[6][7]=new JButton("");Choice[6][7].setBackground(new Color(207,255,207)); 188 Choice[7][7]=new JButton("");Choice[7][7].setBackground(new Color(223,255,191)); 189 for(i=0;i<8;i++){ 190 c=Choice[i][7].getBackground(); 191 for(j=0;j<8;j++){ 192 if(j!=7){ 193 Choice[i][j]=new JButton(""); 194 Choice[i][j].setBackground(new Color(c.getRed()*(j+1)/8,c.getGreen()*(j+1)/8,c.getBlue()*(j+1)/8)); 195 } 196 Choice[i][j].setFocusable(false); 197 Choice[i][j].addActionListener(this); 198 Panel.add(Choice[i][j]); 199 } 200 } 201 Dialog.setSize(280,250); 202 Dialog.setLayout(null); 203 Dialog.setLocation(x, y); 204 Dialog.setResizable(false); 205 Dialog.add(Panel); 206 Panel.setBounds(10, 10, 160, 160); 207 Dialog.add(Old); 208 Dialog.add(OldLabel); 209 Old.setEnabled(false); 210 Old.setBorderPainted(false); 211 Old.setBounds(214, 10, 44, 22); 212 OldLabel.setBounds(180, 10, 44, 22); 213 Dialog.add(New); 214 Dialog.add(NewLabel); 215 New.setEnabled(false); 216 New.setBorderPainted(false); 217 New.setBounds(214, 32, 44, 22); 218 NewLabel.setBounds(180, 32, 44, 22); 219 Dialog.add(R); 220 Dialog.add(G); 221 Dialog.add(B); 222 R.setBounds(214, 97, 44, 22); 223 G.setBounds(214, 123, 44, 22); 224 B.setBounds(214, 149, 44, 22); 225 Dialog.add(RL); 226 Dialog.add(GL); 227 Dialog.add(BL); 228 RL.setBounds(196, 97, 16, 22); 229 GL.setBounds(196, 123, 16, 22); 230 BL.setBounds(196, 149, 16, 22); 231 Dialog.add(OK); 232 Dialog.add(Cancel); 233 OK.setFocusable(false); 234 Cancel.setFocusable(false); 235 OK.setBounds(106, 190, 74, 24); 236 Cancel.setBounds(190, 190, 74, 24); 237 OK.addActionListener(this); 238 Cancel.addActionListener(this); 239 G.addKeyListener(this); 240 R.addKeyListener(this); 241 B.addKeyListener(this); 242 } 243 public void setText(Color c){ 244 R.setText(String.valueOf(c.getRed())); 245 G.setText(String.valueOf(c.getGreen())); 246 B.setText(String.valueOf(c.getBlue())); 247 } 248 public void init(Color c){ 249 New.setBackground(c); 250 Old.setBackground(c); 251 setText(c); 252 } 253 public void actionPerformed(ActionEvent e) { 254 Object obj=e.getSource(); 255 if(obj==OK) Dialog.setVisible(false); 256 else if(obj==Cancel){ 257 New.setBackground(Old.getBackground()); 258 Dialog.setVisible(false); 259 } 260 else{ 261 New.setBackground(((Component) obj).getBackground()); 262 setText(New.getBackground()); 263 } 264 } 265 public void windowClosing(WindowEvent e) { 266 New.setBackground(Old.getBackground()); 267 Dialog.setVisible(false); 268 } 269 public void keyReleased(KeyEvent e) { 270 try{ 271 int r,g,b; 272 if(R.getText().length()==0) r=0; 273 else r=Integer.valueOf(R.getText()); 274 if(G.getText().length()==0) g=0; 275 else g=Integer.valueOf(G.getText()); 276 if(B.getText().length()==0) b=0; 277 else b=Integer.valueOf(B.getText()); 278 New.setBackground(new Color(r,g,b)); 279 } 280 catch(NumberFormatException nfe){setText(New.getBackground());} 281 catch(IllegalArgumentException iae){setText(New.getBackground());} 282 } 283 public void keyPressed(KeyEvent e) {} 284 public void keyTyped(KeyEvent e) {} 285 public void windowActivated(WindowEvent arg0) {} 286 public void windowClosed(WindowEvent arg0) {} 287 public void windowDeactivated(WindowEvent arg0) {} 288 public void windowDeiconified(WindowEvent arg0) {} 289 public void windowIconified(WindowEvent arg0) {} 290 public void windowOpened(WindowEvent arg0) {} 291}
EnsureDialog.java
1import java.awt.BorderLayout; 2import java.awt.Color; 3import java.awt.FlowLayout; 4import java.awt.Font; 5import java.awt.event.ActionEvent; 6import java.awt.event.ActionListener; 7import java.awt.event.WindowEvent; 8import java.awt.event.WindowListener; 9import javax.swing.JButton; 10import javax.swing.JDialog; 11import javax.swing.JFrame; 12import javax.swing.JLabel; 13import javax.swing.JPanel; 14 15public class EnsureDialog implements WindowListener, ActionListener { 16 public int YES, NO, CANCEL, Status; 17 public JDialog Ensure; 18 public JButton Yes, No, Cancel; 19 public JLabel Question; 20 public JPanel ButtonPanel, TextPanel; 21 22 EnsureDialog(JFrame notepad, int x, int y) { 23 YES = 0; 24 NO = 1; 25 CANCEL = 2; 26 Status = CANCEL; 27 Ensure = new JDialog(notepad, true); 28 /* 29 * 这里的模式标志true的作用是使对话框处于notepad的上端, 并且当对话框显示时进程处于停滞状态, 直到对话框不再显示为止。在本程序中 30 * ,由于对对话框进行了事件监听处理,当对话框消失时状态标 志Status同时发生了变化 ,这样就可以在进程继续时获得新的Status值 31 */ 32 Yes = new JButton("Yes"); 33 No = new JButton("No"); 34 Cancel = new JButton("Cancel"); 35 Question = new JLabel(" Do you want to save changes to the file?"); 36 ButtonPanel = new JPanel(); 37 TextPanel = new JPanel(); 38 ButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 16, 10)); 39 TextPanel.setLayout(new BorderLayout()); 40 Ensure.setLayout(new BorderLayout()); 41 ButtonPanel.add(Yes); 42 ButtonPanel.add(No); 43 ButtonPanel.add(Cancel); 44 TextPanel.add(Question); 45 Ensure.add(TextPanel, BorderLayout.CENTER); 46 Ensure.add(ButtonPanel, BorderLayout.SOUTH); 47 Ensure.setLocation(x, y); 48 Ensure.setSize(360, 140); 49 Ensure.setResizable(false); 50 TextPanel.setBackground(Color.WHITE); 51 Question.setFont(new Font(null, Font.PLAIN, 16)); 52 Question.setForeground(new Color(0, 95, 191)); 53 Yes.setFocusable(false); 54 No.setFocusable(false); 55 Cancel.setFocusable(false); 56 Ensure.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 57 Ensure.addWindowListener(this); 58 Yes.addActionListener(this); 59 No.addActionListener(this); 60 Cancel.addActionListener(this); 61 } 62 63 public void actionPerformed(ActionEvent e) { 64 if (e.getSource() == Yes) 65 Status = YES; 66 else if (e.getSource() == No) 67 Status = NO; 68 else if (e.getSource() == Cancel) 69 Status = CANCEL; 70 Ensure.setVisible(false); 71 } 72 73 public void windowClosing(WindowEvent e) { 74 Status = CANCEL; 75 Ensure.setVisible(false); 76 } 77 78 public void windowActivated(WindowEvent e) { 79 } 80 81 public void windowClosed(WindowEvent e) { 82 } 83 84 public void windowDeactivated(WindowEvent e) { 85 } 86 87 public void windowDeiconified(WindowEvent e) { 88 } 89 90 public void windowIconified(WindowEvent e) { 91 } 92 93 public void windowOpened(WindowEvent e) { 94 } 95}
FindAndReplace.java
1import java.awt.TextField; 2import javax.swing.ButtonGroup; 3import javax.swing.JButton; 4import javax.swing.JCheckBox; 5import javax.swing.JDialog; 6import javax.swing.JFrame; 7import javax.swing.JLabel; 8import javax.swing.JRadioButton; 9 10public class FindAndReplace { 11 public JDialog Dialog; 12 public JButton FindNext, Replace, ReplaceAll, Finish; 13 public JCheckBox MatchCase; 14 public JRadioButton Up, Down; 15 public ButtonGroup DirectionGroup; 16 public JLabel FindWhat, ReplaceWith, Direction; 17 public TextField FindText, ReplaceText; 18 19 FindAndReplace(JFrame notepad) { 20 Dialog = new JDialog(notepad, "Find And Replace...", false); 21 /* 22 * 与EnsureDialog不同的是 , 这里的模式标志false使对话框始终处于notepad的上端 , 但点击notepad 23 * 时notepad会继续处于活动状态 , 对话框则变成不活动状态 24 */ 25 FindNext = new JButton("Find Next"); 26 Replace = new JButton("Replace"); 27 ReplaceAll = new JButton("Replace All"); 28 Finish = new JButton("Finish"); 29 MatchCase = new JCheckBox("Match Case", false); 30 Down = new JRadioButton("Down", true); 31 Up = new JRadioButton("Up", false); 32 FindWhat = new JLabel("Find What:"); 33 ReplaceWith = new JLabel("Replace With:"); 34 Direction = new JLabel("Direction:"); 35 FindText = new TextField(""); 36 ReplaceText = new TextField(""); 37 Dialog.setSize(380, 160); 38 Dialog.setResizable(false); 39 FindNext.setFocusable(false); 40 Replace.setFocusable(false); 41 ReplaceAll.setFocusable(false); 42 MatchCase.setFocusable(false); 43 Finish.setFocusable(false); 44 Up.setFocusable(false); 45 Down.setFocusable(false); 46 DirectionGroup = new ButtonGroup(); 47 Dialog.setLayout(null); 48 FindWhat.setBounds(10, 12, 80, 22); 49 ReplaceWith.setBounds(10, 42, 80, 22); 50 FindText.setBounds(95, 12, 160, 22); 51 ReplaceText.setBounds(95, 42, 160, 22); 52 FindNext.setBounds(265, 12, 98, 22); 53 Replace.setBounds(265, 42, 98, 22); 54 ReplaceAll.setBounds(265, 72, 98, 22); 55 Direction.setBounds(10, 72, 80, 22); 56 MatchCase.setBounds(6, 102, 98, 22); 57 Down.setBounds(95, 72, 80, 22); 58 Up.setBounds(175, 72, 80, 22); 59 Finish.setBounds(265, 102, 98, 22); 60 DirectionGroup.add(Up); 61 DirectionGroup.add(Down); 62 Dialog.add(FindWhat); 63 Dialog.add(MatchCase); 64 Dialog.add(FindText); 65 Dialog.add(FindNext); 66 Dialog.add(Direction); 67 Dialog.add(ReplaceWith); 68 Dialog.add(ReplaceText); 69 Dialog.add(Replace); 70 Dialog.add(ReplaceAll); 71 Dialog.add(Finish); 72 Dialog.add(Down); 73 Dialog.add(Up); 74 } 75}
FontDialog.java
1import java.awt.Font; 2import java.awt.GraphicsEnvironment; 3import java.awt.List; 4import java.awt.event.ActionEvent; 5import java.awt.event.ActionListener; 6import java.awt.event.ItemEvent; 7import java.awt.event.ItemListener; 8import java.awt.event.WindowEvent; 9import java.awt.event.WindowListener; 10import javax.swing.JButton; 11import javax.swing.JCheckBox; 12import javax.swing.JDialog; 13import javax.swing.JFrame; 14import javax.swing.JTextArea; 15 16public class FontDialog implements ItemListener, ActionListener, WindowListener{ 17 public JDialog Dialog; 18 public JCheckBox Bold,Italic; 19 public List Size,Name; 20 public int FontName; 21 public int FontStyle; 22 public int FontSize; 23 public JButton OK,Cancel; 24 public JTextArea Text; 25 FontDialog(JFrame notepad, int x, int y) { 26 GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment(); 27 String name[]=g.getAvailableFontFamilyNames(); 28 Bold=new JCheckBox("Bold",false); 29 Italic=new JCheckBox("Italic",false); 30 Dialog=new JDialog(notepad,"Font...",true); 31 Text=new JTextArea("字体预览用例\n9876543210\nAaBbCcXxYyZz"); 32 OK=new JButton("OK"); 33 Cancel=new JButton("Cancel"); 34 Size=new List(); 35 Name=new List(); 36 int i=0; 37 Name.add("Default Value"); 38 for(i=0;i<name.length;i++) Name.add(name[i]); 39 for(i=8;i<257;i++) Size.add(String.valueOf(i)); 40 FontName=0; 41 FontStyle=0; 42 FontSize=8; 43 Dialog.setLayout(null); 44 Dialog.setLocation(x, y); 45 Dialog.setSize(480, 306); 46 Dialog.setResizable(false); 47 OK.setFocusable(false); 48 Cancel.setFocusable(false); 49 Bold.setFocusable(false); 50 Italic.setFocusable(false); 51 Name.setFocusable(false); 52 Size.setFocusable(false); 53 Name.setBounds(10, 10, 212, 259); 54 Dialog.add(Name); 55 Bold.setBounds(314, 10, 64, 22); 56 Dialog.add(Bold); 57 Italic.setBounds(388, 10, 64, 22); 58 Dialog.add(Italic); 59 Size.setBounds(232, 10, 64, 259); 60 Dialog.add(Size); 61 Text.setBounds(306, 40, 157, 157); 62 Dialog.add(Text); 63 OK.setBounds(306, 243, 74, 26); 64 Dialog.add(OK); 65 Cancel.setBounds(390, 243, 74, 26); 66 Dialog.add(Cancel); 67 Name.select(FontName); 68 Size.select(FontSize); 69 Text.setFont(getFont()); 70 Dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 71 Name.addItemListener(this); 72 Size.addItemListener(this); 73 Bold.addItemListener(this); 74 Italic.addItemListener(this); 75 OK.addActionListener(this); 76 Cancel.addActionListener(this); 77 Dialog.addWindowListener(this); 78 } 79 public void itemStateChanged(ItemEvent e) { 80 Text.setFont(getFont()); 81 } 82 public void actionPerformed(ActionEvent e) { 83 if(e.getSource()==OK){ 84 FontName=Name.getSelectedIndex(); 85 FontStyle=getStyle(); 86 FontSize=Size.getSelectedIndex(); 87 Dialog.setVisible(false); 88 } 89 else cancel(); 90 } 91 public void windowClosing(WindowEvent e) { 92 cancel(); 93 } 94 public Font getFont(){ 95 if(Name.getSelectedIndex()==0) return new Font("新宋体",getStyle(),Size.getSelectedIndex()+8); 96 else return new Font(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex()+8); 97 } 98 public void cancel(){ 99 Name.select(FontName); 100 Size.select(FontSize); 101 setStyle(); 102 Text.setFont(getFont()); 103 Dialog.setVisible(false); 104 } 105 public void setStyle(){ 106 if(FontStyle==0 || FontStyle==2) Bold.setSelected(false); 107 else Bold.setSelected(true); 108 if(FontStyle==0 || FontStyle==1) Italic.setSelected(false); 109 else Italic.setSelected(true); 110 } 111 public int getStyle(){ 112 int bold=0,italic=0; 113 if(Bold.isSelected()) bold=1; 114 if(Italic.isSelected()) italic=1; 115 return bold+italic*2; 116 } 117 public void windowActivated(WindowEvent arg0) {} 118 public void windowClosed(WindowEvent arg0) {} 119 public void windowDeactivated(WindowEvent arg0) {} 120 public void windowDeiconified(WindowEvent arg0) {} 121 public void windowIconified(WindowEvent arg0) {} 122 public void windowOpened(WindowEvent arg0) {} 123}
MenuList.java
1import javax.swing.JCheckBoxMenuItem; 2import javax.swing.JMenu; 3import javax.swing.JMenuBar; 4import javax.swing.JMenuItem; 5import javax.swing.KeyStroke; 6 7public class MenuList{ 8 public JMenuBar Menu; 9 public JMenu File, Edit, View, Help; 10 public JMenuItem 11 New,Open,Save,SaveAs,Print,Exit, 12 ViewHelp,AboutNotepad, 13 Font,Color, 14 Undo,Redo,Cut,Copy,Paste,Delete,SelectAll,FindAndReplace; 15 public JCheckBoxMenuItem WordWrap,Truncation; 16 MenuList(){ 17 Menu=new JMenuBar(); 18 File=new JMenu(" File "); 19 New=new JMenuItem("New"); 20 Open=new JMenuItem("Open..."); 21 Save=new JMenuItem("Save"); 22 SaveAs=new JMenuItem("Save As..."); 23 Print=new JMenuItem("Print..."); 24 Exit=new JMenuItem("Exit"); 25 Help=new JMenu(" Help "); 26 ViewHelp=new JMenuItem("View Help..."); 27 AboutNotepad=new JMenuItem("About Notepad..."); 28 View=new JMenu(" View "); 29 WordWrap=new JCheckBoxMenuItem("Word Wrap",true); 30 Truncation=new JCheckBoxMenuItem("Truncation",false); 31 Font=new JMenuItem("Font..."); 32 Color=new JMenuItem("Color..."); 33 Edit=new JMenu(" Edit "); 34 Undo=new JMenuItem("Undo"); 35 Redo=new JMenuItem("Redo"); 36 Cut=new JMenuItem("Cut"); 37 Copy=new JMenuItem("Copy"); 38 Paste=new JMenuItem("Paste"); 39 Delete=new JMenuItem("Delete"); 40 SelectAll=new JMenuItem("Select All"); 41 FindAndReplace=new JMenuItem("Find And Replace..."); 42 Undo.setEnabled(false); 43 Redo.setEnabled(false); 44 Edit.add(Undo); 45 Edit.add(Redo); 46 Edit.addSeparator(); 47 Edit.add(Cut); 48 Edit.add(Copy); 49 Edit.add(Paste); 50 Edit.add(Delete); 51 Edit.addSeparator(); 52 Edit.add(SelectAll); 53 Edit.add(FindAndReplace); 54 View.add(WordWrap); 55 View.add(Truncation); 56 View.addSeparator(); 57 View.add(Font); 58 View.add(Color); 59 Help.add(ViewHelp); 60 Help.add(AboutNotepad); 61 File.add(New); 62 File.add(Open); 63 File.addSeparator(); 64 File.add(Save); 65 File.add(SaveAs); 66 File.addSeparator(); 67 File.add(Print); 68 File.add(Exit); 69 Menu.add(File); 70 Menu.add(Edit); 71 Menu.add(View); 72 Menu.add(Help); 73 New.setAccelerator(KeyStroke.getKeyStroke('N',128)); 74 Open.setAccelerator(KeyStroke.getKeyStroke('O',128)); 75 Save.setAccelerator(KeyStroke.getKeyStroke('S',128)); 76 Print.setAccelerator(KeyStroke.getKeyStroke('P',128)); 77 Undo.setAccelerator(KeyStroke.getKeyStroke('Z',128)); 78 Redo.setAccelerator(KeyStroke.getKeyStroke('Y',128)); 79 Cut.setAccelerator(KeyStroke.getKeyStroke('X',128)); 80 Copy.setAccelerator(KeyStroke.getKeyStroke('C',128)); 81 Paste.setAccelerator(KeyStroke.getKeyStroke('V',128)); 82 Delete.setAccelerator(KeyStroke.getKeyStroke(127,0)); 83 SelectAll.setAccelerator(KeyStroke.getKeyStroke('A',128)); 84 } 85}
TextArea.java
1import java.awt.Toolkit; 2import java.awt.datatransfer.Clipboard; 3import java.awt.datatransfer.DataFlavor; 4import java.awt.datatransfer.Transferable; 5import java.awt.event.ActionEvent; 6import java.awt.event.ActionListener; 7import java.awt.event.ItemEvent; 8import java.awt.event.ItemListener; 9import java.awt.event.MouseEvent; 10import java.awt.event.MouseListener; 11import java.io.BufferedWriter; 12import java.io.FileReader; 13import java.io.FileWriter; 14import java.io.IOException; 15import javax.swing.JFrame; 16import javax.swing.JMenuItem; 17import javax.swing.JPopupMenu; 18import javax.swing.JScrollPane; 19import javax.swing.JTextArea; 20import javax.swing.event.MenuEvent; 21import javax.swing.event.MenuListener; 22import javax.swing.event.UndoableEditEvent; 23import javax.swing.event.UndoableEditListener; 24import javax.swing.undo.UndoManager; 25 26public class TextArea extends JTextArea implements MouseListener,UndoableEditListener, 27MenuListener,ActionListener,ItemListener{ 28 private static final long serialVersionUID = 1L; 29 public boolean Saved; 30 public String Name,Path; 31 public JScrollPane Pane; 32 public JPopupMenu Popup; 33 public JMenuItem Redo,Undo,Cut,Copy,Paste,Delete,SelectAll,FindAndReplace; 34 public UndoManager Manager; 35 public MenuList menu; 36 public FindAndReplace find; 37 TextArea(JFrame notepad,int x,int y){ 38 super(); 39 Saved=true; 40 Name=null; 41 Path=null; 42 Popup=new JPopupMenu(); 43 Undo=new JMenuItem(" Undo"); 44 Redo=new JMenuItem(" Redo"); 45 Cut=new JMenuItem(" Cut"); 46 Copy=new JMenuItem(" Copy"); 47 Paste=new JMenuItem(" Paste"); 48 Delete=new JMenuItem(" Delete"); 49 SelectAll=new JMenuItem(" Select All"); 50 FindAndReplace=new JMenuItem(" Find And Replace..."); 51 Pane=new JScrollPane(this); 52 Manager=new UndoManager(); 53 menu=new MenuList(); 54 find=new FindAndReplace(notepad); 55 find.Dialog.setLocation(x,y); 56 Undo.setEnabled(false); 57 Redo.setEnabled(false); 58 setLineWrap(true); 59 setWrapStyleWord(true); 60 Manager.setLimit(-1); 61 Popup.add(Undo); 62 Popup.add(Redo); 63 Popup.addSeparator(); 64 Popup.add(Cut); 65 Popup.add(Copy); 66 Popup.add(Paste); 67 Popup.add(Delete); 68 Popup.addSeparator(); 69 Popup.add(SelectAll); 70 Popup.add(FindAndReplace); 71 add(Popup); 72 menu.Edit.addMenuListener(this); 73 menu.WordWrap.addItemListener(this); 74 menu.Truncation.addItemListener(this); 75 getDocument().addUndoableEditListener(this); 76 addMouseListener(this); 77 find.FindNext.addActionListener(this); 78 find.Replace.addActionListener(this); 79 find.ReplaceAll.addActionListener(this); 80 find.Finish.addActionListener(this); 81 } 82 public void saveFile(){ 83 try { 84 FileWriter fw = new FileWriter(Path+Name,false); 85 BufferedWriter bw=new BufferedWriter(fw); 86 bw.write(getText()); 87 bw.close(); 88 fw.close(); 89 Saved=true; 90 } catch (IOException e){} 91 } 92 public void openFile(){ 93 try { 94 int c; 95 StringBuffer sb=new StringBuffer(); 96 FileReader fr=new FileReader(Path+Name); 97 setText(null); 98 while((c=fr.read())!=-1) 99 sb.append((char)c); 100 setText(sb.toString()); 101 Saved=true; 102 fr.close(); 103 Undo.setEnabled(false); 104 Redo.setEnabled(false); 105 menu.Undo.setEnabled(false); 106 menu.Redo.setEnabled(false); 107 } 108 catch (IOException e){} 109 } 110 public void delete(){ 111 int start=getSelectionStart(); 112 int end=getSelectionEnd(); 113 replaceRange("",start,end); 114 } 115 public int lastOf(String s1,int i){ 116 String s=getText(); 117 if(find.MatchCase.isSelected()) return s.lastIndexOf(s1,i); 118 else{ 119 s=s.toLowerCase(); 120 return s.lastIndexOf(s1.toLowerCase(),i); 121 } 122 } 123 public int nextOf(String s1,int i){ 124 String s=getText(); 125 if(find.MatchCase.isSelected()) return s.indexOf(s1,i); 126 else{ 127 s=s.toLowerCase(); 128 return s.indexOf(s1.toLowerCase(),i); 129 } 130 } 131 public void actionPerformed(ActionEvent e){ 132 Object obj=e.getSource(); 133 if(obj==find.Finish) find.Dialog.setVisible(false); 134 String s1=find.FindText.getText(); 135 String s2=find.ReplaceText.getText(); 136 int len1=s1.length(),len2=s2.length(); 137 if(len1<1) return; 138 int head=getSelectionStart(),rear=getSelectionEnd(); 139 if(obj==find.Replace){ 140 if(head<rear) replaceRange(s2,head,rear); 141 else obj=find.FindNext; 142 } 143 if(obj==find.FindNext){ 144 if(find.Up.isSelected()){ 145 head=lastOf(s1,head-len1); 146 if(head<0) return; 147 select(head, head+len1); 148 } 149 else{ 150 rear=nextOf(s1, rear); 151 if(rear<0) return; 152 select(rear,rear+len1); 153 } 154 } 155 else if(obj==find.ReplaceAll){ 156 rear=0; 157 while(true){ 158 rear=nextOf(s1,rear); 159 if(rear<0) return; 160 replaceRange(s2,rear,rear+len1); 161 rear=rear+len2; 162 setCaretPosition(rear); 163 } 164 } 165 } 166 public void menuSelected(MenuEvent e){ 167 Clipboard Board=Toolkit.getDefaultToolkit().getSystemClipboard(); 168 Transferable contents = Board.getContents(Board); 169 DataFlavor flavor = DataFlavor.stringFlavor; 170 if(contents.isDataFlavorSupported(flavor)) 171 menu.Paste.setEnabled(true); 172 else 173 menu.Paste.setEnabled(false); 174 if(getSelectedText()!=null){ 175 menu.Cut.setEnabled(true); 176 menu.Copy.setEnabled(true); 177 menu.Delete.setEnabled(true); 178 } 179 else{ 180 menu.Cut.setEnabled(false); 181 menu.Copy.setEnabled(false); 182 menu.Delete.setEnabled(false); 183 } 184 if(getText().isEmpty()){ 185 menu.SelectAll.setEnabled(false); 186 menu.FindAndReplace.setEnabled(false); 187 } 188 else{ 189 menu.SelectAll.setEnabled(true); 190 menu.FindAndReplace.setEnabled(true); 191 } 192 } 193 public void undoableEditHappened(UndoableEditEvent e){ 194 Manager.addEdit(e.getEdit()); 195 Saved=false; 196 menu.Undo.setEnabled(true); 197 Undo.setEnabled(true); 198 menu.Redo.setEnabled(false); 199 Redo.setEnabled(false); 200 } 201 public void mouseReleased(MouseEvent e) { 202 if(e.isPopupTrigger()) 203 { 204 Clipboard Board=Toolkit.getDefaultToolkit().getSystemClipboard(); 205 Transferable contents = Board.getContents(Board); 206 DataFlavor flavor = DataFlavor.stringFlavor; 207 if(contents.isDataFlavorSupported(flavor)) 208 Paste.setEnabled(true); 209 else 210 Paste.setEnabled(false); 211 if(getSelectedText()!=null){ 212 Cut.setEnabled(true); 213 Copy.setEnabled(true); 214 Delete.setEnabled(true); 215 } 216 else{ 217 Cut.setEnabled(false); 218 Copy.setEnabled(false); 219 Delete.setEnabled(false); 220 } 221 if(getText().isEmpty()){ 222 SelectAll.setEnabled(false); 223 FindAndReplace.setEnabled(false); 224 } 225 else{ 226 SelectAll.setEnabled(true); 227 FindAndReplace.setEnabled(true); 228 } 229 Popup.show(this,e.getX(),e.getY()); 230 } 231 } 232 public void itemStateChanged(ItemEvent e) { 233 if(e.getSource()==menu.WordWrap){ 234 setLineWrap(menu.WordWrap.isSelected()); 235 menu.Truncation.setEnabled(menu.WordWrap.isSelected()); 236 } 237 else 238 setWrapStyleWord(!menu.Truncation.isSelected()); 239 } 240 public void mousePressed(MouseEvent e) {} 241 public void mouseClicked(MouseEvent e) {} 242 public void mouseEntered(MouseEvent e) {} 243 public void mouseExited(MouseEvent e) {} 244 public void menuCanceled(MenuEvent e) {} 245 public void menuDeselected(MenuEvent e) {} 246}
Notepad.java
1import java.awt.Dimension; 2import java.awt.FileDialog; 3import java.awt.Image; 4import java.awt.Toolkit; 5import java.awt.event.ActionEvent; 6import java.awt.event.ActionListener; 7import java.awt.event.WindowEvent; 8import java.awt.event.WindowListener; 9import java.awt.print.PrinterException; 10import javax.swing.JFrame; 11public class Notepad implements ActionListener,WindowListener{ 12 static Dimension screen=Toolkit.getDefaultToolkit().getScreenSize(); 13 static Image icon=Toolkit.getDefaultToolkit().getImage("Notepad.png"); 14 JFrame notepad; 15 EnsureDialog ensure; 16 TextArea text; 17 FileDialog dialog; 18 FontDialog font; 19 ColorDialog color; 20 AboutDialog about; 21 Notepad(){ 22 notepad=new JFrame("Notepad"); 23 dialog=new FileDialog(notepad); 24 text=new TextArea(notepad,screen.width/2-190,screen.height/2-90); 25 ensure=new EnsureDialog(notepad,screen.width/2-180,screen.height/2-80); 26 font=new FontDialog(notepad,screen.width/2-240,screen.height/2-150); 27 color=new ColorDialog(notepad,screen.width/2-205,screen.height/2-110); 28 about=new AboutDialog(notepad,screen.width/2-140,screen.height/2-100); 29 notepad.setJMenuBar(text.menu.Menu); 30 notepad.add(text.Pane); 31 notepad.setSize(640,420); 32 notepad.setLocation(screen.width/2-320,screen.height/2-220); 33 notepad.setMinimumSize(new Dimension(185,185)); 34 notepad.setIconImage(icon); 35 notepad.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 36 notepad.addWindowListener(this); 37 text.setFont(font.getFont()); 38 text.setForeground(color.NFC); 39 text.setBackground(color.NBC); 40 text.setSelectedTextColor(color.SFC); 41 text.setSelectionColor(color.SBC); 42 text.menu.Save.addActionListener(this); 43 text.menu.SaveAs.addActionListener(this); 44 text.menu.Open.addActionListener(this); 45 text.menu.New.addActionListener(this); 46 text.menu.Exit.addActionListener(this); 47 text.menu.Undo.addActionListener(this); 48 text.menu.Redo.addActionListener(this); 49 text.menu.Cut.addActionListener(this); 50 text.menu.Copy.addActionListener(this); 51 text.menu.Paste.addActionListener(this); 52 text.menu.Delete.addActionListener(this); 53 text.menu.SelectAll.addActionListener(this); 54 text.menu.FindAndReplace.addActionListener(this); 55 text.menu.WordWrap.addActionListener(this); 56 text.menu.Truncation.addActionListener(this); 57 text.menu.Font.addActionListener(this); 58 text.menu.Color.addActionListener(this); 59 text.menu.ViewHelp.addActionListener(this); 60 text.menu.AboutNotepad.addActionListener(this); 61 text.Undo.addActionListener(this); 62 text.Redo.addActionListener(this); 63 text.Cut.addActionListener(this); 64 text.Copy.addActionListener(this); 65 text.Paste.addActionListener(this); 66 text.Delete.addActionListener(this); 67 text.SelectAll.addActionListener(this); 68 text.FindAndReplace.addActionListener(this); 69 } 70 public void windowClosing(WindowEvent e) { 71 if(text.Saved) System.exit(0); 72 else ensure.Ensure.setVisible(true); 73 if(ensure.Status==ensure.YES && saveFile()) System.exit(0); 74 else if(ensure.Status==ensure.NO) System.exit(0); 75 } 76 public void actionPerformed(ActionEvent e) { 77 Object obj=e.getSource(); 78 if(obj==text.menu.Save) saveFile(); 79 else if(obj==text.menu.SaveAs) saveAsFile(); 80 else if(obj==text.menu.New){ 81 if(!(text.Saved)){ 82 ensure.Ensure.setVisible(true); 83 if(ensure.Status==ensure.YES && saveFile()){} 84 else if(ensure.Status==ensure.NO){} 85 else return; 86 } 87 newFile(); 88 } 89 else if(obj==text.menu.Open){ 90 if(!(text.Saved)){ 91 ensure.Ensure.setVisible(true); 92 if(ensure.Status==ensure.YES && saveFile()){} 93 else if(ensure.Status==ensure.NO){} 94 else return; 95 } 96 openFile(); 97 } 98 else if(obj==text.menu.Print){ 99 try { 100 text.print(); 101 } catch (PrinterException pe){} 102 } 103 else if(obj==text.menu.Exit){ 104 if(text.Saved) System.exit(0); 105 else ensure.Ensure.setVisible(true); 106 if(ensure.Status==ensure.YES && saveFile()) System.exit(0); 107 else if(ensure.Status==ensure.NO) System.exit(0); 108 } 109 else if(obj==text.menu.Undo || obj==text.Undo){ 110 text.Manager.undo(); 111 text.Saved=false; 112 text.menu.Redo.setEnabled(true); 113 text.Redo.setEnabled(true); 114 if(!text.Manager.canUndo()){ 115 text.menu.Undo.setEnabled(false); 116 text.Undo.setEnabled(false); 117 } 118 } 119 else if(obj==text.menu.Redo || obj==text.Redo){ 120 text.Manager.redo(); 121 text.Saved=false; 122 text.menu.Undo.setEnabled(true); 123 text.Undo.setEnabled(true); 124 if(!text.Manager.canRedo()){ 125 text.menu.Redo.setEnabled(false); 126 text.Redo.setEnabled(false); 127 } 128 } 129 else if(obj==text.Cut || obj==text.menu.Cut){ 130 text.cut(); 131 } 132 else if(obj==text.Copy || obj==text.menu.Copy){ 133 text.copy(); 134 } 135 else if(obj==text.Paste || obj==text.menu.Paste){ 136 text.paste(); 137 } 138 else if(obj==text.Delete || obj==text.menu.Delete){ 139 text.delete(); 140 } 141 else if(obj==text.SelectAll || obj==text.menu.SelectAll){ 142 text.selectAll(); 143 } 144 else if(obj==text.FindAndReplace || obj==text.menu.FindAndReplace){ 145 text.find.Dialog.setVisible(true); 146 } 147 else if(obj==text.menu.Font){ 148 font.Dialog.setVisible(true); 149 if(text.getFont()!=font.getFont()) 150 text.setFont(font.getFont()); 151 } 152 else if(obj==text.menu.Color){ 153 color.Dialog.setVisible(true); 154 text.setForeground(color.NFC); 155 text.setBackground(color.NBC); 156 text.setSelectedTextColor(color.SFC); 157 text.setSelectionColor(color.SBC); 158 text.setCaretColor(color.NFC); 159 } 160 else if(obj==text.menu.AboutNotepad){ 161 about.Dialog.setVisible(true); 162 } 163 } 164 public boolean saveFile(){ 165 if(text.Name==null){ 166 dialog.setMode(FileDialog.SAVE); 167 dialog.setTitle("Save As..."); 168 dialog.setFile("Untitled.txt"); 169 dialog.setVisible(true); 170 text.Path=dialog.getDirectory(); 171 text.Name=dialog.getFile(); 172 } 173 if(text.Name==null) return false; 174 text.saveFile(); 175 notepad.setTitle(text.Name+" - Notepad"); 176 return true; 177 } 178 public void saveAsFile(){ 179 String path=text.Path; 180 String name=text.Name; 181 dialog.setMode(FileDialog.SAVE); 182 dialog.setTitle("Save As..."); 183 if(text.Name==null) 184 dialog.setFile("Untitled.txt"); 185 else dialog.setFile(text.Name); 186 dialog.setVisible(true); 187 text.Path=dialog.getDirectory(); 188 text.Name=dialog.getFile(); 189 if(text.Name!=null){ 190 text.saveFile(); 191 notepad.setTitle(text.Name+" - Notepad"); 192 } 193 else{ 194 text.Name=name; 195 text.Path=path; 196 } 197 } 198 public void openFile(){ 199 String path=text.Path; 200 String name=text.Name; 201 dialog.setTitle("Open..."); 202 dialog.setMode(FileDialog.LOAD); 203 dialog.setVisible(true); 204 text.Path=dialog.getDirectory(); 205 text.Name=dialog.getFile(); 206 if(text.Name!=null){ 207 text.openFile(); 208 notepad.setTitle(text.Name+" - Notepad"); 209 } 210 else{ 211 text.Name=name; 212 text.Path=path; 213 } 214 } 215 public void newFile(){ 216 text.Path=null; 217 text.Name=null; 218 text.setText(null); 219 notepad.setTitle("Notepad"); 220 text.Saved=true; 221 text.Undo.setEnabled(false); 222 text.Redo.setEnabled(false); 223 text.menu.Undo.setEnabled(false); 224 text.menu.Redo.setEnabled(false); 225 } 226 public static void main(String s[]){ 227 System.setProperty("java.awt.im.style","on-the-spot"); //去除输入中文时的浮动框 228 Notepad np=new Notepad(); 229 np.notepad.setVisible(true); 230 } 231 public void windowActivated(WindowEvent arg0) {} 232 public void windowClosed(WindowEvent arg0) {} 233 public void windowDeactivated(WindowEvent arg0) {} 234 public void windowDeiconified(WindowEvent arg0) {} 235 public void windowIconified(WindowEvent arg0) {} 236 public void windowOpened(WindowEvent arg0) {} 237}
更多知识欢迎关注微信公众号“51学代码”
作者博客:https://hello1024.world (你好1024的世界)链接

