使用java编写的记事本程序代码
时间: 2023-07-11 17:17:01 浏览: 113
以下是一个简单的Java Swing记事本程序的代码,你可以参考它来编写你自己的程序:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SimpleNotepad extends JFrame implements ActionListener {
private JTextArea textArea;
private JScrollPane scrollPane;
private JMenuBar menuBar;
private JMenu fileMenu, editMenu;
private JMenuItem newFileItem, openFileItem, saveFileItem, saveAsFileItem, exitFileItem;
private JMenuItem undoItem, redoItem, cutItem, copyItem, pasteItem, selectAllItem;
private File currentFile;
public SimpleNotepad() {
super("Simple Notepad");
textArea = new JTextArea(20, 60);
textArea.setFont(new Font("Consolas", Font.PLAIN, 14));
scrollPane = new JScrollPane(textArea);
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
newFileItem = new JMenuItem("New");
openFileItem = new JMenuItem("Open");
saveFileItem = new JMenuItem("Save");
saveAsFileItem = new JMenuItem("Save As");
exitFileItem = new JMenuItem("Exit");
editMenu = new JMenu("Edit");
undoItem = new JMenuItem("Undo");
redoItem = new JMenuItem("Redo");
cutItem = new JMenuItem("Cut");
copyItem = new JMenuItem("Copy");
pasteItem = new JMenuItem("Paste");
selectAllItem = new JMenuItem("Select All");
newFileItem.addActionListener(this);
openFileItem.addActionListener(this);
saveFileItem.addActionListener(this);
saveAsFileItem.addActionListener(this);
exitFileItem.addActionListener(this);
undoItem.addActionListener(this);
redoItem.addActionListener(this);
cutItem.addActionListener(this);
copyItem.addActionListener(this);
pasteItem.addActionListener(this);
selectAllItem.addActionListener(this);
fileMenu.add(newFileItem);
fileMenu.add(openFileItem);
fileMenu.add(saveFileItem);
fileMenu.add(saveAsFileItem);
fileMenu.addSeparator();
fileMenu.add(exitFileItem);
editMenu.add(undoItem);
editMenu.add(redoItem);
editMenu.addSeparator();
editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);
editMenu.addSeparator();
editMenu.add(selectAllItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
setJMenuBar(menuBar);
add(scrollPane);
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new SimpleNotepad();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newFileItem) {
newFile();
} else if (e.getSource() == openFileItem) {
openFile();
} else if (e.getSource() == saveFileItem) {
saveFile();
} else if (e.getSource() == saveAsFileItem) {
saveAsFile();
} else if (e.getSource() == exitFileItem) {
exit();
} else if (e.getSource() == undoItem) {
textArea.undo();
} else if (e.getSource() == redoItem) {
textArea.redo();
} else if (e.getSource() == cutItem) {
textArea.cut();
} else if (e.getSource() == copyItem) {
textArea.copy();
} else if (e.getSource() == pasteItem) {
textArea.paste();
} else if (e.getSource() == selectAllItem) {
textArea.selectAll();
}
}
private void newFile() {
textArea.setText("");
currentFile = null;
}
private void openFile() {
JFileChooser fileChooser = new JFileChooser(".");
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
FileReader reader = new FileReader(file);
textArea.read(reader, null);
reader.close();
currentFile = file;
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void saveFile() {
if (currentFile != null) {
try {
FileWriter writer = new FileWriter(currentFile);
textArea.write(writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
saveAsFile();
}
}
private void saveAsFile() {
JFileChooser fileChooser = new JFileChooser(".");
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
FileWriter writer = new FileWriter(file);
textArea.write(writer);
writer.close();
currentFile = file;
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void exit() {
System.exit(0);
}
}
```
这个程序的功能包括新建、打开、保存、另存为、撤销、重做、剪切、复制、粘贴、选择全部等功能。注意,这只是一个简单的示例程序,你可以根据实际需求进行修改和扩展。
阅读全文