21xrx.com
2025-03-22 03:16:09 Saturday
文章检索 我的文章 写文章
Java实现简单记事本的完整代码
2023-06-14 16:19:20 深夜i     15     0
Java 简单记事本 代码 JFrame JTextArea JMenuBar

在这个数字化信息时代,我们离不开电子设备的帮助,而电子设备又少不了一款重要的软件——记事本。记事本可以记录我们的想法、计划、待办事项等等。而Java作为一种流行的编程语言,可以实现简单的记事本功能。下面是完整的Java代码:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
 
public class SimpleNotepad extends JFrame implements ActionListener{ 
  JTextArea textArea; 
  JScrollPane scrollPane; 
  JMenuBar menuBar; 
  JMenu menuFile, menuEdit, menuHelp; 
  JMenuItem newMenuItem, openMenuItem, saveMenuItem, 
  cutMenuItem, copyMenuItem, pasteMenuItem, 
  aboutMenuItem, exitMenuItem; 
 
  public static void main(String[] args) { 
    new SimpleNotepad(); 
  } 
 
  public SimpleNotepad(){ 
    setTitle("Untitled - SimpleNotepad"); 
    setSize(800, 600); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
 
    textArea = new JTextArea(); 
    scrollPane = new JScrollPane(textArea); 
    add(scrollPane, BorderLayout.CENTER); 
 
    menuBar = new JMenuBar(); 
    menuFile = new JMenu("File"); 
    menuEdit = new JMenu("Edit"); 
    menuHelp = new JMenu("Help"); 
 
    newMenuItem = new JMenuItem("New"); 
    openMenuItem = new JMenuItem("Open"); 
    saveMenuItem = new JMenuItem("Save"); 
    cutMenuItem = new JMenuItem("Cut"); 
    copyMenuItem = new JMenuItem("Copy"); 
    pasteMenuItem = new JMenuItem("Paste"); 
    aboutMenuItem = new JMenuItem("About"); 
    exitMenuItem = new JMenuItem("Exit"); 
 
    newMenuItem.addActionListener(this); 
    openMenuItem.addActionListener(this); 
    saveMenuItem.addActionListener(this); 
    cutMenuItem.addActionListener(this); 
    copyMenuItem.addActionListener(this); 
    pasteMenuItem.addActionListener(this); 
    aboutMenuItem.addActionListener(this); 
    exitMenuItem.addActionListener(this); 
 
    menuFile.add(newMenuItem); 
    menuFile.add(openMenuItem); 
    menuFile.add(saveMenuItem); 
    menuFile.addSeparator(); 
    menuFile.add(exitMenuItem); 
 
    menuEdit.add(cutMenuItem); 
    menuEdit.add(copyMenuItem); 
    menuEdit.add(pasteMenuItem); 
 
    menuHelp.add(aboutMenuItem); 
 
    menuBar.add(menuFile); 
    menuBar.add(menuEdit); 
    menuBar.add(menuHelp); 
 
    setJMenuBar(menuBar); 
    setVisible(true); 
  } 
 
  public void actionPerformed(ActionEvent e){ 
    if (e.getSource() == newMenuItem) 
      new SimpleNotepad(); 
    else if (e.getSource() == openMenuItem){ 
      JFileChooser open = new JFileChooser(); 
      int option = open.showOpenDialog(this); 
      if (option == JFileChooser.APPROVE_OPTION){ 
        textArea.setText(""); 
        try { 
         Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath())); 
         while (scan.hasNext()) 
          textArea.append(scan.nextLine() + "\n"); 
        } catch (Exception ex) { 
         System.out.println(ex.getMessage()); 
        } 
      } 
    } 
 
    else if (e.getSource() == saveMenuItem){ 
      JFileChooser save = new JFileChooser(); 
      int option = save.showSaveDialog(this); 
      if (option == JFileChooser.APPROVE_OPTION){ 
        try { 
          BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath())); 
          out.write(textArea.getText()); 
          out.close(); 
        } catch (Exception ex) { 
          System.out.println(ex.getMessage()); 
        } 
      } 
    } 
 
    else if (e.getSource() == aboutMenuItem){ 
      JOptionPane.showMessageDialog(this, "Simple Notepad\n\nVersion 1.0\n\nDeveloped by SG.", "About", 
                     JOptionPane.INFORMATION_MESSAGE); 
    } 
 
    else if (e.getSource() == exitMenuItem) 
      System.exit(0); 
  } 
}

通过这个Java代码,我们可以实现简单的记事本功能,包括新建、打开、保存、剪切、复制、粘贴、关于等功能。记事本的界面也非常简洁明了,可以直接在文本框中进行编辑。同时,我们也可以根据需求进行修改和扩展。

  
  

评论区