简体   繁体   English

JPanel在继续操作之前不会等待用户输入

[英]JPanel doesn't wait for user input before moving on

I'm trying to make a program that will do some fairly simple calculations in a JPanel. 我正在尝试制作一个程序,它将在JPanel中执行一些相当简单的计算。 One of the requirements is that it will repeat until the user says otherwise. 要求之一是重复进行直到用户说不出来为止。 Unfortunately when the JPanel opens it does not wait for any user input, but instead moves right on to the next thing (as I have yet written the part that asks the user if he would like to continue or quit, it's currently in an infinite while loop which means that new JPanels just keep opening continually until I stop the program). 不幸的是,当JPanel打开时,它不等待任何用户输入,而是直接移至下一件事(因为我已经编写了询问用户是否要继续或退出的部分,它目前处于无限状态循环,这意味着新的JPanels会一直保持打开状态,直到我停止程序为止)。 My question is: Why isn't the program waiting until the user presses the Okay button to move on? 我的问题是:为什么程序没有等到用户按下“确定”按钮继续前进?

Here is my main(): 这是我的main():

public class Driver extends JFrame{

public static void main(String args[]){


    do{
        new GUI();
        while (!GUI.isButtonPressed()){

        }
        //int choice = JOptionPane.showConfirmDialog(null, "Default tax rate is 7.5%\nDefault discount rate is 10%.\nKeep these default values?", "Choose an Option", JOptionPane.YES_NO_OPTION);
        //switch (choice){
            //case JOptionPane.YES_OPTION: {
                //SaleItem newItem = new SaleItem();
                //break;
            //}
            //case JOptionPane.NO_OPTION: {
                //new GUI();
                //break;
            //}
        //}
    }while(true);
}

and here is my GUI: 这是我的GUI:

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



public class GUI extends JFrame {
    JPanel panel;
    JLabel priceLabel;
    JLabel discountLabel;
    JLabel taxLabel;
    JTextField taxTextField;
    JTextField priceTextField;
    JTextField discountTextField;
    JButton okayButton;
    JButton defaultsButton;
    final int WINDOW_WIDTH = 300;
    final int WINDOW_HEIGHT = 250;
    boolean buttonPressed = false;

    public boolean isButtonPressed() {
        return buttonPressed;
    }

    public void setButtonPressed(boolean buttonPressed) {
        this.buttonPressed = buttonPressed;
    }

    public GUI(){
        super("Item Properties");
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(panel);
        setVisible(true);
    }

    private void buildPanel(){
        priceLabel = new JLabel("Enter the item price:");
        priceTextField = new JTextField(10);

        discountTextField = new JTextField(10);
        discountLabel = new JLabel("Enter the discount percent:");

        taxLabel = new JLabel("Enter the tax percent:");
        taxTextField = new JTextField(10);

        okayButton = new JButton("Okay");
        okayButton.addActionListener(new ButtonListener());

        defaultsButton = new JButton("Use Defaults");
        defaultsButton.addActionListener(new ButtonListener());

        panel = new JPanel();

        panel.add(priceLabel);
        panel.add(priceTextField);
        panel.add(discountLabel);
        panel.add(discountTextField);
        panel.add(taxLabel);
        panel.add(taxTextField);

        panel.add(okayButton);
        panel.add(defaultsButton);
    }

    private class ButtonListener implements ActionListener {
        private boolean buttonPressed = false;

        public void actionPerformed(ActionEvent event){
            GUI.setButtonPressed(true);
            SaleItem newItem = new SaleItem(Double.parseDouble(priceTextField.getText()), Double.parseDouble(discountTextField.getText()), Double.parseDouble(taxTextField.getText()));
    }
}

The stuff about getButtonPressed() and isButtonPressed() and all that was me trying (unsuccessfully) to solve the problem on my own. 关于getButtonPressed()和isButtonPressed()的东西以及所有我试图(但不成功)自己解决的问题。

Thanks in advance for any help! 在此先感谢您的帮助!

Unfortunately when the JPanel opens it does not wait for any user input, 不幸的是,当JPanel打开时,它不会等待任何用户输入,

Panels don't open. 面板无法打开。 You add a panel to a frame or dialog. 您将面板添加到框架或对话框。 If you want something to "open" or "popup" then you use a JDialog. 如果您希望“打开”或“弹出”某些内容,请使用JDialog。

SaleItem newItem = new SaleItem(...);

SaleItem should be a modal JDialog . SaleItem应该是modal JDialog Then the dialog will not close until the user clicks on button that you create to close the dialog. 然后,对话框将关闭,直到用户单击您创建的按钮以关闭对话框。 A dialog is created exactly the same way as you create a JFrame. 创建对话框的方式与创建JFrame完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM