Java:Swing - 比较2个文件的JCheckBox

时间:2013-01-29 11:17:23

标签: java swing compare jcheckbox

如果CheckBox为true,我试图这样做,以显示两个文件开始不同的位置。

代码:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;

public class SwingFC implements ActionListener {
    JTextField jtfFirst, jtfSecond;
    JLabel jlabFirst, jlabSecond, jlabResult;
    JButton jbtnComp;
    JCheckBox checkBox;

    SwingFC() {
        JFrame jfrm = new JFrame("Compare Files");
        jfrm.setLayout(new FlowLayout());
        jfrm.setSize(300, 220);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jtfFirst = new JTextField(25);
        jtfSecond = new JTextField(25);
        jtfFirst.setActionCommand("fileA");
        jtfSecond.setActionCommand("fileB");

        JButton jbtnComp = new JButton("Compare");
        jbtnComp.addActionListener(this);

        jlabFirst = new JLabel("First file: ");
        jlabSecond = new JLabel("Second file: ");
        jlabResult = new JLabel("");

        checkBox = new JCheckBox("Show position of mismatch");

        jfrm.add(jlabFirst);
        jfrm.add(jtfFirst);
        jfrm.add(jlabSecond);
        jfrm.add(jtfSecond);
        jfrm.add(checkBox);
        jfrm.add(jbtnComp);
        jfrm.add(jlabResult);

        jfrm.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        int i, j = 0;

        if (jtfFirst.getText().equals("")) {
            jlabResult.setText("First file name missing.");
            return;
        }
        if (jtfSecond.getText().equals("")) {
            jlabResult.setText("Second file name missing.");
            return;
        }

        try (FileInputStream f1 = new FileInputStream(jtfFirst.getText());
                FileInputStream f2 = new FileInputStream(jtfSecond.getText())) {
            String count = null;

            do {
                i = f1.read();
                j = f2.read();
                if (i != j)
                    break;
            } while (i != -1 && j != -1);

            if (i != j)
                if (checkBox.isSelected())
                    jlabResult.setText("Files differ at location" + count);
                else
                    jlabResult.setText("Files are not the same");
            else
                jlabResult.setText("Files compare equal.");
        } catch (IOException e) {
            jlabResult.setText("File Error");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingFC();
            }
        });
    }
}

问题:

  • 我想知道怎样才能分配这个不同的String count并显示 在

    jlabResult.setText(“文件位置不同”+计数);

我该如何解决? 问候。

1 个答案:

答案 0 :(得分:0)

添加变量以存储位置:

int position = 0;

然后在i == j

时递增它

并设置文字jlabResult.setText("Files differ at location " + position);

您不需要变量String count

整个代码是:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;

public class SwingFC implements ActionListener {
    JTextField jtfFirst, jtfSecond;
    JLabel jlabFirst, jlabSecond, jlabResult;
    JButton jbtnComp;
    JCheckBox checkBox;

    SwingFC() {
        JFrame jfrm = new JFrame("Compare Files");
        jfrm.setLayout(new FlowLayout());
        jfrm.setSize(300, 220);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jtfFirst = new JTextField(25);
        jtfSecond = new JTextField(25);
        jtfFirst.setActionCommand("fileA");
        jtfSecond.setActionCommand("fileB");

        JButton jbtnComp = new JButton("Compare");
        jbtnComp.addActionListener(this);

        jlabFirst = new JLabel("First file: ");
        jlabSecond = new JLabel("Second file: ");
        jlabResult = new JLabel("");

        checkBox = new JCheckBox("Show position of mismatch");

        jfrm.add(jlabFirst);
        jfrm.add(jtfFirst);
        jfrm.add(jlabSecond);
        jfrm.add(jtfSecond);
        jfrm.add(checkBox);
        jfrm.add(jbtnComp);
        jfrm.add(jlabResult);

        jfrm.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        int i, j = 0;

        if (jtfFirst.getText().equals("")) {
            jlabResult.setText("First file name missing.");
            return;
        }
        if (jtfSecond.getText().equals("")) {
            jlabResult.setText("Second file name missing.");
            return;
        }

        try (FileInputStream f1 = new FileInputStream(jtfFirst.getText());
             FileInputStream f2 = new FileInputStream(jtfSecond.getText())) {
            //String count = null;

            int position = 0;

            do {
                i = f1.read();
                j = f2.read();
                position++;
                if (i != j)
                    break;
            } while (i != -1 && j != -1);

            if (i != j)
                if (checkBox.isSelected())
                    jlabResult.setText("Files differ at location " + position);
                else
                    jlabResult.setText("Files are not the same");
            else
                jlabResult.setText("Files compare equal.");
        } catch (IOException e) {
            jlabResult.setText("File Error");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SwingFC();
            }
        });
    }
}

它对我有用。 我希望这有用。