java使用if语句和单选按钮在Jlabel上显示文本
我遵循了一些不同的教程,介绍了如何使其工作,但我似乎无法让我的按钮更新JLabel。你能告诉我哪一部分不正确,并引导我找到正确的解决方法吗。这已经困扰了我好几个小时了
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
public class CoinApp extends JFrame {
private JLabel label;
private JRadioButton rdbtnNewRadioButton_3, rdbtnNewRadioButton, rdbtnNewRadioButton_1, rdbtnNewRadioButton_2;
public CoinApp() {
setBounds(50, 50, 500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
ButtonGroup buttonGroup = new ButtonGroup();
JPanel panel = new JPanel();
getContentPane().add(panel);
JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("");
buttonGroup.add(rdbtnNewRadioButton_3);
rdbtnNewRadioButton_3.setIcon(new ImageIcon(getClass().getResource("UsCent.png")));
panel.add(rdbtnNewRadioButton_3);
JRadioButton rdbtnNewRadioButton = new JRadioButton("");
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setIcon(new ImageIcon(getClass().getResource("UsNickel.png")));
panel.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("");
buttonGroup.add(rdbtnNewRadioButton_1);
rdbtnNewRadioButton_1.setIcon(new ImageIcon(getClass().getResource("UsDime.png")));
panel.add(rdbtnNewRadioButton_1);
JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("");
buttonGroup.add(rdbtnNewRadioButton_2);
rdbtnNewRadioButton_2.setVerticalAlignment(SwingConstants.TOP);
rdbtnNewRadioButton_2.setIcon(new ImageIcon(getClass().getResource("UsQuarter.png")));
panel.add(rdbtnNewRadioButton_2);
rdbtnNewRadioButton_3.setSelected(true);
label = new JLabel("CENT w:2.5 d:19.1", JLabel.CENTER);
add(label);
}
**
public void actionPerformed(ActionEvent evt) {
if ( rdbtnNewRadioButton_3.isSelected() ) {
label.setText("CENT w:2.5 d:19.1");
}
else if ( rdbtnNewRadioButton.isSelected() ) {
label.setText("NICKEL w:5.0 d:21.2");
}
else if ( rdbtnNewRadioButton_1.isSelected() ) {
label.setText("DIME w:2.3 d:17.9");
}
else if ( rdbtnNewRadioButton_2.isSelected() ) {
label.setText("QUARTER w:5.7 d:24.3");
}
} // end actionPerformed()
**
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CoinApp frame = new CoinApp();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
for( Coin el : Coin.values())
{
System.out.println(el);
}
}
}
# 1 楼答案
您需要将事件处理程序方法(
actionPerformed
)连接到每个单选按钮。要做到这一点,你需要在每个单选按钮上注册一个ActionListener
。最方便的方法是让主类实现ActionListener
接口,然后在每个单选按钮上调用addActionListener
对this
的引用还请注意,事件处理程序方法当前会给您带来问题,因为您从来没有实际设置类级别的成员(字段)(
rdbtnNewRadioButton_3
等)。实际上,您正在构造函数中创建新的局部变量,其名称与“隐藏”类级字段的名称相同,这些字段在超出范围时变得不可访问,从而使类级变量为空您的构造函数代码应该如下所示:
而不是
祝你好运
# 2 楼答案
首先看一下How to Write an Action Listeners
为了让按钮提供通知,必须在按钮中注册
ActionListener
但是,为了让按钮调用
actionPerformed
方法,您需要实现ActionListener
接口您应该将
@Override
注释添加到您认为要从父类或接口重写的方法中,如果您做了一些错误的事情,例如拼写错误,它将向您发出编译器警告现在你可以用你的按钮注册
ActionListener
更新了
另外,要小心,你在隐藏你的变量
我的意思是,您将变量声明为实例/类变量
这很好,但在构造函数中,您正在声明它们
这意味着当你在程序的其他地方引用
rdbtnNewRadioButton_3
时,它将是null
你应该使用像