gpt4 book ai didi

java - 尝试获取 Radio Button.is Selected() 以设置标签的文本。不工作

转载 作者:行者123 更新时间:2023-11-29 05:05:24 25 4
gpt4 key购买 nike

这是我的代码:

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

不太确定我做错了什么。可能是晚上太晚了,哈哈。任何帮助,将不胜感激。谢谢并致以最诚挚的问候!

最佳答案

与大多数 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;
}

}

}

关于java - 尝试获取 Radio Button.is Selected() 以设置标签的文本。不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30542145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com