java记事本写玫瑰花代码_Java记事本源代码(完整)

这个博客展示了如何使用Java编写一个简单的记事本程序,包括文件操作(新建、打开、保存)、编辑功能(剪切、复制、粘贴、查找、替换)和格式设置。记事本实现了自动换行功能,还包含一个字体设置对话框,允许用户自定义字体样式。通过此程序,读者可以了解Java GUI应用的基本构建和事件处理。
摘要由CSDN通过智能技术生成

《Java记事本源代码(完整)》由会员分享,可在线阅读,更多相关《Java记事本源代码(完整)(13页珍藏版)》请在人人文库网上搜索。

1、* 作品:记事本* 作者:* 功能:简单的文字编辑*/import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;class NotePad extends JFrameprivate JMenuBar menuBar;private JMenu fielMenu,editMenu,formMenu,aboutMenu;private JMen。

2、uItem newMenuItem,openMenuItem,saveMenuItem,exitMenuItem;private JMenuItem cutMenuItem,copyMenuItem,pasteMenuItem,foundItem,replaceItem,selectAll;private JMenuItem font,about;private JTextArea textArea;private JFrame foundFrame,replaceFrame;private JCheckBoxMenuItem wrapline;private JTextField textF。

3、ield1=new JTextField(15);private JTextField textField2=new JTextField(15);private JButton startButton,replaceButton,reallButton;int start=0;String value;File file=null;JFileChooser fileChooser=new JFileChooser();boolean wrap=false;public NotePad()/创建文本域textArea=new JTextArea();add(new JScrollPane(te。

4、xtArea),BorderLayout.CENTER);/创建文件菜单及文件菜单项fielMenu=new JMenu(文件);fielMenu.setFont(new Font(微软雅黑,0,15);newMenuItem=new JMenuItem(新建,new ImageIcon(iconsnew24.gif);newMenuItem.setFont(new Font(微软雅黑,Font.BOLD,13);newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK);newMe。

5、nuItem.addActionListener(listener);openMenuItem=new JMenuItem(打开,new ImageIcon(iconsopen24.gif);openMenuItem.setFont(new Font(微软雅黑,Font.BOLD,13);openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK);openMenuItem.addActionListener(listener);saveMenuItem=new JMenuItem(。

6、保存,new ImageIcon(iconssave.gif);saveMenuItem.setFont(new Font(微软雅黑,Font.BOLD,13);saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK);saveMenuItem.addActionListener(listener);exitMenuItem=new JMenuItem(退出,new ImageIcon(iconsexit24.gif);exitMenuItem.setFont(new Font(。

7、微软雅黑,Font.BOLD,13);exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK);exitMenuItem.addActionListener(listener); /创建编辑菜单及菜单项editMenu=new JMenu(编辑);editMenu.setFont(new Font(微软雅黑,0,15);cutMenuItem=new JMenuItem(剪切,new ImageIcon(iconscut24.gif);cutMenuItem.setFont(ne。

8、w Font(微软雅黑,Font.BOLD,13);cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK);cutMenuItem.addActionListener(listener);copyMenuItem=new JMenuItem(复制,new ImageIcon(iconscopy24.gif);copyMenuItem.setFont(new Font(微软雅黑,Font.BOLD,13);copyMenuItem.setAccelerator(KeyStroke.。

9、getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK);copyMenuItem.addActionListener(listener);pasteMenuItem=new JMenuItem(粘贴,new ImageIcon(iconspaste24.gif);pasteMenuItem.setFont(new Font(微软雅黑,Font.BOLD,13);pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK);pasteMenu。

10、Item.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.setFont(new Font(微软雅黑,Font.BO。

11、LD,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.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK);selectAll。

12、.addActionListener(listener);/创建格式菜单及菜单项formMenu=new JMenu(格式);formMenu.setFont(new Font(微软雅黑,0,15);wrapline=new JCheckBoxMenuItem(自动换行);wrapline.setFont(new Font(微软雅黑,Font.BOLD,13);wrapline.addActionListener(listener);wrapline.addChangeListener(new ChangeListener() public void stateChanged(ChangeEv。

13、ent e) if(wrapline.isSelected()textArea.setLineWrap(true);elsetextArea.setLineWrap(false););font=new JMenuItem(字体);font.setFont(new Font(微软雅黑,Font.BOLD,13);font.addActionListener(listener);/创建关于菜单aboutMenu=new JMenu(关于);aboutMenu.setFont(new Font(微软雅黑,0,15);about=new JMenuItem(记事本);about.setFont(new。

14、 Font(微软雅黑,Font.BOLD,13);about.addActionListener(listener);/添加文件菜单项fielMenu.add(newMenuItem);fielMenu.add(openMenuItem);fielMenu.add(saveMenuItem);fielMenu.addSeparator();fielMenu.add(exitMenuItem);/添加编辑菜单项editMenu.add(cutMenuItem);editMenu.add(copyMenuItem);editMenu.add(pasteMenuItem);editMenu.add(。

15、foundItem);editMenu.add(replaceItem);editMenu.addSeparator();editMenu.add(selectAll);/添加格式菜单项formMenu.add(wrapline);formMenu.add(font);/添加关于菜单项aboutMenu.add(about);/添加菜单menuBar=new JMenuBar();menuBar.add(fielMenu);menuBar.add(editMenu);menuBar.add(formMenu);menuBar.add(aboutMenu);setJMenuBar(menuBar。

16、);/创建两个框架,用作查找和替换foundFrame=new JFrame();replaceFrame=new JFrame();/创建两个文本框textField1=new JTextField(15);textField2=new JTextField(15);startButton=new JButton(开始);startButton.addActionListener(listener);replaceButton=new JButton(替换为);replaceButton.addActionListener(listener);reallButton=new JButton(。

17、全部替换);reallButton.addActionListener(listener);/创建菜单项事件监听器ActionListener listener=new ActionListener() public void 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)fileC。

18、hooser.setSelectedFile(file);int returnVal=fileChooser.showOpenDialog(NotePad.this);if(returnVal=JFileChooser.APPROVE_OPTION)file=fileChooser.getSelectedFile();tryFileReader reader=new FileReader(file);int len=(int)file.length();char array=new charlen;reader.read(array,0,len);reader.close();textArea。

19、.setText(new String(array);catch(Exception e_open)e_open.printStackTrace();if(保存.equals(name)if(file!=null)fileChooser.setSelectedFile(file);int returnVal=fileChooser.showSaveDialog(NotePad.this);if(returnVal=JFileChooser.APPROVE_OPTION)file=fileChooser.getSelectedFile();tryFileWriter writer=new Fil。

20、eWriter(file);writer.write(textArea.getText();writer.close();catch (Exception e_save) e_save.getStackTrace();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.ad。

21、d(textField1,BorderLayout.CENTER);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();JLabel label1。

22、=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,BorderLayout.CENTER);panel1.add(label1);panel1.add(tex。

23、tField1);panel1.add(label2);panel1.add(textField2);panel2.add(startButton);panel2.add(replaceButton);panel2.add(reallButton);replaceFrame.setTitle(替换);replaceFrame.setLocation(300,300);replaceFrame.pack();replaceFrame.setVisible(true);replaceFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);if。

24、(开始.equals(name)|下一个.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;startButton.setText(下一个);else JOptionPane.sho。

25、wMessageDialog(foundFrame, 查找完毕!, 提示, 0,new ImageIcon(iconssearch.gif);foundFrame.dispose();if(替换为.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.setSelected。

26、TextColor(Color.GREEN);start=s+1;textArea.replaceSelection(textField2.getText();else JOptionPane.showMessageDialog(foundFrame, 查找完毕!, 提示, 0,new ImageIcon(iconssearch.gif);foundFrame.dispose();if(全部替换.equals(name)String temp=textArea.getText();temp=temp.replaceAll(textField1.getText(),textField2.getT。

27、ext();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();if(记事本.equals(name)AboutDialog aboutDialog=new AboutDi。

28、alog(NotePad.this);aboutDialog.setVisible(true);/创建字体设置对话面板,并添加相应事件监听器class FontDialog extends JDialog implements ItemListener, ActionListener, WindowListenerpublic JCheckBox Bold=new JCheckBox(Bold,false);public JCheckBox Italic=new JCheckBox(Italic,false);public List Size,Name;public int FontName;。

29、public int FontStyle;public int FontSize;public JButton OK=new JButton(OK);public JButton Cancel=new JButton(Cancel);public JTextArea Text=new JTextArea(字体预览文本域nnAaBbCcXxYyZz);public FontDialog(JFrame owner) super(owner,字体设置,true);GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment。

30、(); String name=g.getAvailableFontFamilyNames();Name=new List();Size=new List();FontName=0;FontStyle=0;FontSize=8;int i=0;Name.add(Default Value);for(i=0;iname.length;i+)Name.add(namei);for(i=8;i257;i+) Size.add(String.valueOf(i);this.setLayout(null);this.setBounds(250,200,480, 306);this.setResizabl。

31、e(false);OK.setFocusable(false);Cancel.setFocusable(false);Bold.setFocusable(false);Italic.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(I。

32、talic);Size.setBounds(232, 10, 64, 259);this.add(Size);Text.setBounds(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.a。

33、ddItemListener(this);Bold.addItemListener(this);Italic.addItemListener(this);OK.addActionListener(this);Cancel.addActionListener(this);this.addWindowListener(this);public void itemStateChanged(ItemEvent e) Text.setFont(getFont();public void actionPerformed(ActionEvent e) if(e.getSource()=OK)FontName。

34、=Name.getSelectedIndex();FontStyle=getStyle();FontSize=Size.getSelectedIndex();this.setVisible(false);else cancel();public void windowClosing(WindowEvent e) cancel();public Font getFont()if(Name.getSelectedIndex()=0) return new Font(新宋体,getStyle(),Size.getSelectedIndex()+8);else return new Font(Name。

35、.getSelectedItem(),getStyle(),Size.getSelectedIndex()+8);public void cancel()Name.select(FontName);Size.select(FontSize);setStyle();Text.setFont(getFont();this.setVisible(false);public void setStyle()if(FontStyle=0 | FontStyle=2) Bold.setSelected(false);else Bold.setSelected(true);if(FontStyle=0 | F。

36、ontStyle=1) Italic.setSelected(false);else Italic.setSelected(true);public int getStyle()int bold=0,italic=0;if(Bold.isSelected() bold=1;if(Italic.isSelected() italic=1;return bold+italic*2;public void windowActivated(WindowEvent arg0) public void windowClosed(WindowEvent arg0) public void windowDea。

37、ctivated(WindowEvent arg0) public void windowDeiconified(WindowEvent arg0) public void windowIconified(WindowEvent arg0) public void windowOpened(WindowEvent arg0) /创建关于对话框class AboutDialog extends JDialog implements ActionListenerpublic JButton OK,Icon;public JLabel Name,Version,Author,Java;public 。

38、JPanel Panel;AboutDialog(JFrame owner) super(owner,关于,true); OK=new JButton(OK);Icon=new JButton(new ImageIcon(iconsedit.gif);Name=new JLabel(Notepad);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 。

39、c=new Color(0,95,191);Name.setForeground(c);Version.setForeground(c);Java.setForeground(c);Author.setForeground(c);Panel.setBackground(Color.white);OK.setFocusable(false);this.setBounds(250,200,280, 180);this.setResizable(false);this.setLayout(null);Panel.setLayout(null);OK.addActionListener(this);I。

40、con.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);this.add(Panel);this.add(OK);Panel.setBounds(0, 0, 280, 100);OK.setBounds(180, 114, 72, 26);Name.setBounds(80, 10, 160。

41、, 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) this.setVisible(false);/创建记事本public class ChenJianmin public static void main(String args)EventQueue.invokeLater(new Runnable() public void run() NotePad notePad=new NotePad();notePad.setTitle(记事本);notePad.setVisible(true);notePad.setBounds(100,100,800,600);notePad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值