I am trying to write a simple desktop application for managing accounts. I am in the beginning phase.
I am running it in my old computer and I sometimes get strange behavior.
Java Version
java version “1.6.0_05”
Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)
My computer configuration
Microsoft Windows XP SP2
Intel(R) Celeron(R) CPU 2.53GHz 1.96GHz 736MB of RAM
I get strange behaviour. When I compile my program and the jframe loads, i sometimes get menu and sometimes I don’t get menu as shown in the figure. Also when I try to resize my jframe, jframe shows menu.
My code
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.AWTException;
public class Main {
public static void main(String[] args) {
new Login();
}
}
class Login extends JFrame{
private int height=450;
private int width=300;
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
JTextArea textArea1, textArea2;
public Login()
{
initWindow();
initMenu();
}
private void initWindow()
{
setVisible(true);
setSize(this.height, this.width);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit toolkit = Toolkit.getDefaultToolkit ();
Dimension dim = toolkit.getScreenSize();
setLocation((dim.width-this.width)/2, (dim.height-this.height)/2);
Image image = toolkit.createImage("account.gif");
setIconImage(image);
}
private void initMenu()
{
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(helpMenu);
JMenuItem configureAction = new JMenuItem("Configure");
JMenuItem exitAction = new JMenuItem("Exit");
fileMenu.add(configureAction);
fileMenu.add(exitAction);
JMenuItem helpAction = new JMenuItem("Help");
JMenuItem aboutAction = new JMenuItem("About");
helpMenu.add(helpAction);
helpMenu.add(aboutAction);
}
}
Window with no menu

Window with menu

Any suggestion to improve code with be highly appreciated.
Thank you
Don’t call
setVisible(true)until the window is completely built, ie. do that statement as the last thing you do with the window.Once the window is visible, any changes you make to the window must be done on the GUI thread.