Results 1 to 2 of 2
  1. #1
    toby is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Use GridLayout in java

    Hi, I am trying to make a Java Applet and using gridlayout
    Here's the code:

    Java Code:
    setLayout(new GridLayout(2,2));
    ButtonA = new Button("A");
    ButtonB = new Button("B");
    ButtonC = new Button("C");
    ButtonD = new Button("D");
    
    		add(ButtonA,"1");
    		add(ButtonB,"2");
    		add(ButtonC,"3");
    		add(ButtonD,"4");
    Unfortunately, this code makes all the buttons stretch towards the whole screen that's 400x400, what if i want to make it in the middle that's 200x200 on a 400x400 applet.
    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    //  <applet code="GridLayoutTest" width="400" height="400"></applet>
    import java.applet.Applet;
    import java.awt.*;
    
    public class GridLayoutTest extends Applet {
        public void init() {
            Panel center = new Panel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            center.add(new ButtonPanel(), gbc);
            setLayout(new BorderLayout());
            add(center);
        }
    
        private class ButtonPanel extends Panel {
            public ButtonPanel() {
                Button ButtonA = new Button("A");
                Button ButtonB = new Button("B");
                Button ButtonC = new Button("C");
                Button ButtonD = new Button("D");
                setLayout(new GridLayout(2,2));
                add(ButtonA,"1");
                add(ButtonB,"2");
                add(ButtonC,"3");
                add(ButtonD,"4");
            }
    
            public Dimension getPreferredSize() {
                return new Dimension(200,200);
            }
        }
    }

Similar Threads

  1. SWT GridLayout
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:04 AM
  2. GridLayout with animation?
    By tojas in forum AWT / Swing
    Replies: 3
    Last Post: 11-12-2007, 11:16 PM
  3. Help with gaps in GridLayout
    By trill in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:20 PM
  4. Gridlayout
    By Marty in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 12:48 PM

Posting Permissions

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