簡體   English   中英

Jlabel不會使用ActionListener中的setText更新嗎?

[英]Jlabel will not update with setText from ActionListener?

我每次單擊“加載” Jbutton時都試圖更新一個空的Jlabel。 我已經在Jbutton中添加了一個動作偵聽器,但是由於某種原因,標簽只是不會更新,或者不會使用setText(String)方法顯示任何文本。

JLabel stupidLabel = new JLabel();
    stupidLabel.setForeground(SystemColor.infoText);
    stupidLabel.setFont(new Font("Arial", Font.PLAIN, 11));
    stupidLabel.setBounds(71, 46, 167, 14);
    panel.add(stupidLabel);'

JButton load = new JButton("Load");
    load.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

           stupidLabel.setText("Update please");
        }
    });
    load.setFont(new Font("Arial", Font.PLAIN, 11));
    load.setBounds(189, 92, 89, 22);
    contentPane.add(load);

似乎不起作用。

似乎是什么問題? 有沒有辦法使標簽每秒鍾左右自動更新,從而完全消除對按鈕的需要?

對我來說很好...

給我看標簽

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            JLabel stupidLabel = new JLabel(" ");
            stupidLabel.setForeground(SystemColor.infoText);
            stupidLabel.setFont(new Font("Arial", Font.PLAIN, 11));
            add(stupidLabel, gbc);

            JButton load = new JButton("Load");
            load.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    stupidLabel.setText("Update please");
                }
            });
            load.setFont(new Font("Arial", Font.PLAIN, 11));
            add(load, gbc);
        }

    }

}

考慮提供一個可運行的示例來演示您的問題。 這不是代碼轉儲,而是您正在執行的操作的一個示例,突出顯示了您所遇到的問題。 這將減少混亂並改善響應

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM