java - 使用 if 语句和单选按钮在 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.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);
        }
    }
}

最佳答案

首先查看 How to Write an Action Listeners

为了让您的按钮提供通知,您必须在其中注册一个 ActionListener

为了让您的按钮调用 actionPerformed 方法,您需要实现 ActionListener 接口(interface)...

public class CoinApp extends JFrame implements ActionListener {
    //...
    @Override
    public void actionPerformed(ActionEvent evt) {

您应该将 @Override 注释添加到您认为要从父类或接口(interface)重写的方法中,如果您做错了什么(例如拼写错误),它会给您一个编译器警告.

现在您可以使用按钮注册 ActionListener...

rdbtnNewRadioButton_3.addActioinListener(this);

已更新...

另外,请注意,您正在隐藏变量...

我的意思是,您将变量声明为实例/类变量...

private JRadioButton rdbtnNewRadioButton_3, rdbtnNewRadioButton, rdbtnNewRadioButton_1, rdbtnNewRadioButton_2;

这很好,但是在你的构造函数中,你正在重新声明它们......

public CoinApp() {
    //...
    JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("");

这意味着当您在程序中的其他位置引用 rdbtnNewRadioButton_3 时,它将为 null

你应该使用类似...的东西

public CoinApp() {
    //...
    rdbtnNewRadioButton_3 = new JRadioButton("");

关于java - 使用 if 语句和单选按钮在 Jlabel 上显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25174498/

相关文章:

java - 为什么注释处理在 maven 构建期间运行多次

java - 我可以将以下代码转换为使用泛型吗?

java - 如何为TableView设置水平滚动条?

java - 我怎样才能关闭一个FrameView java

java - JFrame显示不正确

java - QuickBooks SDK 可以与 WebConnector 配合使用吗?

java - 尝试使用 MouseEvent 更改 JLabel

java - 如何在组合框中显示不带扩展名的文件名?

java - JFrame 对象准备好进行垃圾回收的条件

java - JComponents 没有显示图片背景?