0

Hi All
I am trying to resize the button which is placed in a cell of GridBagLayout but FAILED...:(

Like I want same size of every button (Please see the attached file for what I am getting as Output of my Program which is as follow -

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.*;

public class Main {

  public static void main(String[] args) {
    Frame f = new Frame("Demonstrates the use of weightx, weighty constraints");
    Panel p = new Panel();

    p.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    p.setLayout(new GridBagLayout());
    c = new GridBagConstraints();
    c.insets = new Insets(20, 2, 20, 2);
    c.weighty = 10.0;
    c.weightx = 10.0;
    c.gridx = 0;
    c.gridy = 0;
    p.add(new Button("Java"), c);
    c.gridx = 1;
    p.add(new Button("Source"), c);
    c.gridx = 0;
    c.gridy = 1;
    p.add(new Button("and"), c);
    c.gridx = 1;
    p.add(new Button("Support."), c);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    f.addWindowListener(wndCloser);

    f.add(p);
    f.setSize(600, 200);
    f.setVisible(true);
  }
}
Attachments screenshot.JPG 65.22 KB
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by quuba
0
package demo;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.*;
//http://www.daniweb.com/forums/thread220873.html
public class NewClass {

    public static void main(String[] args) {
        class SButton extends Button {

            SButton(String name) {
                super(name);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(100, 40);
            }
        }
        Frame f = new Frame("Demonstrates the use of weightx, weighty constraints");
        Panel p = new Panel();

        p.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        p.setLayout(new GridBagLayout());
        c = new GridBagConstraints();
        c.insets = new Insets(20, 2, 20, 2);
        c.weighty = 10.0;
        c.weightx = 10.0;
        c.gridx = 0;
        c.gridy = 0;
        p.add(new SButton("Java"), c);
        c.gridx = 1;
        p.add(new SButton("Source"), c);
        c.gridx = 0;
        c.gridy = 1;
        p.add(new SButton("and"), c);
        c.gridx = 1;
        p.add(new SButton("Support."), c);

        WindowListener wndCloser = new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };
        f.addWindowListener(wndCloser);

        f.add(p);
        f.setSize(600, 200);
        f.setVisible(true);

    }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.