简体   繁体   English

Java-我一次只能选中一个复选框吗?

[英]Java- I can only check one checkbox at a time?

I have some code where it has 3 checkboxes and for some reason when I check one checkbox and then another the first one I check automatically unchecks... It was working before but then I changed some things around and it isnt't working anymore... Anyone know what is wrong with my code? 我有一些代码,其中有3个复选框,由于某种原因,当我选中一个复选框,然后又选中另一个复选框时,我自动取消选中了它。。。它以前工作过,但后来我改变了一些东西,现在不工作了。 ..有人知道我的代码有什么问题吗?

The code: 编码:

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);
                }
            }
        }
    }
}

PLEASE FEEL FREE TO COPY THIS CODE INTO A COMPILER AND TRY TO FIND THE PROBLEM THANK YOU SO MUCH TO ANYONE WHO HELPS ME :D 请随意将本代码复制到编译器中,并尝试查找对您有帮助的任何人所表示的问题:D

It is the "pepperoni", "ham", and "sausage" checkboxes you are talking about? 您正在谈论的是“意大利辣味香肠”,“火腿”和“香肠”复选框? If you do not want this behaviour, do not put them in a ButtonGroup. 如果您不希望这种行为,请不要将它们放在ButtonGroup中。 That behaviour is what a ButtonGroup is for. 该行为就是ButtonGroup的目的。

From the ButtonGroup documentation : ButtonGroup文档中

This class is used to create a multiple-exclusion scope for a set of buttons. 此类用于为一组按钮创建多重排除作用域。 Creating a set of buttons with the same ButtonGroup object means that turning "on" one of those buttons turns off all other buttons in the group. 用相同的ButtonGroup对象创建一组按钮意味着将这些按钮之一“打开”会关闭组中的所有其他按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java-如何创建延迟一定时间的字符串检查? - java- How do I create a String check with a delay of a certain amount of time? 爪哇检查回文 - Java- Check palindrome Java-如何获取我的TicTacToe游戏以正确检查获胜者? - Java- How can I get my TicTacToe game to check for winners properly? 如何在NLevel Listview中一次仅选中一个复选框 - How to check only one checkbox at a time in NLevel Listview java-使用扫描器一次读取和处理一条信息 - java- use scanner to read in and process one piece of information at a time Java-我想用另一个改变一个特定的字符串 - Java- I want to change a particular string with another one Java- For循环,带有带默认情况的switch语句。 如何获得默认情况下仅打印输出一次? - Java- For loop with a switch statement containing default case. How can I get the default case to print the output only once? js-我可以使用OAuth仅使用javascript-> clientside和js / java->服务器端将用户验证到我的应用程序中吗? - js- can i authenticate a user into my app using OAuth with only javascript->clientside, and js/java->server side? Java-如何通过GUI获取数字数组 - Java- How can I get an array of numbers through a GUI Java如何自动选中下面的复选框? - Java How can I check a checkbox below automatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM