java记事本运用Java知识设计并实现记事本,实现新建文件、打开文件、保存、另存为、复制、粘贴、字体设置等功能。
时间: 2023-07-10 21:14:10 浏览: 113
好的,以下是一个简单的Java记事本的实现:
```java
import java.awt.BorderLayout;
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 javax.swing.JButton;
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.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
public class JavaNotepad implements ActionListener {
private JFrame frame;
private JTextArea textArea;
private JScrollPane scrollPane;
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenu editMenu;
private JMenu formatMenu;
private JMenuItem newMenuItem;
private JMenuItem openMenuItem;
private JMenuItem saveMenuItem;
private JMenuItem saveAsMenuItem;
private JMenuItem exitMenuItem;
private JMenuItem cutMenuItem;
private JMenuItem copyMenuItem;
private JMenuItem pasteMenuItem;
private JMenuItem selectAllMenuItem;
private JMenuItem fontMenuItem;
private JFileChooser fileChooser;
public JavaNotepad() {
frame = new JFrame("Java Notepad");
textArea = new JTextArea();
scrollPane = new JScrollPane(textArea);
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
editMenu = new JMenu("Edit");
formatMenu = new JMenu("Format");
newMenuItem = new JMenuItem("New");
openMenuItem = new JMenuItem("Open");
saveMenuItem = new JMenuItem("Save");
saveAsMenuItem = new JMenuItem("Save As");
exitMenuItem = new JMenuItem("Exit");
cutMenuItem = new JMenuItem("Cut");
copyMenuItem = new JMenuItem("Copy");
pasteMenuItem = new JMenuItem("Paste");
selectAllMenuItem = new JMenuItem("Select All");
fontMenuItem = new JMenuItem("Font");
fileChooser = new JFileChooser();
// 设置快捷键
newMenuItem.setAccelerator(KeyStroke.getKeyStroke("control N"));
openMenuItem.setAccelerator(KeyStroke.getKeyStroke("control O"));
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke("control S"));
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke("control X"));
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke("control C"));
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke("control V"));
selectAllMenuItem.setAccelerator(KeyStroke.getKeyStroke("control A"));
fontMenuItem.setAccelerator(KeyStroke.getKeyStroke("control F"));
// 添加菜单项
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.addSeparator();
fileMenu.add(saveMenuItem);
fileMenu.add(saveAsMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
editMenu.add(cutMenuItem);
editMenu.add(copyMenuItem);
editMenu.add(pasteMenuItem);
editMenu.addSeparator();
editMenu.add(selectAllMenuItem);
formatMenu.add(fontMenuItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(formatMenu);
frame.setJMenuBar(menuBar);
// 添加事件监听器
newMenuItem.addActionListener(this);
openMenuItem.addActionListener(this);
saveMenuItem.addActionListener(this);
saveAsMenuItem.addActionListener(this);
exitMenuItem.addActionListener(this);
cutMenuItem.addActionListener(this);
copyMenuItem.addActionListener(this);
pasteMenuItem.addActionListener(this);
selectAllMenuItem.addActionListener(this);
fontMenuItem.addActionListener(this);
// 设置文本区域字体
textArea.setFont(new Font("宋体", Font.PLAIN, 16));
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
// 设置界面样式
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
new JavaNotepad();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newMenuItem) {
// 新建文件
textArea.setText("");
frame.setTitle("Java Notepad");
} else if (e.getSource() == openMenuItem) {
// 打开文件
FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.txt)", "txt");
fileChooser.setFileFilter(filter);
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
textArea.setText(sb.toString());
frame.setTitle(file.getName() + " - Java Notepad");
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "打开文件失败!");
}
}
} else if (e.getSource() == saveMenuItem) {
// 保存文件
File file = fileChooser.getSelectedFile();
if (file != null) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText());
writer.close();
frame.setTitle(file.getName() + " - Java Notepad");
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "保存文件失败!");
}
} else {
saveAs();
}
} else if (e.getSource() == saveAsMenuItem) {
// 另存为
saveAs();
} 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();
} else if (e.getSource() == selectAllMenuItem) {
// 全选
textArea.selectAll();
} else if (e.getSource() == fontMenuItem) {
// 字体设置
Font font = textArea.getFont();
Font newFont = JFontChooser.showDialog(frame, "选择字体", font);
if (newFont != null) {
textArea.setFont(newFont);
}
}
}
private void saveAs() {
FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.txt)", "txt");
fileChooser.setFileFilter(filter);
int result = fileChooser.showSaveDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
if (!file.getName().endsWith(".txt")) {
file = new File(file.getParentFile(), file.getName() + ".txt");
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(textArea.getText());
writer.close();
frame.setTitle(file.getName() + " - Java Notepad");
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "保存文件失败!");
}
}
}
}
```
这个记事本程序实现了新建文件、打开文件、保存、另存为、复制、粘贴、字体设置等功能。其中,JFileChooser组件用于选择文件路径,File类用于读写文件,JTextArea组件用于显示文本内容,JFontChooser组件用于选择字体。同时,使用了Swing库中的JMenuBar、JMenu、JMenuItem等组件来实现菜单栏和菜单项的添加和事件处理。
阅读全文