繁体   English   中英

使用if语句和单选按钮在Jlabel上显示文本

[英]Display text on Jlabel with if statements and radio buttons

我遵循了一些有关如何使它工作的不同教程,但似乎无法获得更新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);
        }
    }
}

首先看一下如何编写动作监听器

为了让您的按钮提供通知,您必须在其中注册一个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;

这很好,但是在构造函数中,您正在重新声明它们。

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

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

您应该使用类似...

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

您需要将事件处理程序方法( actionPerformed )连接到每个单选按钮。 为此,您需要向每个单选按钮注册一个ActionListener 最方便的方法是让您的主类实现ActionListener接口,然后在每个单选按钮上调用addActionListener this的引用

还要注意,您的事件处理程序方法当前会给您带来问题,因为您实际上从未设置过类级别的成员(字段)( rdbtnNewRadioButton_3等)。 您实际上是在构造函数中使用与“隐藏”类级别字段相同的名称创建新的局部变量,当它们超出范围时,它们将变得不可访问,从而使类级别变量为空。

您的构造函数代码应如下所示:

rdbtnNewRadioButton_3 = new JRadioButton("");

代替

JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("");

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM