java编写记事本程序
时间: 2024-01-04 07:03:40 浏览: 89
好的,我可以为您提供一个简单的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() {
setTitle("Simple Notepad");
setSize(500, 500);
setLocationRelativeTo(null);
textArea = new TextArea();
add(textArea);
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);
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) {
dispose();
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newMenuItem) {
textArea.setText("");
} else if (e.getSource() == openMenuItem) {
FileDialog fileDialog = new FileDialog(this, "Open File", FileDialog.LOAD);
fileDialog.setVisible(true);
if (fileDialog.getFile() != null) {
String fileName = fileDialog.getDirectory() + fileDialog.getFile();
setTitle(fileName);
readFile(fileName);
}
} else if (e.getSource() == saveMenuItem) {
FileDialog fileDialog = new FileDialog(this, "Save File", FileDialog.SAVE);
fileDialog.setVisible(true);
if (fileDialog.getFile() != null) {
String fileName = fileDialog.getDirectory() + fileDialog.getFile();
setTitle(fileName);
saveFile(fileName);
}
} else if (e.getSource() == exitMenuItem) {
dispose();
} else if (e.getSource() == cutMenuItem) {
textArea.cut();
} else if (e.getSource() == copyMenuItem) {
textArea.copy();
} else if (e.getSource() == pasteMenuItem) {
textArea.paste();
}
}
private void readFile(String fileName) {
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = reader.readLine()) != null) {
textArea.append(line + "\n");
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void saveFile(String fileName) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(textArea.getText());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SimpleNotepad notepad = new SimpleNotepad();
notepad.setVisible(true);
}
}
```
这个程序提供了基本的记事本功能,包括打开、保存、剪切、复制和粘贴。您可以根据您的需要进行修改和扩展,例如添加其他菜单项或功能。
阅读全文