Java简易记事本
时间: 2024-01-30 09:02:41 浏览: 132
好的,下面是一个Java简易记事本的示例代码,包含基本功能如新建、打开、保存、剪切、复制、粘贴、查找、替换、字体和颜色设置等。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class SimpleNotepad extends JFrame {
private JTextArea textArea;
private JFileChooser fileChooser;
private String currentFilename;
public SimpleNotepad() {
setTitle("Simple Notepad");
setSize(400, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
fileChooser = new JFileChooser();
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem newMenuItem = new JMenuItem("New");
newMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newFile();
}
});
fileMenu.add(newMenuItem);
JMenuItem openMenuItem = new JMenuItem("Open");
openMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
});
fileMenu.add(openMenuItem);
JMenuItem saveMenuItem = new JMenuItem("Save");
saveMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
fileMenu.add(saveMenuItem);
JMenuItem saveAsMenuItem = new JMenuItem("Save As...");
saveAsMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFileAs();
}
});
fileMenu.add(saveAsMenuItem);
fileMenu.addSeparator();
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(exitMenuItem);
JMenu editMenu = new JMenu("Edit");
menuBar.add(editMenu);
JMenuItem cutMenuItem = new JMenuItem("Cut");
cutMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.cut();
}
});
editMenu.add(cutMenuItem);
JMenuItem copyMenuItem = new JMenuItem("Copy");
copyMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.copy();
}
});
editMenu.add(copyMenuItem);
JMenuItem pasteMenuItem = new JMenuItem("Paste");
pasteMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea.paste();
}
});
editMenu.add(pasteMenuItem);
editMenu.addSeparator();
JMenuItem findMenuItem = new JMenuItem("Find");
findMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String searchText = JOptionPane.showInputDialog(SimpleNotepad.this, "Find:");
if (searchText != null) {
String text = textArea.getText();
int index = text.indexOf(searchText);
if (index != -1) {
textArea.setSelectionStart(index);
textArea.setSelectionEnd(index + searchText.length());
textArea.requestFocusInWindow();
}
}
}
});
editMenu.add(findMenuItem);
JMenuItem replaceMenuItem = new JMenuItem("Replace");
replaceMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String searchText = JOptionPane.showInputDialog(SimpleNotepad.this, "Find:");
if (searchText != null) {
String replaceText = JOptionPane.showInputDialog(SimpleNotepad.this, "Replace with:");
if (replaceText != null) {
String text = textArea.getText();
String newText = text.replace(searchText, replaceText);
textArea.setText(newText);
}
}
}
});
editMenu.add(replaceMenuItem);
JMenu formatMenu = new JMenu("Format");
menuBar.add(formatMenu);
JMenuItem fontMenuItem = new JMenuItem("Font...");
fontMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Font font = textArea.getFont();
JFontChooser fontChooser = new JFontChooser(SimpleNotepad.this, font);
if (fontChooser.showDialog() == JFontChooser.OK_OPTION) {
Font newFont = fontChooser.getSelectedFont();
textArea.setFont(newFont);
}
}
});
formatMenu.add(fontMenuItem);
JMenuItem colorMenuItem = new JMenuItem("Color...");
colorMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color color = textArea.getForeground();
Color newColor = JColorChooser.showDialog(SimpleNotepad.this, "Choose Color", color);
if (newColor != null) {
textArea.setForeground(newColor);
}
}
});
formatMenu.add(colorMenuItem);
}
private void newFile() {
textArea.setText("");
currentFilename = null;
setTitle("Simple Notepad");
}
private void openFile() {
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedReader reader
阅读全文