用Java图形界面的知识,编写以下软件:点击read按钮,则读入指定txt文档中的内容,显示在左边文本域中,点击sort按钮,则将文本域中以“DBa”开头的字符串提取出来,放到右边的文本域中,点击save按钮,则将处理过后右边的文本保存到一个新的txt文档中。该代码需要注释
时间: 2024-03-01 07:55:00 浏览: 87
好的,以下是一个示例的Java图形界面程序,可以实现您所描述的功能。代码中有详细的注释,您可以根据自己的需求进行修改。
```java
import java.awt.EventQueue;
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.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
public class TxtFileSorter extends JFrame {
private JPanel contentPane;
private JTextArea textAreaLeft;
private JTextArea textAreaRight;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TxtFileSorter frame = new TxtFileSorter();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TxtFileSorter() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 640, 480);
setTitle("Txt File Sorter");
// 创建主面板
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// 创建左边的文本域和滚动条
JScrollPane scrollPaneLeft = new JScrollPane();
scrollPaneLeft.setBounds(10, 42, 300, 380);
contentPane.add(scrollPaneLeft);
textAreaLeft = new JTextArea();
scrollPaneLeft.setViewportView(textAreaLeft);
// 创建右边的文本域和滚动条
JScrollPane scrollPaneRight = new JScrollPane();
scrollPaneRight.setBounds(320, 42, 300, 380);
contentPane.add(scrollPaneRight);
textAreaRight = new JTextArea();
scrollPaneRight.setViewportView(textAreaRight);
// 创建read按钮
JButton btnRead = new JButton("read");
btnRead.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 选择txt文件
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("."));
int result = fileChooser.showOpenDialog(TxtFileSorter.this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
// 读取txt文件
try (BufferedReader reader = new BufferedReader(new FileReader(selectedFile))) {
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append(System.lineSeparator());
}
textAreaLeft.setText(sb.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
btnRead.setBounds(10, 10, 80, 22);
contentPane.add(btnRead);
// 创建sort按钮
JButton btnSort = new JButton("sort");
btnSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 获取左边文本域的内容
String text = textAreaLeft.getText();
// 将以"DBa"开头的字符串提取出来
StringBuilder sb = new StringBuilder();
String[] lines = text.split(System.lineSeparator());
for (String line : lines) {
if (line.startsWith("DBa")) {
sb.append(line);
sb.append(System.lineSeparator());
}
}
// 将结果显示在右边的文本域中
textAreaRight.setText(sb.toString());
}
});
btnSort.setBounds(100, 10, 80, 22);
contentPane.add(btnSort);
// 创建save按钮
JButton btnSave = new JButton("save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 选择保存的文件路径
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("."));
int result = fileChooser.showSaveDialog(TxtFileSorter.this);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
// 将右边文本域的内容保存到文件中
try (BufferedWriter writer = new BufferedWriter(new FileWriter(selectedFile))) {
writer.write(textAreaRight.getText());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
btnSave.setBounds(190, 10, 80, 22);
contentPane.add(btnSave);
// 创建左边的文本域标签
JLabel lblLeft = new JLabel("Left:");
lblLeft.setBounds(10, 25, 80, 15);
contentPane.add(lblLeft);
// 创建右边的文本域标签
JLabel lblRight = new JLabel("Right:");
lblRight.setBounds(320, 25, 80, 15);
contentPane.add(lblRight);
}
}
```
阅读全文