一个简单的记事本

一个简单的记事本。版本V1.0 

支持:文件的新建,打开,保存,只读,退出(含快捷键)

Code:
  1. package myclass.notepad;   
  2.   
  3. import java.awt.Frame;   
  4. import java.awt.HeadlessException;   
  5. import java.awt.event.*;   
  6. import java.awt.MenuBar;   
  7. import java.awt.Menu;   
  8. import java.awt.MenuItem;   
  9. import java.awt.MenuShortcut;   
  10. import java.awt.CheckboxMenuItem;   
  11. import java.awt.FileDialog;   
  12. import java.io.FileInputStream;   
  13. import java.io.*;   
  14. import java.awt.TextArea;   
  15.   
  16. /**  
  17.  * <p>Title:记事本 </p>  
  18.  * <p>Description:简单的记事本程序 </p>  
  19.  * <p>Copyright: Copyright (c) 2010</p>  
  20.  * <p>Company:new core </p>  
  21.  * @author not attributable  
  22.  * @version 1.0  
  23.  */  
  24.   
  25. public class Notepad  {   
  26. public static String path=null;   
  27.   public static void main(String[] args) throws HeadlessException {   
  28.    final Frame frame=new Frame("简单的记事本");   
  29.     frame.setSize(800,600);   
  30.     frame.setLocation(200,200);   
  31.     frame.addWindowListener(new WindowAdapter() {   
  32.             public void windowClosing(WindowEvent e) {   
  33.               System.exit(0);   
  34.             }   
  35.           });   
  36.     MenuBar bar=new MenuBar();   
  37.     Menu menu=new Menu("文件",true);   
  38.     menu.addSeparator();   
  39.     Menu menu1=new Menu("编辑",true);   
  40.      menu1.addSeparator();   
  41.    Menu more=new Menu("更多");   
  42.   
  43.    final TextArea ta=new TextArea();   
  44.   
  45.      MenuShortcut ms = new MenuShortcut(KeyEvent.VK_S, false);   
  46.     MenuShortcut ms1 = new MenuShortcut(KeyEvent.VK_O, false);   
  47.     MenuShortcut ms2 = new MenuShortcut(KeyEvent.VK_F4, false);   
  48.     MenuItem mi=new MenuItem("新建");   
  49.     mi.addActionListener(new ActionListener() {   
  50.          public void actionPerformed(ActionEvent e) {   
  51.            ta.setText("");   
  52.          }   
  53.        });   
  54.     MenuItem mi1=new MenuItem("打开",ms1);   
  55.    final CheckboxMenuItem cm=new CheckboxMenuItem("只读",false);   
  56.    cm.addItemListener(new ItemListener() {   
  57.         public void itemStateChanged(ItemEvent e) {   
  58.           if(cm.getState()){   
  59.             ta.setEditable(false);   
  60.           }else{   
  61.             ta.setEditable(true);   
  62.           }   
  63.   
  64.         }   
  65.       });   
  66.     mi1.addActionListener(new ActionListener() {   
  67.           public void actionPerformed(ActionEvent e) {   
  68.             FileDialog fd=new FileDialog(frame,"打开文件",FileDialog.LOAD);   
  69.             fd.show();   
  70.             //System.out.println(fd.getFile());   
  71.               path=fd.getDirectory()+fd.getFile();   
  72.               //System.out.println(path);   
  73.             if(path!=null){   
  74.               try {   
  75.                 FileInputStream is = new FileInputStream(path);   
  76.                 byte b[]=new byte[10*1024];   
  77.                   int len = is.read(b);   
  78.                   if(cm.getState()){   
  79.                     ta.setEditable(false);   
  80.                     }   
  81.                   //清空先前的内容   
  82.                   ta.setText("");   
  83.                   ta.append(new String(b,0,len));   
  84.               }   
  85.               catch (FileNotFoundException ex) {   
  86.                 System.out.println("找不到该文件");   
  87.               }   
  88.               catch (IOException ex1) {   
  89.                 }   
  90.             }   
  91.           }   
  92.         });   
  93.   
  94.     MenuItem separa=new MenuItem("-");   
  95.     MenuItem mi2=new MenuItem("保存",ms);   
  96.   
  97.     mi2.addActionListener(new ActionListener() {   
  98.               public void actionPerformed(ActionEvent e) {   
  99.                  String a=ta.getText();   
  100.                 if(path!=null){   
  101.                   BufferedWriter wri = null;   
  102.                   try {   
  103.                     wri = new BufferedWriter(new FileWriter(path));   
  104.                     wri.write(a);   
  105.                     wri.close();   
  106.                   }   
  107.                   catch (IOException ex1) {   
  108.                   }   
  109.   
  110.                }else{   
  111.   
  112.                  FileDialog fd=new FileDialog(frame,"保存文件",FileDialog.SAVE);   
  113.                  fd.show();   
  114.   
  115.                  String path1=fd.getDirectory()+fd.getFile();   
  116.                try {   
  117.                  BufferedWriter wri = new BufferedWriter(new FileWriter(path1+".txt"));   
  118.                  wri.write(a);   
  119.                  wri.close();   
  120.                }   
  121.                catch (IOException ex) {   
  122.                }   
  123.   
  124.                }   
  125.   
  126.               }   
  127.             });   
  128.   
  129.     MenuItem mi3=new MenuItem("复制");   
  130.     MenuItem mi4=new MenuItem("粘贴");   
  131.     MenuItem mi5=new MenuItem("剪切");   
  132.   
  133.     MenuItem mi7=new MenuItem("另存为");   
  134.    MenuItem mi8=new MenuItem("页面设置");   
  135.    MenuItem mi9=new MenuItem("打印");   
  136.    MenuItem mi10=new MenuItem("退出",ms2);   
  137.    mi10.addActionListener((new ActionListener() {   
  138.                             public void actionPerformed(ActionEvent e) {   
  139.                               System.exit(0);   
  140.                             }   
  141.                           })   
  142.                            );   
  143.   
  144.     menu.add(mi);   
  145.     menu.add(mi1);   
  146.     menu.add(separa);   
  147.     menu.add(mi2);   
  148.     menu.add(cm);   
  149.     menu.add(more);   
  150.     menu.add(mi10);   
  151.     menu1.add(mi3);   
  152.     menu1.add(mi4);   
  153.     menu1.add(mi5);   
  154.   
  155.     more.add(mi7);   
  156.     more.add(mi8);   
  157.     more.add(mi9);   
  158.   
  159.     bar.add(menu);   
  160.     bar.add(menu1);   
  161.     frame.setMenuBar(bar);   
  162.     frame.add(ta);   
  163.     frame.show();   
  164.   }   
  165.   
  166. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简易的记事本GUI的Java代码示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class SimpleNotepad extends JFrame implements ActionListener { private JTextArea textArea = new JTextArea(); private JMenuBar menuBar = new JMenuBar(); private JMenu fileMenu = new JMenu("File"); private JMenuItem newMenuItem = new JMenuItem("New"); private JMenuItem openMenuItem = new JMenuItem("Open"); private JMenuItem saveMenuItem = new JMenuItem("Save"); private JMenuItem exitMenuItem = new JMenuItem("Exit"); public SimpleNotepad() { setTitle("Simple Notepad"); // Set the size of the window setSize(640, 480); // Set the location of the window setLocationRelativeTo(null); // Add the text area to the content pane getContentPane().add(textArea); // Add the menu bar to the window setJMenuBar(menuBar); // Add the file menu to the menu bar menuBar.add(fileMenu); // Add the menu items to the file menu fileMenu.add(newMenuItem); fileMenu.add(openMenuItem); fileMenu.add(saveMenuItem); fileMenu.addSeparator(); fileMenu.add(exitMenuItem); // Add the action listeners to the menu items newMenuItem.addActionListener(this); openMenuItem.addActionListener(this); saveMenuItem.addActionListener(this); exitMenuItem.addActionListener(this); // Show the window setVisible(true); } public void actionPerformed(ActionEvent e) { // If the user clicks the "New" menu item if (e.getSource() == newMenuItem) { // Clear the text area textArea.setText(""); } // If the user clicks the "Open" menu item else if (e.getSource() == openMenuItem) { // Create a file chooser dialog JFileChooser fileChooser = new JFileChooser(); // Show the dialog int result = fileChooser.showOpenDialog(this); // If the user clicks the "Open" button if (result == JFileChooser.APPROVE_OPTION) { // Get the selected file File file = fileChooser.getSelectedFile(); try { // Open the file and read its contents into the text area BufferedReader reader = new BufferedReader(new FileReader(file)); String line = null; while ((line = reader.readLine()) != null) { textArea.append(line + "\n"); } reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } // If the user clicks the "Save" menu item else if (e.getSource() == saveMenuItem) { // Create a file chooser dialog JFileChooser fileChooser = new JFileChooser(); // Show the dialog int result = fileChooser.showSaveDialog(this); // If the user clicks the "Save" button if (result == JFileChooser.APPROVE_OPTION) { // Get the selected file File file = fileChooser.getSelectedFile(); try { // Save the contents of the text area to the file BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(textArea.getText()); writer.close(); } catch (IOException ex) { ex.printStackTrace(); } } } // If the user clicks the "Exit" menu item else if (e.getSource() == exitMenuItem) { // Exit the application System.exit(0); } } public static void main(String[] args) { new SimpleNotepad(); } } ``` 这个记事本GUI包括了菜单栏和一个文本编辑区域,并且可以实现新建、打开、保存和退出功能。你可以根据自己的需求进行修改和扩展。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值