簡體   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