简体   繁体   中英

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. 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.

In order for your buttons to call your actionPerformed method however, you need to implement the ActionListener interface...

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.

Now you can register the ActionListener with your buttons...

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 .

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. To do this you need to register an ActionListener to each radio button. 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

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). 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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