为什么我的代码AddQnA qa = new AddQnA();在最后一段不能在DisplayQnA上运行?

我正在创建一个GUI,似乎无法创建调用方来在AddQnA类内调用arraylist,因此想知道是否有人知道吗?

public static class AddQnA extends JDialog {

    JLabel label, label2, label3, label4;
    JTextField question, answer;
    JButton input, reset;
    JTextArea textarea;

    ArrayList<String> ques = new ArrayList<>();
    ArrayList<String> ans = new ArrayList<>();

    public AddQnA(JFrame frame) {
        super(frame, "Add Question & Answer", true);
        setLayout(new FlowLayout());

        label = new JLabel("Question :");
        add(label);

        question = new JTextField(60);
        add(question);

        label2 = new JLabel("Answer   : ");
        add(label2);

        answer = new JTextField(60);
        add(answer);

        input = new JButton("Submit");
        add(input);

        reset = new JButton("Reset");
        add(reset);

        label3 = new JLabel("Please use the \"x\" on the top right to exit
        this section");
        add(label3);

        label4 = new JLabel("");
        add(label4);

        textarea = new JTextArea(10, 25);
        add(textarea);
        textarea.setEditable(false);

        Reset a = new Reset();
        reset.addActionListener(a);

        Submit b = new Submit();
        answer.addKeyListener(b);

        Submit c = new Submit();
        input.addActionListener(c);
    }
 }

  public static class DisplayQnA extends JDialog {

    JLabel label;
    JTextArea textarea;
    AddQnA qa = new AddQnA();

    public DisplayQnA(JFrame frame) {
        super(frame, "Display Question & Answer", true);
        setLayout(new FlowLayout());

        label = new JLabel("Displaying All Questions And Asnwers");
        add(label);

        textarea = new JTextArea(10, 25);
        add(textarea);
        textarea.setEditable(false);

        for (int i = 0; i < qa.ques.size(); i++) {
            if (qa.ques.get(i) != null) {
                System.out.println("Question: " + qa.ques.get(i));
                System.out.println("Answer: " + qa.ans.get(i) + "\n");
            } else {
                System.out.println("There are no newly added questions or
                answers");
            }
        }

    }

最佳答案

AddQnA具有1个带有参数JFrame的构造函数。因此您必须发送JFrame对象。

用JFrame对象调用它。在您的代码中:

public static class DisplayQnA extends JDialog {

 JLabel label;
 JTextArea textarea;
 AddQnA qa;

 public DisplayQnA(JFrame frame) {

    super(frame, "Display Question & Answer", true);
    qa = new AddQnA(frame)

10-07 18:57