Java完整记事本源代码

天使之爱你 贡献于2012-08-26

作者 Sky  创建于2011-05-10 04:57:00   修改者Sky  修改于2011-05-10 04:57:00字数31984

文档摘要:Java 完整记事本源代码
关键词:

AboutDialog.java package notepad; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class AboutDialog implements ActionListener{ public JDialog Dialog; public JButton OK,Icon; public JLabel Name,Version,Author,Java; public JPanel Panel; AboutDialog(JFrame notepad, int x, int y) { Dialog=new JDialog(notepad,"About Notepad...",true); OK=new JButton("OK"); Icon=new JButton(new ImageIcon("Notepad.jpg")); Name=new JLabel("Notepad"); Version=new JLabel("Version 1.0"); Java=new JLabel("JRE Version 6.0"); Author=new JLabel("Copyright (c) 2011 Sky. No rights reserved."); Panel=new JPanel(); Color c=new Color(0,95,191); Name.setForeground(c); Version.setForeground(c); Java.setForeground(c); Panel.setBackground(Color.WHITE); OK.setFocusable(false); Dialog.setSize(280, 180); Dialog.setLocation(x, y); Dialog.setResizable(false); Dialog.setLayout(null); Panel.setLayout(null); OK.addActionListener(this); Icon.setFocusable(false); Icon.setBorderPainted(false); Author.setFont(new Font(null,Font.PLAIN,11)); Panel.add(Icon); Panel.add(Name); Panel.add(Version); Panel.add(Author); Panel.add(Java); Dialog.add(Panel); Dialog.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); Java.setBounds(80, 44, 160, 20); Icon.setBounds(16, 14, 48, 48); } public void actionPerformed(ActionEvent e) { Dialog.setVisible(false); } } ColorDialog.java package notepad; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class ColorDialog implements ActionListener, WindowListener{ public JDialog Dialog; public JLabel NFL,NBL,SFL,SBL; public JTextArea Normal,Selected; public JButton NFB,NBB,SFB,SBB,OK,Cancel,Reset; public Color NFC,NBC,SFC,SBC; public ColorChooser Chooser; public ColorDialog(JFrame notepad, int x, int y){ NFC=new Color(0,0,0); NBC=new Color(249,249,251); SFC=new Color(0,0,0); SBC=new Color(191,207,223); Dialog=new JDialog(notepad,"Color...",true); NFL=new JLabel("Normal Foreground:"); NBL=new JLabel("Normal Background:"); SFL=new JLabel("Selected Foreground:"); SBL=new JLabel("Selected Background:"); Normal=new JTextArea("\n Normal 正常"); Selected=new JTextArea("\n Selected 选中 "); NFB=new JButton(""); NBB=new JButton(""); SFB=new JButton(""); SBB=new JButton(""); OK=new JButton("OK"); Cancel=new JButton("Cancel"); Reset=new JButton("Reset"); Chooser=new ColorChooser(Dialog, x+65, y-15); Normal.setEditable(false); Normal.setFocusable(false); Normal.setFont(new Font("新宋体", 0, 16)); Normal.setForeground(NFC); Normal.setBackground(NBC); Selected.setEditable(false); Selected.setFocusable(false); Selected.setFont(Normal.getFont()); Selected.setForeground(SFC); Selected.setBackground(SBC); NFB.setBackground(NFC); NBB.setBackground(NBC); SFB.setBackground(SFC); SBB.setBackground(SBC); Dialog.setLayout(null); Dialog.setLocation(x, y); Dialog.setSize(410, 220); Dialog.setResizable(false); Reset.setFocusable(false); OK.setFocusable(false); Cancel.setFocusable(false); Dialog.add(Normal); Dialog.add(Selected); Dialog.add(NFL); Dialog.add(NBL); Dialog.add(SFL); Dialog.add(SBL); Dialog.add(SBB); Dialog.add(SFB); Dialog.add(NBB); Dialog.add(NFB); Dialog.add(OK); Dialog.add(Cancel); Dialog.add(Reset); SBB.setBounds(144, 100, 60, 22); SFB.setBounds(144, 70, 60, 22); NBB.setBounds(144, 40, 60, 22); NFB.setBounds(144, 10, 60, 22); NFL.setBounds(10, 10, 130, 22); NBL.setBounds(10, 40, 130, 22); SFL.setBounds(10, 70, 130, 22); SBL.setBounds(10, 100, 130, 22); Normal.setBounds(220, 10, 174, 56); Selected.setBounds(220, 66, 174, 56); Reset.setBounds(10, 160, 74, 24); OK.setBounds(236, 160, 74, 24); Cancel.setBounds(320, 160, 74, 24); Dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); Dialog.addWindowListener(this); NFB.addActionListener(this); NBB.addActionListener(this); SFB.addActionListener(this); SBB.addActionListener(this); Reset.addActionListener(this); OK.addActionListener(this); Cancel.addActionListener(this); } public void setTextAreaColor(){ Normal.setForeground(NFB.getBackground()); Normal.setBackground(NBB.getBackground()); Selected.setForeground(SFB.getBackground()); Selected.setBackground(SBB.getBackground()); } public void cancel(){ Normal.setForeground(NFC); Normal.setBackground(NBC); Selected.setForeground(SFC); Selected.setBackground(SBC); NFB.setBackground(NFC); NBB.setBackground(NBC); SFB.setBackground(SFC); SBB.setBackground(SBC); Dialog.setVisible(false); } public void actionPerformed(ActionEvent e) { Object obj=e.getSource(); if(obj==Reset){ NFB.setBackground(new Color(0,0,0)); NBB.setBackground(new Color(249,249,251)); SFB.setBackground(new Color(0,0,0)); SBB.setBackground(new Color(191,207,223)); setTextAreaColor(); } else if(obj==OK){ NFC=NFB.getBackground(); NBC=NBB.getBackground(); SFC=SFB.getBackground(); SBC=SBB.getBackground(); Dialog.setVisible(false); } else if(obj==Cancel) cancel(); else{ Chooser.init(((Component) obj).getBackground()); Chooser.Dialog.setVisible(true); ((Component) obj).setBackground(Chooser.New.getBackground()); setTextAreaColor(); } } public void windowClosing(WindowEvent e) { cancel(); } public void windowActivated(WindowEvent arg0) {} public void windowClosed(WindowEvent arg0) {} public void windowDeactivated(WindowEvent arg0) {} public void windowDeiconified(WindowEvent arg0) {} public void windowIconified(WindowEvent arg0) {} public void windowOpened(WindowEvent arg0) {} } class ColorChooser implements ActionListener,WindowListener,KeyListener{ JDialog Dialog; JButton Choice[][],Old,New,OK,Cancel; JPanel Panel; JTextField R,G,B; JLabel OldLabel,NewLabel,RL,GL,BL; ColorChooser(JDialog color,int x, int y){ Dialog=new JDialog(color,true); Choice=new JButton[8][8]; Panel=new JPanel(); Old=new JButton(""); New=new JButton(""); OldLabel=new JLabel("Old:"); NewLabel=new JLabel("New:"); RL=new JLabel("R:"); GL=new JLabel("G:"); BL=new JLabel("B:"); R=new JTextField(""); G=new JTextField(""); B=new JTextField(""); OK=new JButton("OK"); Cancel=new JButton("Cancel"); Panel.setLayout(new GridLayout(8,8,0,0)); int i=0,j=0; Color c; Choice[0][7]=new JButton("");Choice[0][7].setBackground(new Color(255,255,255)); Choice[1][7]=new JButton("");Choice[1][7].setBackground(new Color(255,223,191)); Choice[2][7]=new JButton("");Choice[2][7].setBackground(new Color(255,207,207)); Choice[3][7]=new JButton("");Choice[3][7].setBackground(new Color(223,191,255)); Choice[4][7]=new JButton("");Choice[4][7].setBackground(new Color(207,207,255)); Choice[5][7]=new JButton("");Choice[5][7].setBackground(new Color(191,223,255)); Choice[6][7]=new JButton("");Choice[6][7].setBackground(new Color(207,255,207)); Choice[7][7]=new JButton("");Choice[7][7].setBackground(new Color(223,255,191)); for(i=0;i<8;i++){ c=Choice[i][7].getBackground(); for(j=0;j<8;j++){ if(j!=7){ Choice[i][j]=new JButton(""); Choice[i][j].setBackground(new Color(c.getRed()*(j+1)/8,c.getGreen()*(j+1)/8,c.getBlue()*(j+1)/8)); } Choice[i][j].setFocusable(false); Choice[i][j].addActionListener(this); Panel.add(Choice[i][j]); } } Dialog.setSize(280,250); Dialog.setLayout(null); Dialog.setLocation(x, y); Dialog.setResizable(false); Dialog.add(Panel); Panel.setBounds(10, 10, 160, 160); Dialog.add(Old); Dialog.add(OldLabel); Old.setEnabled(false); Old.setBorderPainted(false); Old.setBounds(214, 10, 44, 22); OldLabel.setBounds(180, 10, 44, 22); Dialog.add(New); Dialog.add(NewLabel); New.setEnabled(false); New.setBorderPainted(false); New.setBounds(214, 32, 44, 22); NewLabel.setBounds(180, 32, 44, 22); Dialog.add(R); Dialog.add(G); Dialog.add(B); R.setBounds(214, 97, 44, 22); G.setBounds(214, 123, 44, 22); B.setBounds(214, 149, 44, 22); Dialog.add(RL); Dialog.add(GL); Dialog.add(BL); RL.setBounds(196, 97, 16, 22); GL.setBounds(196, 123, 16, 22); BL.setBounds(196, 149, 16, 22); Dialog.add(OK); Dialog.add(Cancel); OK.setFocusable(false); Cancel.setFocusable(false); OK.setBounds(106, 190, 74, 24); Cancel.setBounds(190, 190, 74, 24); OK.addActionListener(this); Cancel.addActionListener(this); G.addKeyListener(this); R.addKeyListener(this); B.addKeyListener(this); } public void setText(Color c){ R.setText(String.valueOf(c.getRed())); G.setText(String.valueOf(c.getGreen())); B.setText(String.valueOf(c.getBlue())); } public void init(Color c){ New.setBackground(c); Old.setBackground(c); setText(c); } public void actionPerformed(ActionEvent e) { Object obj=e.getSource(); if(obj==OK) Dialog.setVisible(false); else if(obj==Cancel){ New.setBackground(Old.getBackground()); Dialog.setVisible(false); } else{ New.setBackground(((Component) obj).getBackground()); setText(New.getBackground()); } } public void windowClosing(WindowEvent e) { New.setBackground(Old.getBackground()); Dialog.setVisible(false); } public void keyReleased(KeyEvent e) { try{ int r,g,b; if(R.getText().length()==0) r=0; else r=Integer.valueOf(R.getText()); if(G.getText().length()==0) g=0; else g=Integer.valueOf(G.getText()); if(B.getText().length()==0) b=0; else b=Integer.valueOf(B.getText()); New.setBackground(new Color(r,g,b)); } catch(NumberFormatException nfe){setText(New.getBackground());} catch(IllegalArgumentException iae){setText(New.getBackground());} } public void keyPressed(KeyEvent e) {} public void keyTyped(KeyEvent e) {} public void windowActivated(WindowEvent arg0) {} public void windowClosed(WindowEvent arg0) {} public void windowDeactivated(WindowEvent arg0) {} public void windowDeiconified(WindowEvent arg0) {} public void windowIconified(WindowEvent arg0) {} public void windowOpened(WindowEvent arg0) {} } EnsureDialog.java package notepad; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class EnsureDialog implements WindowListener, ActionListener{ public int YES,NO,CANCEL,Status; public JDialog Ensure; public JButton Yes,No,Cancel; public JLabel Question; public JPanel ButtonPanel,TextPanel; EnsureDialog(JFrame notepad, int x, int y) { YES=0; NO=1; CANCEL=2; Status=CANCEL; Ensure=new JDialog(notepad,true);/* * 这里的模式标志true的作用是使对话框处于notepad的上端,并且当对话框显示时进程处于停滞状态, * 直到对话框不再显示为止。在本程序中,由于对对话框进行了事件监听处理,当对话框消失时状态标 * 志Status同时发生了变化,这样就可以在进程继续时获得新的Status值 */ Yes=new JButton("Yes"); No=new JButton("No"); Cancel=new JButton("Cancel"); Question=new JLabel(" Do you want to save changes to the file?"); ButtonPanel=new JPanel(); TextPanel=new JPanel(); ButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,16,10)); TextPanel.setLayout(new BorderLayout()); Ensure.setLayout(new BorderLayout()); ButtonPanel.add(Yes); ButtonPanel.add(No); ButtonPanel.add(Cancel); TextPanel.add(Question); Ensure.add(TextPanel,BorderLayout.CENTER); Ensure.add(ButtonPanel,BorderLayout.SOUTH); Ensure.setLocation(x, y); Ensure.setSize(360, 140); Ensure.setResizable(false); TextPanel.setBackground(Color.WHITE); Question.setFont(new Font(null,Font.PLAIN,16)); Question.setForeground(new Color(0,95,191)); Yes.setFocusable(false); No.setFocusable(false); Cancel.setFocusable(false); Ensure.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); Ensure.addWindowListener(this); Yes.addActionListener(this); No.addActionListener(this); Cancel.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource()==Yes) Status=YES; else if(e.getSource()==No) Status=NO; else if(e.getSource()==Cancel) Status=CANCEL; Ensure.setVisible(false); } public void windowClosing(WindowEvent e){ Status=CANCEL; Ensure.setVisible(false); } public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} } FindAndReplace.java package notepad; import java.awt.TextField; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; public class FindAndReplace{ public JDialog Dialog; public JButton FindNext,Replace,ReplaceAll,Finish; public JCheckBox MatchCase; public JRadioButton Up,Down; public ButtonGroup DirectionGroup; public JLabel FindWhat,ReplaceWith,Direction; public TextField FindText,ReplaceText; FindAndReplace(JFrame notepad){ Dialog=new JDialog(notepad,"Find And Replace...",false);/* * 与EnsureDialog不同的是,这里的模式标志false使对话框始终处于notepad的上端,但点击notepad * 时notepad会继续处于活动状态,对话框则变成不活动状态 */ FindNext=new JButton("Find Next"); Replace=new JButton("Replace"); ReplaceAll=new JButton("Replace All"); Finish=new JButton("Finish"); MatchCase=new JCheckBox("Match Case",false); Down=new JRadioButton("Down",true); Up=new JRadioButton("Up",false); FindWhat=new JLabel("Find What:"); ReplaceWith=new JLabel("Replace With:"); Direction=new JLabel("Direction:"); FindText=new TextField(""); ReplaceText=new TextField(""); Dialog.setSize(380, 160); Dialog.setResizable(false); FindNext.setFocusable(false); Replace.setFocusable(false); ReplaceAll.setFocusable(false); MatchCase.setFocusable(false); Finish.setFocusable(false); Up.setFocusable(false); Down.setFocusable(false); DirectionGroup=new ButtonGroup(); Dialog.setLayout(null); FindWhat.setBounds(10,12,80,22); ReplaceWith.setBounds(10,42,80,22); FindText.setBounds(95, 12, 160, 22); ReplaceText.setBounds(95, 42, 160, 22); FindNext.setBounds(265, 12, 98, 22); Replace.setBounds(265, 42, 98, 22); ReplaceAll.setBounds(265, 72, 98, 22); Direction.setBounds(10, 72, 80, 22); MatchCase.setBounds(6, 102, 98, 22); Down.setBounds(95, 72, 80, 22); Up.setBounds(175, 72, 80, 22); Finish.setBounds(265, 102, 98, 22); DirectionGroup.add(Up); DirectionGroup.add(Down); Dialog.add(FindWhat); Dialog.add(MatchCase); Dialog.add(FindText); Dialog.add(FindNext); Dialog.add(Direction); Dialog.add(ReplaceWith); Dialog.add(ReplaceText); Dialog.add(Replace); Dialog.add(ReplaceAll); Dialog.add(Finish); Dialog.add(Down); Dialog.add(Up); } } FontDialog.java package notepad; import java.awt.Font; import java.awt.GraphicsEnvironment; import java.awt.List; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JTextArea; public class FontDialog implements ItemListener, ActionListener, WindowListener{ public JDialog Dialog; public JCheckBox Bold,Italic; public List Size,Name; public int FontName; public int FontStyle; public int FontSize; public JButton OK,Cancel; public JTextArea Text; FontDialog(JFrame notepad, int x, int y) { GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment(); String name[]=g.getAvailableFontFamilyNames(); Bold=new JCheckBox("Bold",false); Italic=new JCheckBox("Italic",false); Dialog=new JDialog(notepad,"Font...",true); Text=new JTextArea("字体预览用例\n9876543210\nAaBbCcXxYyZz"); OK=new JButton("OK"); Cancel=new JButton("Cancel"); Size=new List(); Name=new List(); int i=0; Name.add("Default Value"); for(i=0;i

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 3 人已下载

下载文档