试图让RadioButton.isSelected()来设置标签的文本。不工作

时间:2015-05-30 04:09:53

标签: java swing jradiobutton

这是我的代码:

    panel2.add(question1);
    panel2.add(true1);
    panel2.add(false1);
    panel2.add(labelAnswer1);
    panel2.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    if(true1.isSelected()) {
        labelAnswer1.setText("You are CORRECT!");
    }

    else if(false1.isSelected()) {
        labelAnswer1.setText("You are wrong. The correct answer is " + true1.getText());
    }

    else if(true1.isSelected() && false1.isSelected()) {
        labelAnswer1.setText("You cannot select two answers!");
    }

不确定我做错了什么。可能只是太晚了,哈哈。任何帮助,将不胜感激。谢谢和最好的问候!

3 个答案:

答案 0 :(得分:4)

像大多数GUI框架一样,Swing是事件驱动的,也就是说,某些事情会发生,然后你会对它做出回应。您不知道这些事件可能发生的时间或顺序。

相反,你使用"听众" (也称为Observer Pattern)用于注册您组件的兴趣,这些组件会告诉您何时发生事件并根据您的计划的当前状态采取适当的行动

请查看How to Use Buttons, Check Boxes, and Radio ButtonsHow to Write an Action Listeners了解详情

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.WEST;

            add(new JLabel("Is a banna:"), gbc);

            ButtonGroup bg = new ButtonGroup();
            ActionListener listener = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String cmd = e.getActionCommand();
                    System.out.println("You guessed " + cmd);
                    if ("Yellow".equalsIgnoreCase(cmd)) {
                        JOptionPane.showMessageDialog(TestPane.this, cmd + " is the answer");
                    } else {
                        JOptionPane.showMessageDialog(TestPane.this, cmd + " is not the answer");
                    }
                }
            };
            add(createGuess("Red", listener, bg), gbc);
            add(createGuess("Green", listener, bg), gbc);
            add(createGuess("Blue", listener, bg), gbc);
            add(createGuess("Yellow", listener, bg), gbc);
        }

        protected JRadioButton createGuess(String guess, ActionListener listener, ButtonGroup bg) {
            JRadioButton btn = new JRadioButton(guess);
            btn.addActionListener(listener);
            bg.add(btn);
            return btn;
        }

    }

}

答案 1 :(得分:1)

您应该添加更多代码,以便我们可以看到您的听众在哪里,其他组件等。

你的if语句应该在听众中。现在,代码在您将元素添加到页面后立即执行,而不是其他时间。因此,此代码仅在添加元素之后,在用户有时间选择元素之前发生。另外,如果没有选择元素怎么办?然后没有命中if语句,因此你可能需要添加else语句。

此设置应该是您有两个单选按钮,一个问题,以及用户选择答案时按下的按钮。该按钮应该有一个运行上述if语句的监听器。

答案 2 :(得分:-2)

弄清楚了。

补充说:

true1.addMouseListener(this);

而且:

@Override
public void mouseClicked(MouseEvent e) {
    if (e.getSource() == true1) {
        labelAnswer1.setText("You are CORRECT!");
    }
}

感谢您的帮助!