import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class $ {
public static void main(String... _) {
new MyFrame();
}
}
class MyFrame extends JFrame implements ActionListener {
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JTextField text1;
private JTextField text2;
private JTextField text3;
private JTextArea area;
public MyFrame() {
setTitle("test");
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
setVisible(true);
init();
}
private void init() {
button1 = new JButton("打开第一个文件");
button2 = new JButton("打开第二个文件");
button3 = new JButton("打开第三个文件");
button4 = new JButton("合并");
button5 = new JButton("展示");
button1.setBounds(50, 20, 200, 20);
button2.setBounds(50, 50, 200, 20);
button3.setBounds(50, 80, 200, 20);
button4.setBounds(50, 110, 200, 20);
button5.setBounds(50, 140, 200, 20);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
text1 = new JTextField();
text1.setBounds(300, 20, 300, 20);
text2 = new JTextField();
text2.setBounds(300, 50, 300, 20);
text3 = new JTextField();
text3.setBounds(300, 80, 300, 20);
add(text1);
add(text2);
add(text3);
area = new JTextArea();
area.setBounds(50, 180, 600, 200);
add(area);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
JFileChooser f = new JFileChooser();
f.showDialog(this, "文件一");
text1.setText(f.getSelectedFile().getPath());
}
if (e.getSource() == button2) {
JFileChooser f = new JFileChooser();
f.showDialog(this, "文件二");
text2.setText(f.getSelectedFile().getPath());
}
if (e.getSource() == button3) {
JFileChooser f = new JFileChooser();
f.showDialog(this, "文件三");
text3.setText(f.getSelectedFile().getPath());
}
if (e.getSource() == button4) {
File f1 = new File(text1.getText());
File f2 = new File(text2.getText());
String result = "";
result = getData(result, text1.getText());
result = getData(result, text2.getText());
saveData(result, text3.getText());
}
if (e.getSource() == button5) {
area.setText(getData("", text3.getText()));
}
}
private String getData(String result, String path) {
try {
Scanner in = new Scanner(new File(path));
while (in.hasNextLine()) {
result += in.nextLine() + "\r\n";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return result;
}
private void saveData(String result, String path) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path)));
bw.write(result);
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
|