亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何等待從 GUI 返回主界面的輸入?

我如何等待從 GUI 返回主界面的輸入?

LEATH 2022-12-21 16:31:36
我希望能夠將用戶輸入從我的 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 按預期啟動。但是,缺少第一個 println。我該怎么做呢?我缺少哪些概念或代碼片段?EDIT1:添加了更多細節以使程序可重現和完整。EDIT2:使代碼更具可讀性以便于理解。
查看完整描述

1 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

您的程序需要進行一些更改

  1. extends JFrame如我上面的評論所述刪除,請參閱擴展 JFrame 與在程序中創建它

  2. 將您的程序放在 EDT 上,請參閱此答案的第 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


查看完整回答
反對 回復 2022-12-21
  • 1 回答
  • 0 關注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號