【问题标题】:Jlabel will not update with setText from ActionListener?Jlabel 不会使用来自 ActionListener 的 setText 进行更新?
【发布时间】:2015-11-05 20:05:06
【问题描述】:

每次单击“加载”J 按钮时,我都会尝试更新一个空的 Jlabel。我在 Jbutton 中添加了一个 actionlistener,但由于某种原因,标签不会更新或使用 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);

好像没用。

似乎是什么问题?有没有办法让标签每秒钟左右自动更新一次,有效地完全消除对按钮的需求?

【问题讨论】:

  • 听起来你在隐藏你的变量,但可能使用null 布局并没有帮助。考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个示例,它突出了您遇到的问题。这将导致更少的混乱和更好的响应
  • new Font("Arial", Font.PLAIN, 11) 对于编译时检查和跨平台健壮性,最好是:new Font(Font.SANS_SERIF, Font.PLAIN, 11)

标签: java swing jlabel layout-manager null-layout-manager


【解决方案1】:

对我来说很好......

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

    }

}

考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将减少混乱并获得更好的响应

【讨论】:

  • 嗯,奇怪......我几乎有完全相同的东西。我的加载按钮与标签位于不同的 JPanel 中是否会有所不同?
  • 很奇怪。我想我将不得不重写程序,我正在努力解决这个问题。感谢您的帮助。
  • “我也有几乎一模一样的东西。” 不。不使用布局是此代码的“英里”。 Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作,在不同的语言环境中使用不同的 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多