简体   繁体   English

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

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

I followed a few different tutorials on how to get this working and I just can't seem to get my buttons to update the JLabel. 我遵循了一些有关如何使它工作的不同教程,但似乎无法获得更新JLabel的按钮。 Can you tell me which part is incorrect and lead me in the right path of what to fix. 您能告诉我哪一部分不正确,并引导我正确地解决问题。 This has been plaguing me for hours. 这困扰了我好几个小时。

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

Start by taking a look at How to Write an Action Listeners 首先看一下如何编写动作监听器

In order for your buttons to provide notification, you must register an ActionListener within them. 为了让您的按钮提供通知,您必须在其中注册一个ActionListener

In order for your buttons to call your actionPerformed method however, you need to implement the ActionListener interface... 为了使按钮能够调用actionPerformed方法,您需要实现ActionListener接口。

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

You should add the @Override annotation to methods you believe you are overriding from parent classes or interfaces, it will give you a compiler warning if you've done something wrong, like misspelt it for example. 您应该将@Override注释添加到您认为从父类或接口覆盖的方法中,如果您做错了某些事情(例如,错误地将其遗忘),它将向您发出编译器警告。

Now you can register the ActionListener with your buttons... 现在,您可以使用按钮注册ActionListener了。

rdbtnNewRadioButton_3.addActioinListener(this);

Updated... 更新...

Also, beware, you are shadowing your variables... 另外,请注意,您正在隐藏变量...

What I mean by this is, you are declaring your variables as instance/class variables... 我的意思是,您将变量声明为实例/类变量...

private JRadioButton rdbtnNewRadioButton_3, rdbtnNewRadioButton, rdbtnNewRadioButton_1, rdbtnNewRadioButton_2;

Which is good, but in your constructor, you are re-declaring them... 这很好,但是在构造函数中,您正在重新声明它们。

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

This means when you go to reference rdbtnNewRadioButton_3 somewhere else in your program, it will be null . 这意味着当您在程序的其他位置引用rdbtnNewRadioButton_3 ,它将为null

You should be using something like... 您应该使用类似...

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

You need to hook up your event handler method ( actionPerformed ) to each of the radio buttons. 您需要将事件处理程序方法( actionPerformed )连接到每个单选按钮。 To do this you need to register an ActionListener to each radio button. 为此,您需要向每个单选按钮注册一个ActionListener The most convenient way for you to do this is to have your main class implement the ActionListener interface, and then call addActionListener on each radio button a reference to this 最方便的方法是让您的主类实现ActionListener接口,然后在每个单选按钮上调用addActionListener this的引用

Also note that your event handler method will currently cause you problems because you are never actually setting the class level members (fields) ( rdbtnNewRadioButton_3 etc). 还要注意,您的事件处理程序方法当前会给您带来问题,因为您实际上从未设置过类级别的成员(字段)( rdbtnNewRadioButton_3等)。 You are actually creating new local variables in your constructor with the same name that 'hide' the class level fields, which then become inaccessible when they fall out of scope, leaving the class level variables null. 您实际上是在构造函数中使用与“隐藏”类级别字段相同的名称创建新的局部变量,当它们超出范围时,它们将变得不可访问,从而使类级别变量为空。

Your constructor code should look like this: 您的构造函数代码应如下所示:

rdbtnNewRadioButton_3 = new JRadioButton("");

Instead of 代替

JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("");

Good luck! 祝好运!

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

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