Results 1 to 4 of 4
  1. #1
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default 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);			
    		}
    	}

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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?

  3. #3
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    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);
    			
    					
    		}
    	}

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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

  1. Layout Manager
    By globo in forum New To Java
    Replies: 8
    Last Post: 11-01-2010, 01:19 AM
  2. Replies: 3
    Last Post: 01-11-2010, 05:34 PM
  3. Layout manager
    By Nicholas Jordan in forum Java Applets
    Replies: 2
    Last Post: 11-24-2008, 04:02 PM
  4. Layout Manager of JApplet
    By Peter_APIIT in forum Java Applets
    Replies: 10
    Last Post: 07-19-2008, 06:37 AM
  5. MiG Layout Manager 2.4
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-16-2007, 06:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •