一个简单的使用Java开发的本地化存储记事本应用程序的源代码
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
public class Notepad extends JFrame {
private JTextArea textArea;
private JFileChooser fileChooser;
private File file;
public Notepad() {
怎么用java编写app软件 initUI();
}
private void initUI() {
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane);
fileChooser = new JFileChooser();
setTitle("记事本");
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
private void saveFile() throws IOException {
if (file == null) {
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = SelectedFile();
saveToFile(file);
}
} else {
saveToFile(file);
}
}
private void saveToFile(File file) throws IOException {
FileWriter writer = new FileWriter(file);
PrintWriter printWriter = new PrintWriter(writer);
String data = Text();
printWriter.print(data);
printWriter.close();
}
private void openFile() throws IOException {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = SelectedFile();
String data = AbsolutePath());
textArea.setText(data);
}
}
private String readFile(String fileName) throws IOException {
String data = "";
data = new (fileName)), StandardCharsets.UTF_8);
return data;
}
public static void main(String[] args) {
Notepad notepad = new Notepad();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new Property("user.home")));
Notepad notepadObject = new Notepad();
int returnVal = fileChooser.showOpenDialog(notepadObject);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = SelectedFile();
try {
String data = AbsolutePath());
Area.setText(data);
} catch (IOException e) {
JOptionPane.showMessageDialog(notepadObject, "打开文件失败!", "打开", JOptionPane.ERROR_MESSAGE);
}
}
}
}
这个记事本应用程序有两个主要功能:保存文件和打开文件。保存文件时,应用程序将会提示用户选择一个文件名,然后将记事本中的文本保存为一个包含这个文件名的文件。打开文件时,应用程序会将文件路径传递给 readFile 方法,然后将文件中的文本显示在记事本中。
以下是一些常见的功能的实现代码:
1.查和替换功能:
// 声明查和替换的文本框
private JTextField searchField = new JTextField();
private JTextField replaceField = new JTextField();
// 添加查和替换菜单项
findItem = new JMenuItem("查");
findItem.KeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
findItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
veAll();
searchPanel.add(new JLabel("查:"));
searchPanel.add(searchField);
searchPanel.add(new JLabel("替换成:"));
searchPanel.add(replaceField);
JOptionPane.showMessageDialog(null, searchPanel, "查和替换", JOptionPane.PLAIN_MESSAGE);
String search = Text();
String replace = Text();
String text = Text();
String replacedText = placeAll(search, replace);
textArea.setText(replacedText);
}
});
editMenu.add(findItem);
2.撤销和重做功能:
// 添加撤销和重做菜单项
undoItem = new JMenuItem("撤销");
undoItem.KeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
undoItem.addActionListener(new ActionListener() {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论