简体   繁体   中英

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. 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). My question is: Why isn't the program waiting until the user presses the Okay button to move on?

Here is my 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:

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.

Thanks in advance for any help!

Unfortunately when the JPanel opens it does not wait for any user input,

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.

SaleItem newItem = new SaleItem(...);

SaleItem should be a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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