jLabel 不会显示

jLabel won't show

我有点困惑,我有一个用 Netbeans 制作的 jFrame。这个jFrame有一个jLabel,从一开始就设置为setVisible(false);。每当调用特定方法时,我都会将 jLabel 设置为 setVisible(true);,然后使用计时器在 2 秒后再次将其设置为 false。显然它不会工作,我无法弄清楚为什么。我知道 repaint();方法,但也可以弄清楚如何使它起作用。

我知道调用了设置可见性的实际方法,因为我已将其设置为打印当前状态的一行,它确实如此。

我的实际代码如下。

public JFram() {
        initComponents();
        setResizable(false);
        jLabel2.setVisible(false);
    }

static void tesMethod() {
            try {
         //function that does something
            } finally {
                new JFram().showHide(); //call function which is supposed to change the vissibility of jLabel
            }
    }

    void showHide() {
            jLabel2.setVisible(true);
            System.out.println("reached show");
            new java.util.Timer().schedule( 
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                     jLabel2.setVisible(false);
                     System.out.println("reached timer");
                    }
                 }, 
             2000 
            );
    }

下面的代码是我尝试使用 repaint() 的方式;方法。

void showHide() {
            jLabel2.setVisible(true);
            jLabel2.repaint();
            System.out.println("reached show");
            new java.util.Timer().schedule( 
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                     jLabel2.setVisible(false);
                     jLabel2.repaint();
                     System.out.println("reached timer");
                    }
                 }, 
             2000 
            );
    }

可能是排版问题。 当您在任何布局计算发生之前将 resizable 设置为 false 时,标签在第一个布局时被忽略(因为不可见)。 你可以试试 revalidate().

我觉得你的问题主要在于你使用了java.util.Timer instead of a javax.swing.Timer and probably you're blocking the Event Dispatch Thread (EDT).

您可以尝试此代码并将其与您的代码进行比较,我也看不到您将 JLabel 添加到框架的位置。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class ShyLabel {

    private JFrame frame;
    private JLabel label;
    private Timer timer;
    private boolean isVisible;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ShyLabel().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        String labelText = "I'm a shy label that hides every 2 seconds";

        isVisible = true;
        frame = new JFrame(getClass().getSimpleName());
        label = new JLabel(labelText);
        timer = new Timer(2000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText(isVisible ? "" : labelText);
                isVisible = !isVisible;
            }
        });

        timer.setInitialDelay(2000);
        timer.start();

        frame.add(label);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

下面的图片是由上面的代码生成的,但是由于我录制 GIF 的时间,它看起来非常快,而不是应该花费 2 秒...