繁体   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