【问题标题】:I can't make my program to wait until GUI has finished gathering requested info (Java)我不能让我的程序等到 GUI 完成收集请求的信息 (Java)
【发布时间】:2016-01-31 11:31:27
【问题描述】:

我对 Java 还是很陌生,在我最终提出这个问题之前,我搜索并搜索了 SO,但我似乎并没有完全理解它。 如您所见,我有一个创建 GUI 的类,请求一些输入并将该输入存储在可返回的 String[] 中。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InputConsole {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static String[] details = new String[3];
public static JButton btn2013;
public static JButton btn2014;
public static JButton btn2015;
public static JButton btn2016;
public static JButton btnGo;
public static JTextField textField2;
public static JTextField textField4;

public static void addComponentsToPane(Container pane) {
    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }

    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    if (shouldFill) {
        c.fill = GridBagConstraints.HORIZONTAL;
    }

    btn2013 = new JButton("ZMR 2013");
    btn2013.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[2] = "2013";
            btn2014.setEnabled(false);
            btn2015.setEnabled(false);
            btn2016.setEnabled(false);
            textField2.requestFocusInWindow();

        }
    });
    if (shouldWeightX) {
        c.weightx = 0.0;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(10, 0, 0, 0);
    c.gridx = 0;
    c.gridy = 0;
    pane.add(btn2013, c);

    btn2014 = new JButton("ZMR 2014");
    btn2014.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[2] = "2014";
            btn2013.setEnabled(false);
            btn2015.setEnabled(false);
            btn2016.setEnabled(false);
            textField2.requestFocusInWindow();
        }
    });
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(10, 10, 0, 0);
    c.weightx = 0.0;
    c.gridx = 1;
    c.gridy = 0;
    pane.add(btn2014, c);

    btn2015 = new JButton("ZMR 2015");
    btn2015.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[2] = "2015";
            btn2013.setEnabled(false);
            btn2014.setEnabled(false);
            btn2016.setEnabled(false);
            textField2.requestFocusInWindow();
        }
    });
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.0;
    c.gridx = 2;
    c.gridy = 0;
    pane.add(btn2015, c);

    btn2016 = new JButton("ZMR 2016");
    btn2016.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[2] = "2016";
            btn2013.setEnabled(false);
            btn2014.setEnabled(false);
            btn2015.setEnabled(false);
            textField2.requestFocusInWindow();
        }
    });
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.0;
    c.gridx = 3;
    c.gridy = 0;
    pane.add(btn2016, c);

    JLabel textField1 = new JLabel("What was your Bib number? : ");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 2;
    pane.add(textField1, c);

    textField2 = new JTextField(10);
    textField2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[0] = textField2.getText();
            textField4.requestFocusInWindow();

        }

    });

    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 2;
    c.gridy = 2;
    pane.add(textField2, c);

    JLabel textField3 = new JLabel("What is your email address : ");

    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 3;
    c.gridwidth = 2;
    pane.add(textField3, c);

    textField4 = new JTextField(15);
    textField4.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[1] = textField4.getText();
            btnGo.setEnabled(true);
            btnGo.requestFocusInWindow();

        }
    });
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 2;
    c.gridy = 3;
    pane.add(textField4, c);

    btnGo = new JButton("Go And Get Me My Diploma!");
    btnGo.setEnabled(false);
    btnGo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            JOptionPane.showMessageDialog(null, details[0] + " " + details[1] + " " + details[2]);
        }
    });
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 0;
    c.weighty = 1.0;
    c.anchor = GridBagConstraints.PAGE_END;
    c.insets = new Insets(10, 0, 0, 0);
    c.gridx = 0;
    c.gridwidth = 4;
    c.gridy = 4;
    pane.add(btnGo, c);
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame("Zagori Mountain Running Diploma Maker");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addComponentsToPane(frame.getContentPane());

    frame.pack();
    frame.setSize(500, 250);
    frame.setVisible(true);
}

public String[] inputBib() {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });

    return details;

}

但是当我在另一个类中调用这个 GUI 时

public class CheckFiles {
InputConsole bibInput = new InputConsole();

String[] detailsInput = bibInput.inputBib();
private static Scanner scanner;

public String bibCorrected()  {

    String yearToCheck = null;
    {

        if (detailsInput[2] == "2016") {
            yearToCheck = "ZMR2016.txt";
        } else if (detailsInput[2] == "2015") {
            yearToCheck = "ZMR2015.txt";
        } else if (detailsInput[2] == "2014") {
            yearToCheck = "ZMR2014.txt";
        } else {
            yearToCheck = "ZMR2013.txt";

为了获得那个String[],我得到了一个java.lang.NullPointerException。我知道我得到这个是因为程序不会等待 GUI 获取所有输入并将可返回的 String[] 归档为 null。我想我知道我必须对 wait() 和 notify() 做一些事情,但我似乎不明白到底是什么。 提前感谢您的任何建议。 (对于长线程非常抱歉)

【问题讨论】:

    标签: java multithreading user-interface wait notify


    【解决方案1】:

    您可以在 GUI 上添加一个按钮,该按钮将调用 bibCorrected() 方法。目前您正在显示然后返回,因此数组为空,并且 arg 2 不存在因此抛出 NPE。这可能是解决问题的最简单方法。

    另外,最好使用 String.equals(String) 而不是 ==。阅读这篇 StackOverflow 帖子What is the difference between == vs equals() in Java?

    【讨论】:

    • 那么,您的建议是我跳过整个 CheckFiles{} 类并在 GUI 内完成所有工作?
    • 您可以让 CheckFiles 类只调用该方法并将数组作为参数。
    • 很抱歉再次打扰您,但正如我告诉您的那样,我是 Java 架构的新手。我已经尝试了几个小时来弄清楚您正在谈论的那个按钮的放置位置以及如何从那里调用 bibCorrected() 。我了解我正在显示 GUI 然后返回但我不知道如何继续的事实。请您详细说明您之前的答案。
    • 在你的 GUI 类中创建一个带有 actionListener 的 JButton,它运行 CheckFiles 中的方法。在您的 CheckFiles 类中,将 bibCorrected 方法更改为 public String bibCorrected(String[] detals) { 并使用它。
    • 感谢您为我指明了正确的方向。我没有按照您指定的方式进行操作,但是您观察到我只显示 GUI 并没有返回任何内容,这有助于找到一种方法来完成我的工作。我所做的只是将两个方法添加到 GUI 的侦听器中;最后一个按钮。一个用于检查文件,另一个用于发送包含用户提供的输入的电子邮件。再次感谢您。
    猜你喜欢
    • 1970-01-01
    • 2017-01-04
    • 2015-10-27
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多