Java 使用if语句和单选按钮在Jlabel上显示文本
我遵循了一些不同的教程,介绍了如何使其工作,但我似乎无法让我的按钮更新JLabel。你能告诉我哪一部分不正确,并引导我找到正确的解决方法吗。这已经困扰了我好几个小时了Java 使用if语句和单选按钮在Jlabel上显示文本,java,swing,jframe,jlabel,Java,Swing,Jframe,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
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);
}
}
}
首先看一看 为了让按钮提供通知,您必须在按钮中注册一个
ActionListener
但是,为了让按钮调用actionPerformed
方法,您需要实现ActionListener
界面
public class CoinApp extends JFrame implements ActionListener {
//...
@Override
public void actionPerformed(ActionEvent evt) {
您应该将@Override
注释添加到您认为要从父类或接口重写的方法中,如果您做了错误的事情,例如拼写错误,它将向您发出编译器警告
现在,您可以使用按钮注册ActionListener
rdbtnNewRadioButton_3.addActioinListener(this);
已更新…
另外,请注意,您正在隐藏您的变量
private JRadioButton rdbtnNewRadioButton_3, rdbtnNewRadioButton, rdbtnNewRadioButton_1, rdbtnNewRadioButton_2;
我的意思是,您将变量声明为实例/类变量
private JRadioButton rdbtnNewRadioButton_3, rdbtnNewRadioButton, rdbtnNewRadioButton_1, rdbtnNewRadioButton_2;
这很好,但是在构造函数中,您正在声明它们
public CoinApp() {
//...
JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("");
这意味着当您在程序中的其他地方转到referencerdbtnNewRadioButton_3
时,它将为null
你应该使用像
public CoinApp() {
//...
rdbtnNewRadioButton_3 = new JRadioButton("");
首先看一看 为了让按钮提供通知,您必须在按钮中注册一个
ActionListener
但是,为了让按钮调用actionPerformed
方法,您需要实现ActionListener
界面
public class CoinApp extends JFrame implements ActionListener {
//...
@Override
public void actionPerformed(ActionEvent evt) {
您应该将@Override
注释添加到您认为要从父类或接口重写的方法中,如果您做了错误的事情,例如拼写错误,它将向您发出编译器警告
现在,您可以使用按钮注册ActionListener
rdbtnNewRadioButton_3.addActioinListener(this);
已更新…
另外,请注意,您正在隐藏您的变量
private JRadioButton rdbtnNewRadioButton_3, rdbtnNewRadioButton, rdbtnNewRadioButton_1, rdbtnNewRadioButton_2;
我的意思是,您将变量声明为实例/类变量
private JRadioButton rdbtnNewRadioButton_3, rdbtnNewRadioButton, rdbtnNewRadioButton_1, rdbtnNewRadioButton_2;
这很好,但是在构造函数中,您正在声明它们
public CoinApp() {
//...
JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("");
这意味着当您在程序中的其他地方转到referencerdbtnNewRadioButton_3
时,它将为null
你应该使用像
public CoinApp() {
//...
rdbtnNewRadioButton_3 = new JRadioButton("");
您需要将事件处理程序方法(
actionPerformed
)连接到每个单选按钮。为此,您需要向每个单选按钮注册一个ActionListener
。最方便的方法是让主类实现ActionListener
接口,然后在每个单选按钮上调用addActionListener
以引用this
还请注意,事件处理程序方法当前会给您带来问题,因为您从未实际设置类级成员(字段)(rdbtnNewRadioButton_3
etc)。实际上,您正在构造函数中创建新的局部变量,其名称与“隐藏”类级字段的名称相同,这些字段在超出范围时变得不可访问,从而使类级变量为空
您的构造函数代码应该如下所示:
rdbtnNewRadioButton_3 = new JRadioButton("");
而不是
JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("");
祝你好运 您需要将事件处理程序方法(
actionPerformed
)连接到每个单选按钮。为此,您需要向每个单选按钮注册一个ActionListener
。最方便的方法是让主类实现ActionListener
接口,然后在每个单选按钮上调用addActionListener
以引用this
还请注意,事件处理程序方法当前会给您带来问题,因为您从未实际设置类级成员(字段)(rdbtnNewRadioButton_3
etc)。实际上,您正在构造函数中创建新的局部变量,其名称与“隐藏”类级字段的名称相同,这些字段在超出范围时变得不可访问,从而使类级变量为空
您的构造函数代码应该如下所示:
rdbtnNewRadioButton_3 = new JRadioButton("");
而不是
JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("");
祝你好运