实验要求:
- 点击read按钮,则读入指定txt文档中的内容,显示在第一个文本域(左边文本域中)。
2.点击sort按钮,则将文本域中特点的字符提取出来,放到右边的文本域中。
3.点击save按钮,则将处理过后右边的文本保存到一个新的txt文档中。
请用Java图形界面的知识,编写以上软件。
代码:
-- 按键处理类ButtonHandler:
package com.junlin.exer8;
import com.junlin.exer8.MyFrame;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* @Description:
* @author: junlin623
* @create: 2022-11-11 21:34
*/
class ButtonHandler implements ActionListener {
private MyFrame window; //表示处理的是哪个窗口中的点击事件
public ButtonHandler(JFrame window) {
this.window = (MyFrame) window;
}
@Override
public void actionPerformed(ActionEvent e) {
//表示点击的按钮是哪一个
JButton sourceBtn = (JButton)e.getSource();
FileReader fr = null;
if(sourceBtn == this.window.readBtn) { //读
try {
fr = new FileReader("./resources/" + this.window.filename);
char[] buffer = new char[10];
int len = 0;
while((len = fr.read(buffer)) != -1) {
String s = new String(buffer, 0, len);
this.window.leftArea.append(s);
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}finally {
try {
fr.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}else if(sourceBtn == this.window.sortBtn) { //提取
String str = this.window.leftArea.getText();
String[] words = str.split("\n");
for(String s: words) {
if(s.contains("DBa") || s.contains("RRi")) {
this.window.rightArea.append(s + "\n");
}
}
}else if(sourceBtn == this.window.saveBtn) { //保存
String str = this.window.rightArea.getText();
FileWriter fw = null;
try {
fw = new FileWriter("./resources/result.txt");
fw.write(str);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
}
-- 界面类MyFrame
package com.junlin.exer8;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Description:
* @author: junlin623
* @create: 2022-11-11 21:10
*/
public class MyFrame extends JFrame {
public JButton readBtn; //读按钮
public JButton sortBtn; //提取按钮
public JButton saveBtn; //保存按钮
public JTextArea leftArea; //左侧文本区域
public JTextArea rightArea; //右侧文本区域
public String filename = "data.txt"; //要读取的文件名
public MyFrame() {
super("文件处理");
this.setLayout(new FlowLayout());
//左侧区域
readBtn = new JButton("read");
this.add(readBtn);
JScrollPane leftPane = new JScrollPane();
leftPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
leftArea = new JTextArea(30,30); //这里必须指定行数和列数才可以显示滚动条, 而且不能使用下面这样代码
//leftArea.setPreferredSize(new Dimension(300, 500));
leftArea.setLineWrap(true);
leftPane.setViewportView(leftArea);
this.add(leftPane);
sortBtn = new JButton("sort");
this.add(sortBtn);
//右侧区域
JScrollPane rightPane = new JScrollPane();
rightPane.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
rightArea = new JTextArea(30,30);
rightArea.setLineWrap(true);
rightPane.setViewportView(rightArea);
this.add(rightPane);
saveBtn = new JButton("save");
this.add(saveBtn);
//添加点击事件
readBtn.addActionListener(new ButtonHandler(this));
sortBtn.addActionListener(new ButtonHandler(this));
saveBtn.addActionListener(new ButtonHandler(this));
this.setSize(1000, 700);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
--主类:
package com.junlin.exer8;
/**
* @Description:
* @author: junlin623
* @create: 2022-11-11 21:10
*/
public class JavaMain {
public static void main(String[] args) {
new MyFrame();
}
}