无聊的编程课

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, http://yy.china.com.cn);
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();
}
}
}

转载于:https://www.cnblogs.com/yjhkdxr/archive/2012/12/07/2807804.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值