package com.swing;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.swing.*;
public class SwingFC implements ActionListener{
public static void main(String[] args) {
// TODO Auto-generated method stub
new SwingFC();
}
JTextField jtf1;
JTextField jtf2;
JCheckBox jcb;
JLabel jl3;
JButton jb;
JLabel jl1;
JLabel jl2;
SwingFC(){
JFrame jframe=new JFrame();
jframe.setLayout(new FlowLayout());
jl1=new JLabel("第一个文件名");
jl2=new JLabel("第二个文件名");
jtf1=new JTextField(20);
jtf2=new JTextField(20);
jcb=new JCheckBox("是否显示不同的位置");
jb=new JButton("比较");
jb.addActionListener(this);
jl3=new JLabel("不同的位置:");
jframe.add(jl1);
jframe.add(jtf1);
jframe.add(jl2);
jframe.add(jtf2);
jframe.add(jcb);
jframe.add(jb);
jframe.add(jl3);
jframe.setSize(300,200);
jframe.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
int i;
int j;
int count=0;
FileInputStream fi1=new FileInputStream(jtf1.getText());
FileInputStream fi2=new FileInputStream(jtf2.getText());
do {
i = fi1.read();
j = fi2.read();
count++;
if(i!=j) {
break;
}
}while(i!=-1&&j!=-1);
if(i!=j) {
if(jcb.isSelected()) {
jl3.setText("不同的位置:"+count);
}
else {
jl3.setText("不匹配");
}
}
else {
jl3.setText("匹配");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
Swing之文本比较
最新推荐文章于 2023-06-06 14:26:00 发布