Java JFrame is very strange
When I am running this, JLabel is not visible, but when I resize window (with mouse) JLabel is showed. Why?
import javax.swing.*;
import java.awt.*;
public class FrmTaoLogin extends JFrame {
private JPanel pnlLeft = new JPanel();
public FrmTaoLogin() {
super()开发者_如何学运维;
pnlLeft.setBorder(BorderFactory.createEtchedBorder());
pnlLeft.add(new JLabel("test1"));
getContentPane().add(pnlLeft,BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(200, 200);
}
public static void main(String[] args) {
FrmTaoLogin FrmLogin = new FrmTaoLogin();
FrmLogin.setVisible(true);
}
}
IIRC, this happens when you don't call Frame.pack(). It should work if you call 'pack()' as the last line of the constructor.
I suspect that the problem here may have to do with trying to build and show your GUI components outside of the Swing thread.
What if you change main()
to invoke your GUI code on the Swing thread, like this?
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
FrmTaoLogin FrmLogin = new FrmTaoLogin();
FrmLogin.setVisible(true);
}
});
}
This look like some of the L&F bugs in older Java VMs on newer OS. For example on Windows 7 the most problems are solved first with 1.6.0_17. You should start your program with a console. If you see some stacktraces in the event thread then it is a problem of an L&F bug.
Thanx to all, problem resolved. I change Windows theme and all working fine. I think that's Windows Aero and my NVIDIA GeForce FX5500 problem. This card official not working with windows Aero.
精彩评论