java 记事本界面_Java图形界面开发之简易记事本代码
时间: 2023-10-05 14:05:25 浏览: 189
以下是一个简单的Java记事本界面的代码,包括文件菜单、编辑菜单和帮助菜单。您可以在此基础上进行修改和完善。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleNotepad extends JFrame implements ActionListener {
private JTextArea textArea = new JTextArea();
private JScrollPane scrollPane = new JScrollPane(textArea);
private String filename = "";
public SimpleNotepad() {
// 创建菜单栏
JMenuBar menuBar = new JMenuBar();
// 创建文件菜单
JMenu fileMenu = new JMenu("文件");
JMenuItem newMenuItem = new JMenuItem("新建");
JMenuItem openMenuItem = new JMenuItem("打开");
JMenuItem saveMenuItem = new JMenuItem("保存");
JMenuItem saveAsMenuItem = new JMenuItem("另存为");
JMenuItem exitMenuItem = new JMenuItem("退出");
newMenuItem.addActionListener(this);
openMenuItem.addActionListener(this);
saveMenuItem.addActionListener(this);
saveAsMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(saveAsMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
// 创建编辑菜单
JMenu editMenu = new JMenu("编辑");
JMenuItem copyMenuItem = new JMenuItem("复制");
JMenuItem cutMenuItem = new JMenuItem("剪切");
JMenuItem pasteMenuItem = new JMenuItem("粘贴");
JMenuItem selectAllMenuItem = new JMenuItem("全选");
copyMenuItem.addActionListener(this);
cutMenuItem.addActionListener(this);
pasteMenuItem.addActionListener(this);
selectAllMenuItem.addActionListener(this);
editMenu.add(copyMenuItem);
editMenu.add(cutMenuItem);
editMenu.add(pasteMenuItem);
editMenu.addSeparator();
editMenu.add(selectAllMenuItem);
// 创建帮助菜单
JMenu helpMenu = new JMenu("帮助");
JMenuItem aboutMenuItem = new JMenuItem("关于");
aboutMenuItem.addActionListener(this);
helpMenu.add(aboutMenuItem);
// 将菜单栏添加到窗口中
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
// 将文本区域添加到滚动面板中
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
getContentPane().add(scrollPane);
// 设置窗口属性
setTitle("简易记事本");
setSize(600, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
if (action.equals("新建")) {
newFile();
} else if (action.equals("打开")) {
openFile();
} else if (action.equals("保存")) {
saveFile();
} else if (action.equals("另存为")) {
saveAsFile();
} else if (action.equals("退出")) {
System.exit(0);
} else if (action.equals("复制")) {
textArea.copy();
} else if (action.equals("剪切")) {
textArea.cut();
} else if (action.equals("粘贴")) {
textArea.paste();
} else if (action.equals("全选")) {
textArea.selectAll();
} else if (action.equals("关于")) {
JOptionPane.showMessageDialog(this, "简易记事本", "关于", JOptionPane.PLAIN_MESSAGE);
}
}
private void newFile() {
textArea.setText("");
filename = "";
setTitle("简易记事本");
}
private void openFile() {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
filename = fileChooser.getSelectedFile().getPath();
setTitle(filename);
try {
textArea.setText("");
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(filename));
String line = null;
while ((line = reader.readLine()) != null) {
textArea.append(line + "\n");
}
reader.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
private void saveFile() {
if (filename.equals("")) {
saveAsFile();
} else {
try {
java.io.PrintWriter writer = new java.io.PrintWriter(new java.io.FileWriter(filename));
writer.println(textArea.getText());
writer.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
private void saveAsFile() {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
filename = fileChooser.getSelectedFile().getPath();
setTitle(filename);
try {
java.io.PrintWriter writer = new java.io.PrintWriter(new java.io.FileWriter(filename));
writer.println(textArea.getText());
writer.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new SimpleNotepad();
}
}
```
希望这个简单的记事本界面代码能为您提供参考。
阅读全文