如何等待从GUI返回主界面的输入?

时间:2019-05-30 09:50:34

标签: java swing jframe

我希望能够将用户输入从我的GUI传递到我的一个班级。但是,输入不会传递,并立即检查if语句。 如何让程序等待输入并仅在单击按钮后检查?

主班

public class MainTest {
    public static void main(String[] args) {
        String weaponCategory;
        //Create Java GUI
        GUITest window = new GUITest();

        if(window.getCategory() != "")
        {
            System.out.println("test");
        }
    }

}

GUITest类

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

public class GUITest implements ActionListener{

    private JFrame frmInventorysystem;
    private JPanel frameBottom;
    private JComboBox equipList;
    private String category = "";
    private JButton confirmBtn, cancelBtn;

    /**
     * Create the application.
     */
    public GUITest() 
    {       
        frmInventorysystem = new JFrame();
        frmInventorysystem.setTitle("InventorySystem");
        frmInventorysystem.setBounds(100, 100, 450, 300);
        frmInventorysystem.getContentPane().setLayout(new BorderLayout(0, 0));

        frmInventorysystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /*JFrame inside another JFrame is not recommended. JPanels are used instead.
        * Creating a flow layout for the bottom frame
        */
        frameBottom = new JPanel();
        frameBottom.setLayout(new FlowLayout());

        //creates comboBox to find out which of the three items player is looking to insert
        String[] weaponCategories = {"Weapon", "Armor", "Mod"};
        equipList = new JComboBox(weaponCategories);
        frmInventorysystem.getContentPane().add(equipList, BorderLayout.NORTH);

        //Converting BorderLayout.south into a flow layout
        frmInventorysystem.getContentPane().add(frameBottom, BorderLayout.SOUTH);

        confirmBtn = new JButton("Confirm");
        confirmBtn.addActionListener(this);

        frameBottom.add(confirmBtn);

        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(this);
        frameBottom.add(cancelBtn);

        frmInventorysystem.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        //creates new windows to sort equipment when confirmBtn is clicked
        if(e.getSource() == confirmBtn) 
        {
            if(equipList.getSelectedItem().equals("Weapon"))
            {
                //GUIWeaponCategory weapon = new GUIWeaponCategory();
                category = equipList.getSelectedItem().toString();
            }
        }
        //Exits when cancelBtn is clicked
        if(e.getSource() == cancelBtn)
        {
            System.exit(0);
        }
    }

    public String getCategory()
    {
        return category;
    }

    public void setCategory(String a)
    {
        category = a;
    }
}

GUITest按预期启动。 但是,缺少第一个println。 我将如何去做呢? 我缺少什么概念或代码段?

EDIT1:添加了更多细节,使程序可重复且完整。

EDIT2:使代码更具可读性,以便于理解。

1 个答案:

答案 0 :(得分:1)

您的程序上需要进行一些更改

  1. 删除我上面的​​评论中所述的extends JFrame,请参阅Extends JFrame vs. creating it inside the program

  2. 将您的程序放在EDT上,有关如何执行此操作的示例,请参见this answer上的第3点和main方法。

  3. 您对ActionListeners的工作方式感到困惑,他们会等到您在程序中执行某些操作(即,按Confirm按钮)然后再执行某些操作。程序中的“某物”表示:打印所选项目并检查其是否为武器,然后执行其他操作。

因此,在这种情况下,您无需返回到main即可继续执行程序,main仅用于初始化应用程序,而没有其他作用。 您需要思考事件,而不要循序渐进。这是棘手的也是最重要的部分。

您需要从控制台应用程序中更改编程范例,并do-while更改所有内容以顺序方式发生,而不是在用户对您的应用程序执行操作时触发的事件。

例如:

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

public class GUITest implements ActionListener {
    private JFrame frmInventorysystem;
    private JPanel frameBottom;
    private JComboBox equipList;
    private JButton confirmBtn, cancelBtn;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new GUITest()); //Java 8+ if using an earlier version check the point #2 in this answer and modify the code accordingly.
    }

    /**
     * Create the application.
     */
    public GUITest() {
        frmInventorysystem = new JFrame();
        frmInventorysystem.setTitle("InventorySystem");
        frmInventorysystem.setBounds(100, 100, 450, 300);
        frmInventorysystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmInventorysystem.getContentPane().setLayout(new BorderLayout(0, 0));

        /*
         * JFrame inside another JFrame is not recommended. JPanels are used instead
         * Creating a flow layout for the bottom frame
         */
        frameBottom = new JPanel();
        frameBottom.setLayout(new FlowLayout());

        // creates comboBox to find out which of the three items player is looking to
        // insert
        String[] weaponCategories = { "Weapon", "Armor", "Mod" };
        equipList = new JComboBox(weaponCategories);
        frmInventorysystem.getContentPane().add(equipList, BorderLayout.NORTH);

        // Converting BorderLayout.south into a flow layout
        frmInventorysystem.getContentPane().add(frameBottom, BorderLayout.SOUTH);

        confirmBtn = new JButton("Confirm");
        confirmBtn.addActionListener(this);

        frameBottom.add(confirmBtn);

        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(this);
        frameBottom.add(cancelBtn);

        frmInventorysystem.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        // creates new windows to sort equipment when confirmBtn is clicked
        if (e.getSource() == confirmBtn) {
            String category = equipList.getSelectedItem().toString(); //Get the selected category
            doSomething(category); //Pass it as a parameter
        }
        // Exits when cancelBtn is clicked
        if (e.getSource() == cancelBtn) {
            frmInventorysystem.dispose();
        }
    }

    // Do something with the category
    private void doSomething(String selectedEquipment) {
        System.out.println(selectedEquipment);
        if (selectedEquipment.equals("Weapon")) {
            System.out.println("It's a weapon!"); //You can open dialogs or do whatever you need here, not necessarily a print.
        } else {
            System.out.println("Not a weapon");
        }
    }
}

请注意,我删除了继承,我没有返回到main,而是仍然打印所选项目并检查它是否是武器。

我还以一种更安全的方式退出了该应用程序。

这是示例输出:

Weapon
It's a weapon!
Armor
Not a weapon