0

its a maybe a dumb question but i am curious to understand this thing.... The below code works but the one below that doesn't work.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Menu extends JFrame
{

public Menu()
{

JMenuBar menubar = new JMenuBar();
ImageIcon icon = new ImageIcon("exit.png");
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
JMenuItem fileClose = new JMenuItem("Close",icon);
fileClose.setMnemonic(KeyEvent.VK_C);
fileClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{System.exit(0);}
});


file.add(fileClose);
menubar.add(file);
setJMenuBar(menubar);


setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);
}


public static void main (String args[])
{
new Menu();
}


}

The below one doesn't work

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Menu extends JFrame
{

public Menu()
{


setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300,200);
setLocationRelativeTo(null);

JMenuBar menubar = new JMenuBar();
ImageIcon icon = new ImageIcon("exit.png");
JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
JMenuItem fileClose = new JMenuItem("Close",icon);
fileClose.setMnemonic(KeyEvent.VK_C);
fileClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{System.exit(0);}
});


file.add(fileClose);
menubar.add(file);
setJMenuBar(menubar);


}


public static void main (String args[])
{
new Menu();
}


}

I thought Java supports free style coding !!! (that's what it says in my Book)

PS: Please Someone Edit the Tile to suit the question correctly, I am not sure what to put in Title.

2
  • You do see that actual code differs in those examples, right? I mean, beyond style. Commented Aug 10, 2010 at 4:31
  • in the 1st set methods are defined after JmenuBar . while in the 2nd it is written up. I always used to declare at the top for my other codes untill now. PS. I started learning swings this morning about 3 hours before. Commented Aug 10, 2010 at 4:38

1 Answer 1

1

The issue in your 2nd code sample is that you are calling setVisible before you actually add stuff to the GUI. Your saying "Here's some stuff, now show" while in the second one your saying "Show, now here's some stuff"

Fix: Move setVisible to the end of the constructor

Sign up to request clarification or add additional context in comments.

2 Comments

So is it a good practice to always call setVisible at the end . Or should i define it in the main class . Also wat about other set methods like setSize, setLocationRelativeTo, where should i call them ???
You can call setVisible anywhere, as long as its after you've setup the GUI. For clearness I always put it at the end of the constructor. The other methods you mentioned are fine anywhere before setVisible, since they are only used when drawing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.