当取消选中复选框或单选按钮时,如何防止if语句更改值



我目前正在研究一个简单的程序来学习如何制作GUI。用户有能力选中某些选项,如果他们不再希望他们取消选中。但是,当取消选择框并进行另一个选择时,它仍然会计算我的else语句并相应地减少值。

下面的代码就是这样一个例子,即使我什么都不做,我点击其他的值仍然从变量"itemTotal"中扣除,我认为这是因为每次触发一个偶数,并采取行动,但这不是我想要发生的事情

if(cheesebx.isSelected())
        {
            if(itemTotal == 0)
            {
                JOptionPane.showMessageDialog(null, "Please select Single, or Double burger first");
                cheesebx.setSelected(false);
            }
            else
            {
            itemTotal += cheese;
            }
        }
        else if (!cheesebx.isSelected())
        {
            itemTotal -= cheese;
        }

我注意到,如果选择了其他内容,下面的代码位也会从总框中扣除值。

if(single_radio.isSelected())
        {
            if(itemTotal != 0 && itemTotal != single)
            {
                itemTotal -= two;
                itemTotal += single;
            }
            if(itemTotal == 0)
            {
                itemTotal += single;
            }

        }

这是我的完整代码。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package week3lab;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class posGUI extends javax.swing.JFrame implements ActionListener {
    public double single = 3.50;
    public double two = 4.75;
    public double cheese = 0.50;
    public double bacon  = 1.25;
    public double meal   = 4.00;
    public double orderTotal = 0;
    public double itemTotal  = 0;
    public int quantity = 1;

    /**
     * Creates new form posGUI
     */
    public posGUI() {
        initComponents();
        exitbtn.addActionListener(this);
        addbtn.addActionListener(this);
        newbtn.addActionListener(this);
        nextbtn.addActionListener(this);
        single_radio.addActionListener(this);
        double_radio.addActionListener(this);
        cheesebx.addActionListener(this);
        baconbx.addActionListener(this);
        mealbx.addActionListener(this);
        pricetxt.setText(Double.toString(itemTotal));
        totaltxt.setText(Integer.toString(quantity));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        single_radio = new javax.swing.JRadioButton();
        double_radio = new javax.swing.JRadioButton();
        cheesebx = new javax.swing.JCheckBox();
        baconbx = new javax.swing.JCheckBox();
        mealbx = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        pricetxt = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        quantitytxt = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        totaltxt = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ordertxt = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        exitbtn = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        addbtn = new javax.swing.JMenuItem();
        nextbtn = new javax.swing.JMenuItem();
        newbtn = new javax.swing.JMenuItem();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        buttonGroup1.add(single_radio);
        single_radio.setText("Single Burger");
        buttonGroup1.add(double_radio);
        double_radio.setText("Double Burger");
        cheesebx.setText("Add Cheese");
        baconbx.setText("Add Bacon");
        mealbx.setText("Make it a meal");
        jLabel1.setText("Item Price");
        jLabel2.setText("YOUR ORDER");
        jLabel3.setText("Quantity");
        jLabel4.setText("Order Total");
        ordertxt.setColumns(20);
        ordertxt.setRows(5);
        jScrollPane1.setViewportView(ordertxt);
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(mealbx)
                            .addComponent(double_radio)
                            .addComponent(cheesebx)
                            .addComponent(baconbx))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(single_radio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addGap(92, 92, 92))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(pricetxt)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jLabel3)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(quantitytxt)
                            .addComponent(totaltxt, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
                        .addGap(22, 22, 22))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(single_radio)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(double_radio)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cheesebx)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(baconbx)
                        .addGap(18, 18, 18)
                        .addComponent(mealbx))
                    .addComponent(jScrollPane1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(quantitytxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(pricetxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(totaltxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(32, 32, 32))
        );
        jMenu1.setText("File");
        exitbtn.setText("Exit");
        jMenu1.add(exitbtn);
        jMenuBar1.add(jMenu1);
        jMenu2.setText("Order");
        addbtn.setText("Add to Order");
        jMenu2.add(addbtn);
        nextbtn.setText("Clear for Next Item");
        jMenu2.add(nextbtn);
        newbtn.setText("New Order");
        jMenu2.add(newbtn);
        jMenuBar1.add(jMenu2);
        setJMenuBar(jMenuBar1);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>                        
    /**
     * @param args the command line arguments
     */

   @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == exitbtn)
        {
            System.exit(0);
        }
        if(e.getSource() == nextbtn)
        {
            ordertxt.append(Integer.toString(quantity) + "");
            if(single_radio.isSelected())
            {
                ordertxt.append("single ");
            }
            if (cheesebx.isSelected())
            {
                ordertxt.append("cheese ");
            }
            if (baconbx.isSelected())
            {
                ordertxt.append("bacon ");
            }
            if(mealbx.isSelected())
            {
                ordertxt.append("meal ");
            }
            orderTotal += itemTotal * quantity;
            ordertxt.append("at " + Double.toString(itemTotal) + "" + "each");
            totaltxt.setText(Double.toString(orderTotal));
        }
        if(single_radio.isSelected())
        {
            if(itemTotal != 0 && itemTotal != single)
            {
                itemTotal -= two;
                itemTotal += single;
            }
            if(itemTotal == 0)
            {
                itemTotal += single;
            }

        }
        if(double_radio.isSelected())
        {
            if(itemTotal != 0 && itemTotal != two)
            {
                itemTotal -= single;
                itemTotal += two;
            }
            if(itemTotal == 0)
            {
                itemTotal += two;
            }

        }
        if(cheesebx.isSelected())
        {
            if(itemTotal == 0)
            {
                JOptionPane.showMessageDialog(null, "Please select Single, or Double burger first");
                cheesebx.setSelected(false);
            }
            else
            {
            itemTotal += cheese;
            }

        }
        if(baconbx.isSelected())
        {
            if(itemTotal == 0)
            {
                JOptionPane.showMessageDialog(null, "Please select Single, or Double burger first");
                baconbx.setSelected(false);
            }
            else
            {
                itemTotal += bacon;
            }

        }
        if(mealbx.isSelected())
        {    
            if(itemTotal == 0)
            {
                JOptionPane.showMessageDialog(null, "Please select Single, or Double burger first");
                mealbx.setSelected(false);
            }
            else
            {
                itemTotal += meal;
            }
        }
        pricetxt.setText(Double.toString(itemTotal));
    }

    // Variables declaration - do not modify                     
    private javax.swing.JMenuItem addbtn;
    private javax.swing.JCheckBox baconbx;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JCheckBox cheesebx;
    private javax.swing.JRadioButton double_radio;
    private javax.swing.JMenuItem exitbtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JCheckBox mealbx;
    private javax.swing.JMenuItem newbtn;
    private javax.swing.JMenuItem nextbtn;
    private javax.swing.JTextArea ordertxt;
    private javax.swing.JTextField pricetxt;
    private javax.swing.JTextField quantitytxt;
    private javax.swing.JRadioButton single_radio;
    private javax.swing.JTextField totaltxt;
    // End of variables declaration                   
}

当您单击常规按钮时,它不会从actionPerformed(ActionEvent e)方法返回,但将继续检查您的JRadioButton的选择。解决方案是使用if {...} else if {...}语句,如下所示:

@Override
public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if (source == exitbtn) {
        System.exit(0);
    } else if (source == nextbtn) {
        ordertxt.append(Integer.toString(quantity) + "");
        if (single_radio.isSelected()) {
            ordertxt.append("single ");
        } else if (cheesebx.isSelected()) {
            ordertxt.append("cheese ");
        } else if (baconbx.isSelected()) {
            ordertxt.append("bacon ");
        } else if (mealbx.isSelected()) {
            ordertxt.append("meal ");
        }
        orderTotal += itemTotal * quantity;
        ordertxt.append("at " + Double.toString(itemTotal) + "" + "each");
        totaltxt.setText(Double.toString(orderTotal));
    } else if(source == single_radio) {
        if (single_radio.isSelected()) {
            if (itemTotal != 0 && itemTotal != single) {
                itemTotal -= two;
                itemTotal += single;
            }
            if (itemTotal == 0) {
                itemTotal += single;
            }
        }
    }
    // Etcetera
}

确保为每个操作显式检查源对象。

另一种选择是实现单独的Action对象,如下所示:https://docs.oracle.com/javase/tutorial/uiswing/misc/action.html

相关内容

  • 没有找到相关文章

最新更新