从单选按钮,复选框返回值

时间:2015-04-11 18:07:59

标签: java jpanel jcheckbox jradiobutton

我是Java的新手,我无法返回单选按钮和复选框所代表的值(价格)。我之前从未这样做过,所以我真的不确定从哪里开始。这就是我所拥有的:

public class Size extends JPanel {

JLabel lblSize;
JRadioButton radioSmall;
JRadioButton radioMedium;
JRadioButton radioLarge;
double cost = 0.00;
String size = "Unknown Size";

public Size() {
    createPanel();
}

private void createPanel() {
    super.setLayout(new GridBagLayout());
    GridBagConstraints bag = new GridBagConstraints();
    ButtonGroup group = new ButtonGroup();

    bag.fill = GridBagConstraints.BOTH;
    bag.anchor = GridBagConstraints.FIRST_LINE_START;
    bag.insets = new Insets(5,5,5,5);

    bag.gridx = 0;
    bag.gridy = 0;
    bag.gridwidth = 2;
    lblSize = new JLabel("What size pizza?");
    lblSize.setFont(new Font("Arial", Font.BOLD, 12));
    lblSize.setForeground(Color.BLUE);
    this.add(lblSize, bag);

    bag.gridx = 0;
    bag.gridy = 1;
    radioSmall = new JRadioButton("Small");
    radioSmall.setActionCommand("8.00");
    group.add(radioSmall);
    this.add(radioSmall, bag);

    bag.gridx = 0;
    bag.gridy = 2;
    radioMedium = new JRadioButton("Medium");
    radioMedium.setActionCommand("10.00");
    group.add(radioMedium);
    this.add(radioMedium, bag);

    bag.gridx = 0;
    bag.gridy = 3;
    radioLarge = new JRadioButton("Large");
    radioLarge.setActionCommand("12.00");
    group.add(radioLarge);
    this.add(radioLarge, bag);
}

ActionListener radioButtonActionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("8.00")) {
            cost = 8.00;
            size = "Small";
        }
        else if (e.getActionCommand().equals("10.00")) {
            cost = 10.00;
            size = "Medium";
        }
        else if (e.getActionCommand().equals("12.00")) {
            cost = 12.00;
            size = "Large";
        }
        else {
            cost = 0.00;
        }
    }
};

public double getCost() {
    return cost;
}

public String getPizzaSize() {
    return size;
}

}

这是我尝试实施复选框的地方。有人可以指出我正确的方向吗?:

public class Toppings extends JPanel {

JLabel lblToppings;
JCheckBox boxPepperoni;
JCheckBox boxSausage;
JCheckBox boxMushroom;
JCheckBox boxOnion;

double toppingCost = 0.00;
StringBuilder str = new StringBuilder();

public Toppings() {
    createPanel();
}

private void createPanel() {
    super.setLayout(new GridBagLayout());
    GridBagConstraints bag = new GridBagConstraints();

    bag.fill = GridBagConstraints.BOTH;
    bag.anchor = GridBagConstraints.FIRST_LINE_START;
    bag.insets = new Insets(5,5,5,5);

    bag.gridx = 0;
    bag.gridy = 0;
    boxPepperoni = new JCheckBox("Pepperoni");
    boxPepperoni.addItemListener(new CheckBoxListener());
    this.add(boxPepperoni, bag);

    bag.gridx = 1;
    bag.gridy = 0;
    boxSausage = new JCheckBox("Sausage");
    boxSausage.addItemListener(new CheckBoxListener());
    this.add(boxSausage, bag);

    bag.gridx = 0;
    bag.gridy = 1;
    boxMushroom = new JCheckBox("Mushroom");
    boxMushroom.addItemListener(new CheckBoxListener());
    this.add(boxMushroom, bag);

    bag.gridx = 1;
    bag.gridy = 1;
    boxOnion = new JCheckBox("Onion");
    boxOnion.addItemListener(new CheckBoxListener());
    this.add(boxOnion, bag);
}

public double getToppingCost() {
    return toppingCost;
}

public String getToppings() {
    return str.toString();
}

private class CheckBoxListener implements ItemListener {
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource()==boxPepperoni) {
            if(boxPepperoni.isSelected()) {
                toppingCost += 2.00;
                str.append("Pepperoni");
            }
        }
        else if (e.getSource()==boxSausage) {
            if(boxSausage.isSelected()) {
                toppingCost += 2.00;
                str.append("Sausage");
            }
        }
        else if (e.getSource()==boxMushroom) {
            if(boxMushroom.isSelected()) {
                toppingCost += 2.00;
                str.append("Mushroom");
            }
        }
        else if (e.getSource()==boxOnion) {
            if(boxOnion.isSelected()) {
                toppingCost += 2.00;
                str.append("Onion");
            }
        }
    }
}

} 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您在createPanel()的代码中遗漏了以下内容(我的意思是第一个示例)

radioSmall.addActionListener(radioButtonActionListener);
radioMedium.addActionListener(radioButtonActionListener);
radioLarge.addActionListener(radioButtonActionListener);

当您添加这些来电时,您的getCost()getPizzaSize()方法会返回费用和尺寸。