请编写一个Java程序实现如下图所示的“我的记事本”窗口(TextEditor),窗口 1 中包括一个菜单栏(JMenuBar),其中有三个菜单(JMennu),分别是文件菜单、编辑 菜单和查看菜单,窗口的主体部分是一个可编辑区域,用到了多行多列的文本框 (JTextArea)和滚动条面板(JScrollPane):
时间: 2024-02-01 17:13:16 浏览: 161
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
public class TextEditor extends JFrame {
private JTextArea textArea;
private JFileChooser fileChooser;
private String currentFile = "";
public TextEditor() {
initUI();
}
private void initUI() {
setTitle("我的记事本");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
textArea = new JTextArea();
textArea.setFont(new Font("微软雅黑", Font.PLAIN, 16));
JScrollPane scrollPane = new JScrollPane(textArea);
getContentPane().add(scrollPane, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("文件");
JMenuItem newItem = new JMenuItem("新建");
JMenuItem openItem = new JMenuItem("打开");
JMenuItem saveItem = new JMenuItem("保存");
JMenuItem saveAsItem = new JMenuItem("另存为");
JMenuItem exitItem = new JMenuItem("退出");
newItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newFile();
}
});
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openFile();
}
});
saveItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
saveAsItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveFileAs();
}
});
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.add(exitItem);
JMenu editMenu = new JMenu("编辑");
JMenuItem cutItem = new JMenuItem("剪切");
JMenuItem copyItem = new JMenuItem("复制");
JMenuItem pasteItem = new JMenuItem("粘贴");
JMenuItem selectAllItem = new JMenuItem("全选");
cutItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.cut();
}
});
copyItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.copy();
}
});
pasteItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.paste();
}
});
selectAllItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.selectAll();
}
});
editMenu.add(cutItem);
editMenu.add(copyItem);
editMenu.add(pasteItem);
editMenu.add(selectAllItem);
JMenu viewMenu = new JMenu("查看");
JMenuItem zoomInItem = new JMenuItem("放大");
JMenuItem zoomOutItem = new JMenuItem("缩小");
zoomInItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Font font = textArea.getFont();
textArea.setFont(new Font(font.getName(), font.getStyle(), font.getSize() + 1));
}
});
zoomOutItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Font font = textArea.getFont();
textArea.setFont(new Font(font.getName(), font.getStyle(), font.getSize() - 1));
}
});
viewMenu.add(zoomInItem);
viewMenu.add(zoomOutItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(viewMenu);
setJMenuBar(menuBar);
}
private void newFile() {
currentFile = "";
textArea.setText("");
}
private void openFile() {
if (fileChooser == null) {
fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt");
fileChooser.setFileFilter(filter);
}
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
currentFile = file.getAbsolutePath();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
StringBuilder sb = new StringBuilder();
while (line != null) {
sb.append(line).append("\n");
line = reader.readLine();
}
reader.close();
textArea.setText(sb.toString());
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "打开文件失败", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFile() {
if (currentFile.isEmpty()) {
saveFileAs();
} else {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(currentFile));
writer.write(textArea.getText());
writer.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "保存文件失败", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFileAs() {
if (fileChooser == null) {
fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt");
fileChooser.setFileFilter(filter);
}
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
currentFile = file.getAbsolutePath();
if (!currentFile.endsWith(".txt")) {
currentFile += ".txt";
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(currentFile));
writer.write(textArea.getText());
writer.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "保存文件失败", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
try {
// 设置LookAndFeel为系统默认
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TextEditor editor = new TextEditor();
editor.setVisible(true);
}
});
}
}
```
阅读全文