JAVA - 无法从isSelected()返回

时间:2017-05-21 14:18:55

标签: java

我在这里有这部分代码,但每当我启动程序时,我只会得到左脑。即使我检查一个或另一个盒子。

    chckbxMusic = new JCheckBox("Music");
    chckbxMusic.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            chckbxMath.setSelected(false);
        }
    });


    chckbxMusic.setBounds(46, 107, 85, 23);
    Question_1.add(chckbxMusic);


    chckbxMath = new JCheckBox("Math");
    chckbxMath.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {   
            chckbxMusic.setSelected(false);
        }
    });
    chckbxMath.setBounds(243, 107, 128, 23);
    Question_1.add(chckbxMath);

    if(chckbxMusic.isSelected()) brain_right++;
    else brain_left++;

1 个答案:

答案 0 :(得分:0)

好吧也许完整的代码可以帮助更多.. 我重写了一下,但两个变量的结果总是为0 ??

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class PrgVeloce {

    private JFrame frame;
    private JPanel Question;
    private JPanel Result;
    public int brain_left;
    public int brain_right;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    PrgVeloce window = new PrgVeloce();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public PrgVeloce() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new CardLayout(0, 0));

        Question = new JPanel();
        frame.getContentPane().add(Question, "name_971195205673");
        Question.setLayout(null);

        final JCheckBox chckbxMath = new JCheckBox("Math");
        chckbxMath.setBounds(39, 115, 128, 23);
        Question.add(chckbxMath);

        final JCheckBox chckbxMusic = new JCheckBox("Music");
        chckbxMusic.setBounds(237, 115, 128, 23);
        Question.add(chckbxMusic);


        JButton btnNext = new JButton("Next");
        btnNext.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent e) {
                if(chckbxMusic.isSelected())
                {
                    brain_left = 10;
                }
               if(chckbxMath.isSelected())
                {
                    brain_right = 10;
                }
                Question.setVisible(false);
                Result.setVisible(true);
            }
        });
        btnNext.setBounds(158, 210, 117, 29);
        Question.add(btnNext);

        Result = new JPanel();
        frame.getContentPane().add(Result, "name_978670915264");
        Result.setLayout(null);

        JLabel lblEmisferoDx = new JLabel("Right brain:");
        lblEmisferoDx.setBounds(90, 118, 110, 16);
        Result.add(lblEmisferoDx);

        JLabel lblNewLabel = new JLabel("Left Brain:");
        lblNewLabel.setBounds(90, 157, 84, 16);
        Result.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel(brain_right + "%");
        lblNewLabel_1.setBounds(218, 118, 61, 16);
        Result.add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel(brain_left + "%");
        lblNewLabel_2.setBounds(218, 157, 61, 16);
        Result.add(lblNewLabel_2);
    }
}