用java语言设计简易记事本软件
时间: 2023-12-04 07:05:50 浏览: 36
好的,以下是一个简单的Java记事本程序的代码示例:
```
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SimpleNotepad extends Frame implements ActionListener {
private TextArea textArea;
private MenuBar menuBar;
private Menu fileMenu, editMenu;
private MenuItem newMenuItem, openMenuItem, saveMenuItem, exitMenuItem, cutMenuItem, copyMenuItem, pasteMenuItem;
public SimpleNotepad() {
super("Simple Notepad");
// 创建菜单栏和菜单项
menuBar = new MenuBar();
fileMenu = new Menu("File");
newMenuItem = new MenuItem("New");
openMenuItem = new MenuItem("Open");
saveMenuItem = new MenuItem("Save");
exitMenuItem = new MenuItem("Exit");
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
editMenu = new Menu("Edit");
cutMenuItem = new MenuItem("Cut");
copyMenuItem = new MenuItem("Copy");
pasteMenuItem = new MenuItem("Paste");
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
// 将菜单栏设置为窗口的菜单栏
setMenuBar(menuBar);
// 创建文本区域
textArea = new TextArea();
// 将文本区域添加到窗口中
add(textArea);
// 注册菜单项的事件处理程序
newMenuItem.addActionListener(this);
openMenuItem.addActionListener(this);
saveMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
cutMenuItem.addActionListener(this);
copyMenuItem.addActionListener(this);
pasteMenuItem.addActionListener(this);
// 注册窗口关闭事件处理程序
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 设置窗口的大小和位置
setSize(500, 400);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newMenuItem) {
textArea.setText("");
} else if (e.getSource() == openMenuItem) {
FileDialog dialog = new FileDialog(this, "Open", FileDialog.LOAD);
dialog.setVisible(true);
if (dialog.getFile() != null) {
String filename = dialog.getDirectory() + dialog.getFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line = null;
textArea.setText("");
while ((line = reader.readLine()) != null) {
textArea.append(line + "\n");
}
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} else if (e.getSource() == saveMenuItem) {
FileDialog dialog = new FileDialog(this, "Save", FileDialog.SAVE);
dialog.setVisible(true);
if (dialog.getFile() != null) {
String filename = dialog.getDirectory() + dialog.getFile();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(textArea.getText());
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} else if (e.getSource() == exitMenuItem) {
System.exit(0);
} else if (e.getSource() == cutMenuItem) {
textArea.cut();
} else if (e.getSource() == copyMenuItem) {
textArea.copy();
} else if (e.getSource() == pasteMenuItem) {
textArea.paste();
}
}
public static void main(String[] args) {
new SimpleNotepad();
}
}
```
这个程序创建了一个带有菜单栏、文本区域和一些菜单项的窗口。用户可以使用菜单项来新建、打开、保存和退出文件,以及剪切、复制和粘贴文本。该程序使用了Java的AWT和IO包,可以在任何支持Java的操作系统上运行。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)