带有if语句的java用于复选框gui

时间:2014-08-09 02:18:07

标签: java user-interface if-statement

大家好,您可以协助实施以下内容: 风格披萨gui的-drop和drap菜单 肉:火腿,鸡肉和意大利辣香肠 海鲜:虾,贻贝和扇贝 蔬菜:从额外的浇头列表中选择

- 帮助并退出

的帮助菜单栏

-request /添加新的顶部按钮,弹出框要求用户输入顶部,然后是下面的另外两个按钮,一个用于点击请求,它将顶部发送到电子邮件地址,或者如果点击添加顶部,则另一个弹出框请求管理员输入设置为123456的授权码,然后点击另一个按钮,添加顶部,将顶部添加到顶部菜单

-a 2d drwaing

到目前为止,这是我的代码:

//Packages
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;


public class CaseyPizza extends JFrame
{
    private static final long serialVersionUID = 1L;
    private JFrame frame;
    private JPanel pizzaPanel, centerPanel, pricePanel, checkBoxPanel, radioButtonPanel;
    private final int FRAME_WIDTH = 600;
    private final int FRAME_HEIGHT = 400;
    private ButtonGroup group;
    private JRadioButton smallButton, mediumButton, largeButton;
    private JCheckBox olivCheckBox, mushCheckBox, pineapCheckBox, capsiCheckBox;
    private JTextField priceTextField;
    private double price = 0.0;
    private double topPrice = 0.0;
    private double showPrice = 0.0;
    private ActionListener listener = new PriceListener();
    public CaseyPizza() {
        pizzaPanel = new JPanel();
        pizzaPanel.setLayout(new BorderLayout(10, 10));
        createRadioButtonPanel();
        createCheckBoxPanel();
        createPricePanel();
        createCenterPanel();

        pizzaPanel.add(centerPanel, BorderLayout.CENTER);
        pizzaPanel.add(pricePanel, BorderLayout.SOUTH);
        frame = new JFrame("Casey Pizza Ordering System");
        frame.add(pizzaPanel, BorderLayout.CENTER);
        frame.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocation(100, 100);
        frame.setVisible(true);
    }
    private void createRadioButtonPanel() {
        radioButtonPanel = new JPanel();
        radioButtonPanel.setLayout(new GridLayout(3, 1));
        radioButtonPanel.setBorder(new TitledBorder(new EtchedBorder(), "Size"));
        group = new ButtonGroup();
        smallButton = new JRadioButton(" Small ");
        group.add(smallButton);
        smallButton.addActionListener(listener);
        radioButtonPanel.add(smallButton);
        mediumButton = new JRadioButton(" Medium ");
        group.add(mediumButton);
        mediumButton.addActionListener(listener);
        radioButtonPanel.add(mediumButton);
        largeButton = new JRadioButton(" Large ");
        group.add(largeButton);
        largeButton.addActionListener(listener);
        radioButtonPanel.add(largeButton);
    }

    // add the check boxes to this frame
    // add action listener for the check boxes
    private void createCheckBoxPanel() {
        checkBoxPanel = new JPanel();
        checkBoxPanel.setLayout(new GridLayout(4, 1));
        olivCheckBox = new JCheckBox(" Olives ");
        olivCheckBox.addActionListener(listener);
        checkBoxPanel.add(olivCheckBox);
        mushCheckBox = new JCheckBox(" Mushrooms ");
        mushCheckBox.addActionListener(listener);
        checkBoxPanel.add(mushCheckBox);
        pineapCheckBox = new JCheckBox(" Pineapple ");
        pineapCheckBox.addActionListener(listener);
        checkBoxPanel.add(pineapCheckBox);
        capsiCheckBox = new JCheckBox(" Capsicum ");
        capsiCheckBox.addActionListener(listener);
        checkBoxPanel.add(capsiCheckBox);
    }

    //Price Panel
    private void createPricePanel() {
        pricePanel = new JPanel();
        pricePanel.add(new JLabel("Your Price:"));
        priceTextField = new JTextField(7);
        priceTextField.setFont(new Font("Serif", Font.BOLD, 16));
        priceTextField.setEditable(false);
        priceTextField.setForeground(Color.blue);
        priceTextField.setBackground(pricePanel.getBackground());
        priceTextField.setDisabledTextColor(Color.blue);
        priceTextField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        pricePanel.add(priceTextField);
        priceTextField.setText(" n/a Price");
    }
    private void createCenterPanel() {
        centerPanel = new JPanel();
        centerPanel.add(radioButtonPanel);
        centerPanel.add(checkBoxPanel);
    }



    //Method for price of pizza build
    private class PriceListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            topPrice = 0;
            if (smallButton.isSelected()) {
                price = 3.00;
            } else if (mediumButton.isSelected()) {
                price = 6.00;
            } else if (largeButton.isSelected()) {
                price = 8.00;
            }

            if(olivCheckBox.isSelected())
            {
                topPrice += 0.50;
            }
            if(mushCheckBox.isSelected())
            {
                topPrice += 0.50;
            }
            if(pineapCheckBox.isSelected())
            {
                topPrice += 0.50;
            }
            if(capsiCheckBox.isSelected())
            {
                topPrice += 0.50;
}
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    showPrice = price + topPrice;
                    priceTextField.setText(" $" + showPrice);
                    System.out.println("Price dispayed");
                }
            });
        }
}
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                CaseyPizza pPF = new CaseyPizza();
                System.out.println("Run");
            }
        });
}
}

2 个答案:

答案 0 :(得分:0)

应该是

if (olivCheckBox.isSelected() && mushCheckBox.isSelected() && pineapCheckBox.isSelected() && capsiCheckBox.isSelected()){

同样改变所有其他if语句

或类似

if( (olivCheckBox.isSelected()) && (mushCheckBox.isSelected()) && (pineapCheckBox.isSelected()) && (capsiCheckBox.isSelected())) {

表示所有if / else if语句

前三个条件语法中的语法不正确:

if ((olivCheckBox.isSelected()) && (mushCheckBox.isSelected()) && (pineapCheckBox.isSelected()) && (capsiCheckBox.isSelected())){
                topPrice = 2.00;
            } else if ((olivCheckBox.isSelected()) && (mushCheckBox.isSelected()) && (pineapCheckBox.isSelected())) {
                topPrice = 1.50;
            } else if ((olivCheckBox.isSelected()) && (mushCheckBox.isSelected())) {
                topPrice = 1.00;
            } 

你缺少if / else if。

的括号

答案 1 :(得分:0)

也许我错过了这一点,但......

如果每个顶部都是0.50,为什么不只是使用一系列if语句来检查选中的复选框,并将0.50添加到topPrice,示例...

topPrice = 0;
if(olivCheckBox.isSelected())
{
    topPrice += 0.50;
}
if(mushCheckBox.isSelected())
{
    topPrice += 0.50;
}
if(pineapCheckBox.isSelected())
{
    topPrice += 0.50;
}
if(capsiCheckBox.isSelected())
{
    topPrice += 0.50;
}

你甚至可以把它们放在一个数组中,然后简单地循环它......

JCheckBox toppings[] = new JCheckBox[] {
    olivCheckBox,
    mushCheckBox,
    pineapCheckBox,
    capsiCheckBox
};
topPrice = 0;
for (JCheckBox topping : toppings) {
    if (topping.isSelected()) {
        topPrice += 0.50;
    }
}

这样,如果你想添加更多的浇头,你就不需要重写很多代码......