ForgottenKahz
ForgottenKahz

Reputation: 276

NetBeans GUI design acting inconsistent when I test with the run file

When I select run file to test my code the GUI application's menu bar does not consistently appear. I'm using NetBeans IDE version 7.0.1. I'm testing the code by selecting anywhere within the code, right clicking the mouse and selecting Run File from the resulting menu. The resulting GUI application has the menu bar but when I rerun the application the menu bar is not there but when I rerun it the menu bar appears. Perhaps you can test to see if you are having the same problem when you run the program? Is there some sort of residual memory thing going on that I need to purge before I run the program? Here's the code:

public static void main(String[] args) {
    JFrame main = new JFrame("Main");
    main.setVisible(true);
    main.setSize(600, 600);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints contraints = new GridBagConstraints();
    main.getContentPane().add(panel, BorderLayout.NORTH);
    contraints.gridx = 0;
    contraints.gridy = 0;
    contraints.insets = new Insets(0,0,0,0);

    //Adding labels and buttons
    JLabel enterUserName = new JLabel("Enter User Name:");
    contraints.gridx = 0;
    contraints.gridy = 1;
    contraints.insets = new Insets(1, 1, 1, 1);
    panel.add(enterUserName, contraints);
    JLabel enterPassWord = new JLabel("Enter Password:");
    contraints.gridx = 0;
    contraints.gridy = 2;
    contraints.insets = new Insets(1, 1, 1, 1);
    panel.add(enterPassWord, contraints);
    JButton logInButton = new JButton("Login");
    contraints.gridx = 0;
    contraints.gridy = 3;
    contraints.insets = new Insets(1, 1, 1, 1);
    panel.add(logInButton, contraints);
    logInButton.addActionListener(new helpAbout());


    //Adding menu bar and menubar items
    JMenuBar menuBar = new JMenuBar();
    main.setJMenuBar(menuBar);
    contraints.gridx = 0;
    contraints.gridy = 0;
    contraints.insets = new Insets(0, 0, 0, 0);
    JMenu file = new JMenu("File");
    menuBar.add(file, contraints);
    JMenu help = new JMenu("Help");
    menuBar.add(help, contraints);

    //Adding menu bar functionality
    JMenuItem exit = new JMenuItem("Exit");
    file.add(exit);
    exit.addActionListener(new fileExit());
    JMenuItem about = new JMenuItem("About");
    help.add(about);
    about.addActionListener(new helpAbout());
}

static class helpAbout implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        JFrame test = new JFrame("About");
        test.setVisible(true);
        test.setSize(200, 200);
        JLabel label2 = new JLabel("About");
        JPanel panel2 = new JPanel();
        test.add(panel2);
        panel2.add(label2);
    }
}

static class fileExit implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
}

Upvotes: 0

Views: 825

Answers (2)

mKorbel
mKorbel

Reputation: 109815

Code line main.setVisible(true); must be last code line into main method

Upvotes: 1

BillRobertson42
BillRobertson42

Reputation: 12883

Call setVisible(true) after building the frame.

If you add layout/controls to a visible form, which you might want to do in other situations, you need to call revalidate() on the frame/panel.

Upvotes: 2

Related Questions