Results 1 to 7 of 7
  1. #1
    vishalrimt is offline Member
    Join Date
    Aug 2010
    Posts
    15
    Rep Power
    0

    Default Problem regarding "GridLayout"

    Hey all,

    plz tell me that how to use the "GridLayout" class in the following code...
    i want the size of textarea to be set by me...
    but it is not been done here....:(


    XML Code:
    import java.awt.*;
    import java.awt.event.*;
    
    class Demo implements ActionListener
    {
    
    TextField t;
    
    Demo(TextField t)
    {
    this.t=t;
    }
    
    public void actionPerformed(ActionEvent a)
    {
    String s=a.getActionCommand();
    t.setText(s);
    }
    }
    
    
    
    class Cal
    {
    
    Cal()
    {
    
    	int i;
    	int c=10;
    	Frame f=new Frame();
    	f.setSize(400,400);
    	
    	Panel p=new Panel();
    	p.setLayout(new GridLayout(3,2));
    	
    	Button b[]=new Button[10];
    
    	TextField t=new TextField();
    	Demo d=new Demo(t);
    	
    	for(i=0;i<b.length;i++)
    	{
    		String s=Integer.toString(i);
    		b[i]=new Button(s);
    		b[i].setBounds(i*40,120,30,30);
    		p.add(b[i]);
    	}
    
    
    	t.setBounds(30,40,340,40);
    	p.add(t);
    
    
    	for(i=0;i<b.length;i++)
    	{
    		b[i].addActionListener(d);
    	}
    
    	
    	f.add(p);	
    	f.setVisible(true);
    
    }
    
    
    public static void main(String zx[])
    {
    new Cal();
    
    }
    }


    plzzz do help me...
    plzzz....

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're using setBounds which shouldn't be used at all if you're using layouts. Rather if you want to set the size of the Buttons held by the GridLayout-using container, then set their preferredSize and don't set the size of the application. Also, since your TextArea is separate from your buttons, best if you place your Buttons in one Panel that uses GridLayout and then place this Button Panel in another Panel using another layout such as BorderLayout, and then add the TextArea to the same BorderLayout using Panel. Most important of all, read the tutorials. By the way, why AWT and not Swing?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by vishalrimt View Post
    plzzz
    plzzz....
    Lemmy guess; a wasp! No! A bee! Yes? Am I right?

    kind regards,

    Jos

  4. #4
    vishalrimt is offline Member
    Join Date
    Aug 2010
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You're using setBounds which shouldn't be used at all if you're using layouts. Rather if you want to set the size of the Buttons held by the GridLayout-using container, then set their preferredSize and don't set the size of the application. Also, since your TextArea is separate from your buttons, best if you place your Buttons in one Panel that uses GridLayout and then place this Button Panel in another Panel using another layout such as BorderLayout, and then add the TextArea to the same BorderLayout using Panel. Most important of all, read the tutorials. By the way, why AWT and not Swing?
    Thanks Fubarable...

    i tried it but didnt worked...:(
    plzz tell me how to use both "GridLayout" and this "BorderLayout" in my program..
    nd i am on the learning stage now...:) my teacher told me to use awt first and then try swing... so i am using awt...:)

  5. #5
    vishalrimt is offline Member
    Join Date
    Aug 2010
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Lemmy guess; a wasp! No! A bee! Yes? Am I right?
    sorry Josah... bt i didn't understand what are you trying to say...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by vishalrimt View Post
    Thanks Fubarable...

    i tried it but didnt worked...:(
    "It didn't work" doesn't help us figure out what you're doing wrong. If you attempt to do something and desire our help, you need to show your attempt and describe in detail how it's not working including posting any error messages that you encounter.

    plzz tell me how to use both "GridLayout" and this "BorderLayout" in my program..
    I won't create the code for you, but can guide you to the tutorials that will show you how to use these layouts. They can be found here: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    nd i am on the learning stage now...:) my teacher told me to use awt first and then try swing... so i am using awt...:)
    Most here would say that your teacher is mistaken as there's no need to do AWT first and then go to Swing.

    Please avoid unnecessary and non-standard abbreviations. It's hard enough to interpret questions here and your use of these abbreviations makes it harder still, not to mention for those for whom English is not their primary language.

  7. #7
    vishalrimt is offline Member
    Join Date
    Aug 2010
    Posts
    15
    Rep Power
    0

    Default

    i will keep that in mind:)

    and really thanks for the tutorial link:)

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  3. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •