Example usage for java.awt GridBagLayout GridBagLayout

List of usage examples for java.awt GridBagLayout GridBagLayout

Introduction

In this page you can find the example usage for java.awt GridBagLayout GridBagLayout.

Prototype

public GridBagLayout() 

Source Link

Document

Creates a grid bag layout manager.

Usage

From source file:GridBagWithContaints.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of gridx, gridy,ipadx, ipady and insets constraints");
    JPanel p = new JPanel();

    p.setLayout(new GridBagLayout());
    // creates a constraints object
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2); // insets for all components
    c.gridx = 0; // column 0
    c.gridy = 0; // row 0
    c.ipadx = 5; // increases components width by 10 pixels
    c.ipady = 5; // increases components height by 10 pixels
    p.add(new JButton("Java"), c); // constraints passed in
    c.gridx = 1; // column 1
    // c.gridy = 0; // comment out this for reusing the obj
    c.ipadx = 0; // resets the pad to 0
    c.ipady = 0;/*from   www.  ja  v  a  2  s.co m*/
    p.add(new JButton("Source"), c);
    c.gridx = 0; // column 0
    c.gridy = 1; // row 1
    p.add(new JButton("and"), c);
    c.gridx = 1; // column 1
    p.add(new JButton("Support."), c);

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

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:Main.java

public static void main(String[] args) {
    try {//from ww w .  j  av a 2s. c o m
        bg = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    } catch (Exception ex) {
        System.out.println(ex);
    }

    JPanel tabPanel = new JPanel(new GridBagLayout()) {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 300);
        }
    };
    JPanel buttons = new JPanel(new GridLayout(4, 1, 15, 15));
    buttons.setOpaque(false);
    for (int i = 0; i < 4; i++) {
        buttons.add(new JButton("Button"));
    }
    tabPanel.add(buttons);

    JTabbedPane tabPane = new JTabbedPane();
    tabPane.add("Panel with Bachground", tabPanel);

    JFrame frame = new JFrame();
    frame.setContentPane(tabPane);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setUndecorated(true);// w  w  w .  ja va  2  s.c o m
    frame.setBackground(new Color(0, 0, 0, 0));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new ShadowPane());

    JPanel panel = new JPanel(new GridBagLayout());
    panel.add(new JLabel("Look ma, no hands"));

    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JLabel userLabel = new JLabel("User Name");
    JLabel passLabel = new JLabel("Password");
    JTextField userText = new JTextField(20);
    JPasswordField passText = new JPasswordField(20);
    JButton loginButton = new JButton("Login");

    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.insets = new Insets(4, 4, 4, 4);
    gbc.gridx = 0;//from  www.  j  a  v a  2 s  . co m
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.NONE;
    panel.add(userLabel, gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel.add(userText, gbc);

    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.NONE;
    panel.add(passLabel, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    panel.add(passText, gbc);

    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.CENTER;
    gbc.gridwidth = 2;
    panel.add(loginButton, gbc);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(panel));
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    final JLabel valueLabel = new JLabel(String.valueOf(value));
    JButton decButton = new JButton("-");
    decButton.addActionListener(e -> valueLabel.setText(String.valueOf(--value)));

    JButton incButton = new JButton("+");
    incButton.addActionListener(e -> valueLabel.setText(String.valueOf(++value)));

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;//from  w  w w .ja v  a2s.c  om
    c.gridx = 0;
    c.gridy = 0;
    panel.add(decButton, c);
    c.gridx = 1;
    panel.add(valueLabel, c);
    c.gridx = 2;
    panel.add(incButton, c);

    c.gridy = 1;
    int w = 32;
    for (c.gridx = 0; c.gridx < 3; c.gridx++) {
        panel.add(Box.createHorizontalStrut(w), c);
    }

    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel gui = new JPanel(new BorderLayout());
    gui.setBorder(new EmptyBorder(2, 3, 2, 3));

    JPanel textPanel = new JPanel(new BorderLayout(5, 5));
    textPanel.add(new JScrollPane(new JTextArea("Top Text", 3, 20)), BorderLayout.PAGE_START);
    textPanel.add(new JScrollPane(new JTextArea("Main Text", 10, 10)));
    gui.add(textPanel, BorderLayout.CENTER);

    JPanel buttonCenter = new JPanel(new GridBagLayout());
    buttonCenter.setBorder(new EmptyBorder(5, 5, 5, 5));
    JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 5, 5));
    for (int ii = 1; ii < 6; ii++) {
        buttonPanel.add(new JButton("Button " + ii));
    }//from   w w w  .j a  v  a 2s  . c om

    buttonCenter.add(buttonPanel);

    gui.add(buttonCenter, BorderLayout.LINE_END);

    JFrame f = new JFrame("Demo");
    f.add(gui);

    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    f.setLocationByPlatform(true);

    f.pack();

    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame();
    frame.setResizable(false);// w  w w. j av a 2 s.c om
    removeButtons(frame);
    JPanel panel = new JPanel(new GridBagLayout());
    JButton button = new JButton("Exit");
    panel.add(button, new GridBagConstraints());
    frame.getContentPane().add(panel);
    frame.setSize(400, 300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    button.addActionListener(e -> System.exit(0));
}

From source file:GlassExample.java

/** Construct a Splash screen with the given image */
public static void main(String[] args) {
    JFrame f = new JFrame("GlassPane");

    final JPanel p1 = new JPanel();
    p1.add(new JLabel("GlassPane Example"));
    JButton show = new JButton("Show");
    p1.add(show);//from   w  w  w.j a va 2 s  .c om
    p1.add(new JButton("No-op"));
    f.getContentPane().add(p1);

    final JPanel glass = (JPanel) f.getGlassPane();

    glass.setVisible(true);
    glass.setLayout(new GridBagLayout());
    JButton glassButton = new JButton("Hide");
    glass.add(glassButton);

    f.setSize(150, 80);
    f.setVisible(true);

    boolean debug = false;
    if (debug) {
        System.out.println("Button is " + glassButton);
        System.out.println("GlassPane is " + glass);
    }

    // Add actions to the buttons...

    // show button (re-)shows the glass pane.
    show.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            glass.setVisible(true);
            p1.repaint();
        }
    });
    // hide button hides the Glass Pane to show what's under.
    glassButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            glass.setVisible(false);
            p1.repaint();
        }
    });
}

From source file:TextAcceleratorExample.java

public static void main(String[] args) {
    try {//from  w ww  . ja va  2  s  . co m
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JLabel l;
    JTextField t;
    JButton b;
    JFrame f = new JFrame("Text Accelerator Example");
    Container cp = f.getContentPane();
    cp.setLayout(new GridBagLayout());
    cp.setBackground(UIManager.getColor("control"));
    GridBagConstraints c = new GridBagConstraints();

    c.gridx = 0;
    c.gridy = GridBagConstraints.RELATIVE;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.insets = new Insets(2, 2, 2, 2);
    c.anchor = GridBagConstraints.EAST;

    cp.add(l = new JLabel("Name:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('n');
    cp.add(l = new JLabel("House/Street:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('h');
    cp.add(l = new JLabel("City:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('c');
    cp.add(l = new JLabel("State/County:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('s');
    cp.add(l = new JLabel("Zip/Post code:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('z');
    cp.add(l = new JLabel("Telephone:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('t');
    cp.add(b = new JButton("Clear"), c);
    b.setMnemonic('l');

    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.CENTER;

    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('n');
    c.gridx = 1;
    c.gridy = GridBagConstraints.RELATIVE;
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('h');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('c');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('s');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('z');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('t');
    c.weightx = 0.0;
    c.fill = GridBagConstraints.NONE;
    cp.add(b = new JButton("OK"), c);
    b.setMnemonic('o');

    f.pack();
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            System.exit(0);
        }
    });
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JFrame f = new JFrame();
    f.setTitle("Example2");
    f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
    p1.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "JPanel 1"));
    for (int i = 0; i < NB1; i++)
        p1.add(new JButton("Button " + i));

    JPanel p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));

    p2.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "JPanel 2"));
    for (int i = NB1; i < NB2; i++)
        p2.add(new JButton("Button " + i));

    JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
    p3.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "JPanel 3"));
    for (int i = NB2; i < NB3; i++)
        p3.add(new JButton("Button " + i));

    JPanel global = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL; // added
    gbc.weightx = 1.0f; // added
    gbc.gridy = 0;/*from  w w  w  . j a v  a2  s  . co  m*/
    global.add(p1, gbc);
    gbc.gridy++;
    global.add(p2, gbc);
    gbc.gridy++;
    global.add(p3, gbc);

    f.add(global);
    f.pack();
    f.setVisible(true);

}