Results 1 to 4 of 4
Thread: Simple GUI layout manager
- 11-16-2010, 01:30 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Simple GUI layout manager
I am trying to get this done:
I would like to know what I am missing
My code so far:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Layoutdemo2 { public static void main(String [] args) { MyFrame frame = new MyFrame("Alan Window"); frame.setSize(400, 300); frame.setLocation(100, 75); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class MyFrame extends JFrame { JTextField txt1 = new JTextField("One"); JTextField txt2 = new JTextField("Two"); JTextField txt3 = new JTextField("Three"); JPanel txtpan1= new JPanel(); JButton btn1 = new JButton("btn1"); JButton btn2 = new JButton("btn2"); JButton btn3 = new JButton("btn3"); JButton btn4 = new JButton("btn4"); JPanel txtpan2 = new JPanel(); JPanel btnpan = new JPanel(); public MyFrame(String s) { super(s); setLayout (new GridLayout (4, 2)); txtpan1.setLayout (new GridLayout (1,0)); //txtpan1.add(txt1); add(txt1, BorderLayout.NORTH); btnpan.setLayout ( new GridLayout (2, 2)); btnpan.add(btn1); btnpan.add(btn2); btnpan.add(btn3); btnpan.add(btn4); add(btnpan); txtpan2.setLayout (new GridLayout (2, 2)); txtpan2.add(txt2); txtpan2.add(txt3); add(txtpan2); } }
- 11-16-2010, 01:51 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
I would like to know what I am missing
Have you run your application? That's the best way to see if it does what you expect. If you get some unintended runtime behaviour that you don't understand, describe that behaviour (and describe what you intended if it is other than the visual appearance you posted.)
A couple of things: At the start of the MyFrame constructor you set the layout of the content pane to a 4x2 grid layout. But a few lines you later you add txt1 using the argument BorderLayout.NORTH. It doesn't really make any sense to use constants from the BorderLayout class with the GridLayout class like that. What did you expect that argument to do?
At the end of that constructor you set another 2x2 layout on the txtpan2 panel. Is there some reason you make it 2x2 when you are only adding 2 text fields?
- 11-16-2010, 02:00 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 61
- Rep Power
- 0
Ok I corrected some errors based on your statement. Everything is placed correctly the problem now is with buttons and textfields sizes.
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Layoutdemo2 { public static void main(String [] args) { MyFrame frame = new MyFrame("Alan Window"); frame.setSize(400, 300); frame.setLocation(100, 75); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class MyFrame extends JFrame { JTextField txt1 = new JTextField("One", 20); JTextField txt2 = new JTextField("Two"); JTextField txt3 = new JTextField("Three"); JPanel txtpan1= new JPanel(); JButton btn1 = new JButton("btn1"); JButton btn2 = new JButton("btn2"); JButton btn3 = new JButton("btn3"); JButton btn4 = new JButton("btn4"); JPanel txtpan2 = new JPanel(); JPanel btnpan = new JPanel(); public MyFrame(String s) { super(s); setLayout(new FlowLayout()); setLayout (new GridLayout (4, 2)); add(txt1); btnpan.setLayout ( new GridLayout (2, 2)); btnpan.add(btn1); btnpan.add(btn2); btnpan.add(btn3); btnpan.add(btn4); add(btnpan); txtpan2.setLayout(new GridLayout (1, 2)); txtpan2.add(txt2); txtpan2.add(txt3); add(txtpan2); } }
- 11-16-2010, 02:17 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Having two setLayout() statements like that is redundant. Only the second will actually do anything.
According to the GridLayout API docs "The container is divided into equal-sized rectangles, and one component is placed in each rectangle." But if you look at the picture you posted, you will see that the things added to the content pane (components or panels) are not the same size. GridLayout seems inappropriate.
To my eye you picture looks like a BorderLayout with a text field positioned north and a 3x2 panel placed in the center.
Similar Threads
-
Layout Manager
By globo in forum New To JavaReplies: 8Last Post: 11-01-2010, 01:19 AM -
How does the programmer invoke a different layout manager
By nafiro in forum AWT / SwingReplies: 3Last Post: 01-11-2010, 05:34 PM -
Layout manager
By Nicholas Jordan in forum Java AppletsReplies: 2Last Post: 11-24-2008, 04:02 PM -
Layout Manager of JApplet
By Peter_APIIT in forum Java AppletsReplies: 10Last Post: 07-19-2008, 06:37 AM -
MiG Layout Manager 2.4
By levent in forum Java SoftwareReplies: 0Last Post: 05-16-2007, 06:11 AM
Bookmarks