写一个有基本功能的java记事本
时间: 2024-03-05 08:49:06 浏览: 126
好的,以下是一个基本功能的 Java 记事本示例代码,包含新建、打开、保存、另存为、复制、粘贴、剪切、查找、替换、字体设置等基本功能:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;
public class JavaNote extends JFrame implements ActionListener {
private JTextArea textArea = new JTextArea();
private JScrollPane scrollPane = new JScrollPane(textArea);
private JFileChooser fileChooser = new JFileChooser();
private String currentFile = null;
public JavaNote() {
setTitle("JavaNote");
setSize(800, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("文件");
menuBar.add(fileMenu);
JMenuItem newMenuItem = new JMenuItem("新建");
newMenuItem.addActionListener(this);
fileMenu.add(newMenuItem);
JMenuItem openMenuItem = new JMenuItem("打开");
openMenuItem.addActionListener(this);
fileMenu.add(openMenuItem);
JMenuItem saveMenuItem = new JMenuItem("保存");
saveMenuItem.addActionListener(this);
fileMenu.add(saveMenuItem);
JMenuItem saveAsMenuItem = new JMenuItem("另存为");
saveAsMenuItem.addActionListener(this);
fileMenu.add(saveAsMenuItem);
JMenu editMenu = new JMenu("编辑");
menuBar.add(editMenu);
JMenuItem cutMenuItem = new JMenuItem("剪切");
cutMenuItem.addActionListener(this);
editMenu.add(cutMenuItem);
JMenuItem copyMenuItem = new JMenuItem("复制");
copyMenuItem.addActionListener(this);
editMenu.add(copyMenuItem);
JMenuItem pasteMenuItem = new JMenuItem("粘贴");
pasteMenuItem.addActionListener(this);
editMenu.add(pasteMenuItem);
JMenuItem findMenuItem = new JMenuItem("查找");
findMenuItem.addActionListener(this);
editMenu.add(findMenuItem);
JMenuItem replaceMenuItem = new JMenuItem("替换");
replaceMenuItem.addActionListener(this);
editMenu.add(replaceMenuItem);
JMenu formatMenu = new JMenu("格式");
menuBar.add(formatMenu);
JMenuItem fontMenuItem = new JMenuItem("字体设置");
fontMenuItem.addActionListener(this);
formatMenu.add(fontMenuItem);
add(scrollPane, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "新建":
newFile();
break;
case "打开":
openFile();
break;
case "保存":
saveFile();
break;
case "另存为":
saveAsFile();
break;
case "复制":
textArea.copy();
break;
case "粘贴":
textArea.paste();
break;
case "剪切":
textArea.cut();
break;
case "查找":
find();
break;
case "替换":
replace();
break;
case "字体设置":
setFont();
break;
default:
break;
}
}
private void newFile() {
if (textArea.getText().length() > 0) {
int result = JOptionPane.showConfirmDialog(this, "是否保存当前文件?", "提示", JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
saveFile();
} else if (result == JOptionPane.CANCEL_OPTION) {
return;
}
}
textArea.setText("");
currentFile = null;
}
private void openFile() {
if (textArea.getText().length() > 0) {
int result = JOptionPane.showConfirmDialog(this, "是否保存当前文件?", "提示", JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) {
saveFile();
} else if (result == JOptionPane.CANCEL_OPTION) {
return;
}
}
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileFilter(new FileNameExtensionFilter("文本文件(*.txt)", "txt"));
int result = fileChooser.showOpenDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (FileReader reader = new FileReader(file)) {
char[] buffer = new char[1024];
StringBuilder builder = new StringBuilder();
int length;
while ((length = reader.read(buffer)) != -1) {
builder.append(buffer, 0, length);
}
textArea.setText(builder.toString());
currentFile = file.getAbsolutePath();
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "文件打开失败!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFile() {
if (currentFile == null) {
saveAsFile();
} else {
try (FileWriter writer = new FileWriter(currentFile)) {
writer.write(textArea.getText());
JOptionPane.showMessageDialog(this, "文件保存成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "文件保存失败!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveAsFile() {
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileFilter(new FileNameExtensionFilter("文本文件(*.txt)", "txt"));
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (FileWriter writer = new FileWriter(file)) {
writer.write(textArea.getText());
currentFile = file.getAbsolutePath();
JOptionPane.showMessageDialog(this, "文件保存成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "文件保存失败!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
private void find() {
String text = JOptionPane.showInputDialog(this, "请输入要查找的内容:", "查找", JOptionPane.PLAIN_MESSAGE);
if (text != null && text.length() > 0) {
String content = textArea.getText();
int index = content.indexOf(text);
if (index != -1) {
textArea.setCaretPosition(index);
textArea.select(index, index + text.length());
} else {
JOptionPane.showMessageDialog(this, "未找到指定内容!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
private void replace() {
String findText = JOptionPane.showInputDialog(this, "请输入要查找的内容:", "查找", JOptionPane.PLAIN_MESSAGE);
if (findText != null && findText.length() > 0) {
String content = textArea.getText();
int index = content.indexOf(findText);
if (index != -1) {
String replaceText = JOptionPane.showInputDialog(this, "请输入要替换的内容:", "替换", JOptionPane.PLAIN_MESSAGE);
if (replaceText != null) {
content = content.substring(0, index) + replaceText + content.substring(index + findText.length());
textArea.setText(content);
textArea.setCaretPosition(index);
textArea.select(index, index + replaceText.length());
}
} else {
JOptionPane.showMessageDialog(this, "未找到指定内容!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
private void setFont() {
Font font = textArea.getFont();
String fontName = JOptionPane.showInputDialog(this, "请输入字体名称:", font.getName());
if (fontName != null && fontName.length() > 0) {
int fontStyle = font.getStyle();
String fontStyleStr = JOptionPane.showInputDialog(this, "请输入字体样式(0:常规 1:粗体 2:斜体 3:粗斜体):", fontStyle);
if (fontStyleStr != null && fontStyleStr.matches("[0-3]")) {
fontStyle = Integer.parseInt(fontStyleStr);
int fontSize = font.getSize();
String fontSizeStr = JOptionPane.showInputDialog(this, "请输入字体大小:", fontSize);
if (fontSizeStr != null && fontSizeStr.matches("\\d+")) {
fontSize = Integer.parseInt(fontSizeStr);
Font newFont = new Font(fontName, fontStyle, fontSize);
textArea.setFont(newFont);
}
}
}
}
public static void main(String[] args) {
JavaNote note = new JavaNote();
note.setVisible(true);
}
}
```
这个记事本支持打开、保存和另存为文件,可以进行复制、粘贴和剪切操作,还支持查找和替换文本内容,可以设置字体。
阅读全文