📄 bijiao.java
字号:
package 文件比较soft28;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import 对参赛选手的得分进行处理图形界面soft30.Score;
public class Bijiao extends JFrame {
private JTextArea textArea;
static String FilePath1 = "";
static String FilePath2 = "";
public static void main(String[] args) {
Bijiao sd = new Bijiao();
sd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Bijiao() {
super("文件比较");
getContentPane().setLayout(null);
Container cn = this.getContentPane();
this.setSize(600, 500);
this.show();
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(92, 158, 405, 227);
getContentPane().add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
FileDialog fd=new FileDialog(Bijiao.this);//添加浏览文件的面板
fd.setVisible(true);//显示面板
FilePath1=fd.getDirectory()+fd.getFile();
Runtime rt = Runtime.getRuntime();
try {
rt.exec("notepad.exe " + FilePath1);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
});
button.setText("打开文件1");
button.setBounds(92, 39, 99, 23);
getContentPane().add(button);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
FileDialog fd=new FileDialog(Bijiao.this);//添加浏览文件的面板
fd.setVisible(true);//显示面板
FilePath2=fd.getDirectory()+fd.getFile();
Runtime rt = Runtime.getRuntime();
try {
rt.exec("notepad.exe " + FilePath2);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
});
button_1.setText("打开文件2");
button_1.setBounds(238, 39, 99, 23);
getContentPane().add(button_1);
final JButton button_2 = new JButton();
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
Bijiao.this.compl(textArea);
}
});
button_2.setText("开始比较");
button_2.setBounds(398, 39, 99, 23);
getContentPane().add(button_2);
this.setResizable(true);//显示
this.setVisible(true);
}
void compl(JTextArea jt){
jt.setText("");
try{
File f1=new File(FilePath1);
File f2=new File(FilePath2);
if (f1.exists() && f2.exists()){
String s1="";
String s2="";
BufferedReader in1=new BufferedReader(new FileReader(f1));
BufferedReader in2=new BufferedReader(new FileReader(f2));
try{
int i=0;
while( ( (s1=in1.readLine() )!=null )&&( (s2=in2.readLine()) !=null) ){
i++;
if(! s1.equals(s2) ){
jt.append("文件一第"+i+"行:"+s1+"\n");
jt.append("文件二第"+i+"行:"+s2+"\n");
jt.append("有不同。\n");
System.out.println("文件一第"+i+"行:"+s1+"\n"+"文件二第"+i+"行:"+s2+"\n"+"有不同。\n");
/*Confirm kuang=new Confirm(this,"找到不同,是否结束?",true);
if( Confirm.returnValue)
break;
else
continue;*/
}
}
System.out.println("文件读取结束.");
jt.append("文件比较结束。\n");
}catch(IOException exc){
System.out.println("有文件读取错误,无法比较。");
}
}else{
jt.append("有文件不存在,无法比较。\n");
}
}catch(FileNotFoundException e){
System.out.println("有文件不存在,无法比较。");
jt.append("有文件不存在,无法比较。\n");
}
}
}
class Confirm extends Dialog{
static boolean returnValue=false;
public Confirm(Frame f,String s,boolean b){
super(f,s,b);
this.setLayout(new FlowLayout());
JLabel LABEL=new JLabel(s);
JButton NO=new JButton("否");
NO.addActionListener(new NOACT());
JButton YES=new JButton("是");
YES.addActionListener(new YESACT());
this.add(LABEL);
this.add(YES);
this.add(NO);
this.setResizable(true);
this.setSize(300, 80);
this.setLocation(f.getLocation().x, f.getLocation().x);
this.setVisible(true);
this.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Confirm.this.dispose();
}
});
}
class YESACT implements ActionListener {
public void actionPerformed(ActionEvent e) {
returnValue=true;
Confirm.this.dispose();
}
}
class NOACT implements ActionListener {
public void actionPerformed(ActionEvent e) {
returnValue=false;
Confirm.this.dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -