-
JFrame issues
Hi guys,
I am new to Java and I am trying to build a window-based application.
I have some idea of how to do this, however I have run into a few problems. Here is the frame section of my code, below that I will post my concerns:
//Method that creates a blank frame
public static void Form()
{
//Code that creates a blank frame
JFrame frame = new JFrame("Wages");
frame.setResizable(true);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
JPanel panel = new JPanel();
frame.add(panel);
JTextArea jat = new JTextArea("Test",10,20);
JLabel label1 = new JLabel("Label 1");
JButton button1 = new JButton("Button 1");
panel.add(label1);
panel.add(button1);
panel.add(jat);
}
//Main method
public static void main(String[] args)throws FileNotFoundException
{
/*I use a main method because the frame is
* not the only method that I am using
*/
Form();
}
Core issues:
1. When I run the program, I have to resize (either maximize or minimize) the window in order for it to show the contents (i.e. the text area, button, and the label).
2. Whenever I do try to change the location of any of my panel items (eg. jat.setLocation(20, 20);), they still appear in the same location as if I did not set the location to begin with.
If any of my declarations or method uses seem elementary, remember that I am still a newbie.
I would appreciate any help that you all can offer.
Regards,
Brent
-
call frame.pack() before calling frame.setVisible(true).
Because you need to learn about LayoutManagers. JPanel uses a FlowLayout by default, and the contentPane of JFrame has, per default, a BorderLayout.
See this and this and this