[英]jLabel won't show
I am slightly confused, I have a jFrame of which I have made in Netbeans. 我有些困惑,我有一个用Netbeans制作的jFrame。 This jFrame has a jLabel, of which is set to
setVisible(false);
此jFrame具有一个jLabel,其jLabel设置为
setVisible(false);
;。 from the beginning. 从一开始就。 Whenever a specific method is called, I then set the jLabel to
setVisible(true);
每当调用特定方法时,我便将jLabel设置为
setVisible(true);
;。 and then use a timer to set it to false
again after 2 seconds. 然后在2秒后使用计时器将其设置为
false
。 Apparently it won't work and I am unable to figure out why. 显然,它不起作用,我无法弄清楚原因。 I am aware of the repaint();
我知道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
);
}
The code below here is how I tried to use the repaint(); 下面的代码是我尝试使用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
);
}
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) . 我认为您的问题主要出在您使用
java.util.Timer
而不是javax.swing.Timer
并且您可能阻塞了事件调度线程(EDT) 。
You could try this code and compare it with yours, I also don't see where you're adding your JLabel
to your frame. 您可以尝试使用此代码并将其与您的代码进行比较,但我也看不到将
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);
}
}
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... 下面的图像是由上面的代码产生的,但是由于我录制GIF的时间看起来非常快,而不是需要花费2秒钟的时间...
May be it is a problem of layout. 可能是布局问题。 As you set resizable to false before any layout calculation occurred, the label was ignored (as invisible) by the time of the first layout.
当您在进行任何布局计算之前将resizable设置为false时,标签在第一个布局时便被忽略(不可见)。 You could try revalidate().
您可以尝试revalidate()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.