我有一些代码,它有3个复选框,由于某种原因,当我选中一个复选框,然后另一个我检查的第一个自动取消选中...它之前工作但后来我改变了一些东西而且它不是工作了......任何人都知道我的代码有什么问题吗?
代码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Pizza2 extends JFrame{
public static void main(String[] args){
new Pizza2();
}
JTextField name, phone, address;
JRadioButton small, medium, large, thick, thin;
JCheckBox pepperoni, ham, sausage;
JButton okButton, closeButton;
public Pizza2(){
this.setTitle("Pizza Order");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("pizzaIcon.png")));
ButtonListener bl = new ButtonListener();
JPanel panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
addItem(panel1, new JLabel("Name:"), 0, 0, 1, 1, GridBagConstraints.EAST);
addItem(panel1, new JLabel("Phone:"), 0, 1, 1, 1, GridBagConstraints.EAST);
addItem(panel1, new JLabel("Address:"), 0, 2, 1, 1, GridBagConstraints.EAST);
name = new JTextField(20);
phone = new JTextField(10);
address = new JTextField(20);
addItem(panel1, name, 1, 0, 2, 1, GridBagConstraints.WEST);
addItem(panel1, phone, 1, 1, 1, 1, GridBagConstraints.WEST);
addItem(panel1, address, 1, 2, 2, 1, GridBagConstraints.WEST);
Box sizeBox = Box.createVerticalBox();
small = new JRadioButton("Small");
medium = new JRadioButton("Medium");
large = new JRadioButton("Large");
small.setSelected(true);
ButtonGroup sizeGroup = new ButtonGroup();
sizeGroup.add(small);
sizeGroup.add(medium);
sizeGroup.add(large);
sizeBox.add(small);
sizeBox.add(medium);
sizeBox.add(large);
sizeBox.setBorder(BorderFactory.createTitledBorder("Size"));
addItem(panel1, sizeBox, 0, 3, 1, 1, GridBagConstraints.NORTH);
Box styleBox = Box.createVerticalBox();
thin = new JRadioButton("Thin");
thick = new JRadioButton("Thick");
ButtonGroup styleGroup = new ButtonGroup();
styleGroup.add(thin);
styleGroup.add(thick);
styleBox.add(thin);
styleBox.add(thick);
styleBox.setBorder(BorderFactory.createTitledBorder("Style"));
addItem(panel1, styleBox, 1, 3, 1, 1, GridBagConstraints.NORTH);
Box topBox = Box.createVerticalBox();
pepperoni = new JCheckBox("Pepperoni");
ham = new JCheckBox("Ham");
sausage = new JCheckBox("Sausage");
ButtonGroup topGroup = new ButtonGroup();
topGroup.add(pepperoni);
topGroup.add(ham);
topGroup.add(sausage);
topBox.add(pepperoni);
topBox.add(ham);
topBox.add(sausage);
topBox.setBorder(BorderFactory.createTitledBorder("Toppings"));
addItem(panel1, topBox, 2, 3, 1, 1, GridBagConstraints.NORTH);
Box buttonBox = Box.createHorizontalBox();
okButton = new JButton("OK");
closeButton = new JButton("Close");
okButton.addActionListener(bl);
closeButton.addActionListener(bl);
buttonBox.add(okButton);
buttonBox.add(Box.createHorizontalStrut(20));
buttonBox.add(closeButton);
addItem(panel1, buttonBox, 2, 4, 1, 1, GridBagConstraints.NORTH);
this.add(panel1);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private void addItem(JPanel p, JComponent c, int x, int y, int width, int height, int align){
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = x;
gc.gridy = y;
gc.gridwidth = width;
gc.gridheight = height;
gc.weightx = 100.0;
gc.weighty = 100.0;
gc.insets = new Insets(5, 5, 5, 5);
gc.anchor = align;
gc.fill = GridBagConstraints.NONE;
p.add(c, gc);
}
private class ButtonListener implements ActionListener{
boolean isValid = false;
final String ENTRY = "This information is required";
public void actionPerformed(ActionEvent e){
if(e.getSource() == okButton){
//CHECK IF FIELDS CONTAIN INFO
if(name.getText().equals("") || name.getText().equals(ENTRY)){
isValid = false;
name.setText(ENTRY);
}
if(phone.getText().equals("") || phone.getText().equals(ENTRY)){
isValid = false;
phone.setText(ENTRY);
}
if(address.getText().equals("") || address.getText().equals(ENTRY)){
isValid = false;
address.setText(ENTRY);
}
else
{
isValid = true;
//IF THE INFORMATION IS VALID:
if(isValid = true){
String tops = "";
if(pepperoni.isSelected()){
tops += "Pepperoni\n";
}
if(ham.isSelected()){
tops += "Ham\n";
}
if(sausage.isSelected()){
tops += "Sausage\n";
}
String msg = "You ordered a ";
if(small.isSelected()){
msg += "small pizza with ";
}
if(medium.isSelected()){
msg += "medium pizza with ";
}
if(large.isSelected()){
msg += "large pizza with ";
}
if(tops.equals("")){
msg += "no toppings.";
}
else{
msg += "the following toppings:\n" + tops;
}
String info = "\nDeliver to " + name.getText() + " at " + address.getText();
String phoneNo = "\nYour phone number: " + phone.getText();
String fullMsg = msg + info + phoneNo;
JOptionPane.showMessageDialog(null, fullMsg, "Your Order", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
}
else if(e.getSource() == closeButton){
int message = JOptionPane.showConfirmDialog(null, "Your Order will be cancelled", "Cancel Order", JOptionPane.WARNING_MESSAGE);
if(message == JOptionPane.OK_OPTION){
System.exit(0);
}
}
}
}
}
请随意将此代码复制到编码器中并尝试找出问题 非常感谢任何人帮助我:D
答案 0 :(得分:4)
您正在谈论的是“意大利辣香肠”,“火腿”和“香肠”复选框?如果您不想要此行为,请不要将它们放在ButtonGroup中。这种行为就是ButtonGroup的用途。
此类用于为一组按钮创建多重排除范围。使用相同的ButtonGroup对象创建一组按钮意味着“打开”其中一个按钮会关闭组中的所有其他按钮。