jLabel不会显示 [英] jLabel won't show
问题描述
我有点困惑,我有一个用Netbeans制作的jFrame.此jFrame有一个jLabel,从一开始就将其设置为setVisible(false);
.每当调用特定方法时,我便将jLabel设置为setVisible(true);
,然后使用计时器在2秒后再次将其设置为false
.显然,它不起作用,我无法弄清楚原因.我知道repaint();方法,但可以弄清楚如何使它起作用.
I am slightly confused, I have a jFrame of which I have made in Netbeans. This jFrame has a jLabel, of which is set to setVisible(false);
from the beginning. Whenever a specific method is called, I then set the jLabel to setVisible(true);
and then use a timer to set it to false
again after 2 seconds. Apparently it won't work and I am unable to figure out why. I am aware of the repaint(); method, but can figure out how to make that work either.
我知道设置可见性的实际方法是调用的,因为我已将其设置为以当前状态打印一行.
I know the actual method for setting the visibility is called, as I have set it to print a line with the current state, which it does.
我的实际代码是下面的代码.
My actual code is the one below.
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()的方式;方法.
The code below here is how I tried to use the repaint(); method.
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
);
}
推荐答案
我认为您的问题主要出在您使用 javax.swing.Timer
,可能您正在阻止
I think your problem lies mainly in you using a java.util.Timer
instead of a javax.swing.Timer
and probably you're blocking the Event Dispatch Thread (EDT).
您可以尝试将此代码与您的代码进行比较,我也看不到您要将JLabel
添加到框架中的位置.
You could try this code and compare it with yours, I also don't see where you're adding your JLabel
to your frame.
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秒钟的时间...
The below image is produced by the above code, however because of the time I recorded the GIF it looks really fast instead of taking 2 seconds as it should be...
这篇关于jLabel不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!