java gui if else与arraylist无关

时间:2017-10-01 12:37:40

标签: java swing

我的java GUI代码从file.txt添加到arraylist我在一个按钮中有一个代码,用于比较文本字段中引入的代码和araylist中的位置以及if else。但是输入你输入的内容总是通过if而不是通过if。

如果我在Arraylist ArrayList <String> answer = new ArrayList <String> ();中手动添加组件 然后,如果其他工作。

但似乎我将文本字段的文本与文本字段进行比较  ArrayList <String> answer1 = new ArrayList <String> ();,if不起作用不能很好地进行比较

问题是我需要询问用户266个问题用户必须输入答案,如果答案不正确,应该会出现错误的答复消息 但我无法得到if else与一个装有file.text的arraylist一起使用这里是我的代码。

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

public class question_answer extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JButton btnAnswer;
    private JLabel lblNewLabelcuestion;
    private JLabel LabelTextF;
    private JLabel labelArraylistPos;
    private JLabel lblNewLabelTF;
    private JLabel lblNewLabelAL;
    static int count = 0;

    private JLabel lblNewLabel_1;
    private static JTextArea textArea;
    private JLabel lblNewLabel;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    question_answer frame = new question_answer();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });

    }

    /**
     * Create the frame.
     */
    public question_answer() {
        File archive = null;
        FileReader fr = null;

        //This works
        /*
         * ArrayList<String> question = new ArrayList<String>();
         * question.add("What is your name?");
         * question.add("What is your surname?");
         * question.add("What is your age?");
         * ArrayList<String> answer = new ArrayList<String>();
         * answer.add("Pedro"); 
         * answer.add("Rodriguez");
         * answer.add("46");
         */

        ArrayList<String> answer1 = new ArrayList<String>();

        try {
            archive = new File("C:\\answer.txt");
            String line;
            fr = new FileReader(archive);
            BufferedReader br = new BufferedReader(fr);

            while ((line = br.readLine()) != null) {
                answer1.add(line);
            }

            br.close();
        } catch (IOException e) {
            System.out.println(e);
        }

        finally {
            try {
                if (fr != null) {
                    fr.close();

                }
            } catch (IOException e) {
            }
        }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 567, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        lblNewLabelcuestion = new JLabel("New label");
        lblNewLabelcuestion.setBounds(60, 56, 360, 24);
        contentPane.add(lblNewLabelcuestion);

        lblNewLabelTF = new JLabel("New label");
        lblNewLabelTF.setBounds(60, 292, 56, 16);
        contentPane.add(lblNewLabelTF);

        LabelTextF = new JLabel("TextField");
        LabelTextF.setBounds(60, 263, 56, 16);
        contentPane.add(LabelTextF);

        lblNewLabelAL = new JLabel("New label");
        lblNewLabelAL.setBounds(170, 292, 56, 16);
        contentPane.add(lblNewLabelAL);

        labelArraylistPos = new JLabel("Arraylist Position");
        labelArraylistPos.setBounds(170, 263, 110, 16);
        contentPane.add(labelArraylistPos);

        textField = new JTextField();
        textField.setBounds(340, 139, 116, 22);
        contentPane.add(textField);
        textField.setColumns(10);
        lblNewLabelcuestion.setText(answer1.get(count));
        btnAnswer = new JButton("answer");

        lblNewLabelAL.setText(answer1.get(count));

        btnAnswer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                String line1;
                for (int j = 0; j < answer1.size(); j++) {
                    line1 = answer1.get(j);
                    textArea.append(line1);
                }

                lblNewLabelcuestion.setText(answer1.get(count));
                lblNewLabelAL.setText(answer1.get(count));
                lblNewLabelTF.setText(textField.getText());

                String c = answer1.get(count);
                String Tf = textField.getText();

                if (Tf.equals(c)) {
                    lblNewLabel_1.setText("Good");
                } else {
                    lblNewLabel_1.setText("Wrong");
                }
                count += 1;
            }
        });
        btnAnswer.setBounds(340, 198, 97, 25);
        contentPane.add(btnAnswer);

        lblNewLabel_1 = new JLabel("New label");
        lblNewLabel_1.setBounds(45, 153, 256, 16);
        contentPane.add(lblNewLabel_1);

        textArea = new JTextArea();
        textArea.setBounds(345, 273, 181, 67);
        contentPane.add(textArea);

        lblNewLabel = new JLabel("achive.text  content");
        lblNewLabel.setBounds(340, 244, 116, 16);
        contentPane.add(lblNewLabel);
    }

}
`

2 个答案:

答案 0 :(得分:0)

在任何文本编辑器中,当您按Enter键时,编辑器会进入所谓的换行符。这个字符是不可见的,但它告诉编辑器在哪里创建一个新行。您需要从行中删除这些字符。好消息是你只需添加一行代码就可以实现这一目标!你在哪里:

while ((line = br.readLine()) != null) {
    answer1.add(line);
}

将其替换为:

while ((line = br.readLine()) != null) {
    line = line.replace(System.getProperty("line.separator"), “”);
    answer1.add(line);
}

上面的代码将用空字符串替换OS定义的任何行分隔符,从而使得比较的字符串相等。

答案 1 :(得分:0)

解决方案是String c = answer1.get(count).trim();