使用Java设计一个类似于Windows中的记事本,只保留基础功能,并对每段程序进行注释
时间: 2024-03-23 22:41:01 浏览: 14
好的,我可以为您提供一个简单的Java记事本实现,并添加注释说明。下面是实现代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class SimpleNotepad extends JFrame implements ActionListener {
private JTextArea textArea = new JTextArea(); // 文本区域
public SimpleNotepad() {
super("SimpleNotepad");
setJMenuBar(createMenuBar()); // 创建菜单栏
getContentPane().add(createToolBar(), BorderLayout.NORTH); // 创建工具栏
getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER); // 添加滚动条
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
setVisible(true);
}
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenuItem newMenuItem = new JMenuItem("新建");
JMenuItem openMenuItem = new JMenuItem("打开");
JMenuItem saveMenuItem = new JMenuItem("保存");
JMenuItem exitMenuItem = new JMenuItem("退出");
newMenuItem.addActionListener(this);
openMenuItem.addActionListener(this);
saveMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
return menuBar;
}
private JToolBar createToolBar() {
JToolBar toolBar = new JToolBar();
JButton newButton = new JButton(new ImageIcon("new.gif"));
JButton openButton = new JButton(new ImageIcon("open.gif"));
JButton saveButton = new JButton(new ImageIcon("save.gif"));
newButton.addActionListener(this);
openButton.addActionListener(this);
saveButton.addActionListener(this);
toolBar.add(newButton);
toolBar.add(openButton);
toolBar.add(saveButton);
return toolBar;
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("新建")) {
textArea.setText("");
} else if (command.equals("打开")) {
openFile();
} else if (command.equals("保存")) {
saveFile();
} else if (command.equals("退出")) {
System.exit(0);
}
}
private void openFile() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
textArea.append(line + "\n"); // 读取文件并添加到文本区域
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void saveFile() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText()); // 将文本区域的内容写入文件
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new SimpleNotepad();
}
}
```
这个简单的记事本实现了基本的新建、打开、保存和退出功能。它使用了JMenuBar、JToolBar和JTextArea等Swing组件,可以在Windows、Linux和Mac OS X上运行。希望这个实现代码和注释能够帮助到您。