GridLayout, n*m grid. Column width/row height adjusted to fith the widest/highest Component.

Post on 31-Mar-2015

222 views 2 download

Tags:

transcript

GridLayout, n*m grid

Column width/row height adjusted to fith the widest/highest Component

Components can span multiple columns/rows

”Tall” components can create greater vertical distance

Components will keep their original size as default

Components can also be stretched to fill the available space

Components can be placed towards any side of the cell

Hei

Hei

Hei

import javax.swing.*;import java.awt.*;

public class Test extends JPanel {

public void lagLayout () { GridBagLayout layout = new GridBagLayout (); setLayout (layout); GridBagConstraints gbc = new GridBagConstraints ();

Hei

Hei

Hei

gbc.fill = GridBagConstraints.NONE;gbc.insets = new Insets (2, 2, 2, 2);

import javax.swing.*;import java.awt.*;

public class Test extends JPanel {

public void lagLayout () { GridBagLayout layout = new GridBagLayout (); setLayout (layout); GridBagConstraints gbc = new GridBagConstraints ();

Hei

Hei

Hei

gbc.gridx = 0;gbc.gridy = 0;gbc.gridwidth = 1;gbc.gridheight = 1;JLabel label1 = new JLabel ("Hei");layout.setConstraints (label1, gbc);add (label1);

import javax.swing.*;import java.awt.*;

public class Test extends JPanel {

public void lagLayout () { GridBagLayout layout = new GridBagLayout (); setLayout (layout); GridBagConstraints gbc = new GridBagConstraints (); gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets (2, 2, 2, 2);

Hei

Hei

Hei

gbc.gridy = 6;gbc.gridwidth = 3;gbc.anchor = GridBagConstraints.WEST;JTextField text2 = new JTextField ("Dette er en test");layout.setConstraints (text2, gbc);add (text2);

import javax.swing.*;import java.awt.*;

public class Test extends JPanel {

public void lagLayout () { GridBagLayout layout = new GridBagLayout (); setLayout (layout); GridBagConstraints gbc = new GridBagConstraints (); gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets (2, 2, 2, 2); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; JLabel label1 = new JLabel ("Hei"); layout.setConstraints (label1, gbc); add (label1);

Hei

Hei

Hei

gbc.gridy = 4;gbc.gridx = 2;gbc.gridwidth = 2;gbc.fill = GridBagConstraints.HORIZONTAL;JButton button2 = new JButton (”Fin knapp");layout.setConstraints (button2, gbc);add (button2);

GridBagLayout layout = new GridBagLayout (); setLayout (layout); GridBagConstraints gbc = new GridBagConstraints (); gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets (2, 2, 2, 2); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; JLabel label1 = new JLabel ("Hei"); layout.setConstraints (label1, gbc); add (label1); gbc.gridy = 6; gbc.gridwidth = 3; gbc.anchor = GridBagConstraints.WEST; JTextField text2 = new JTextField ("Dette er en test"); layout.setConstraints (text2, gbc); add (text2);

Hei

Hei

Hei

gbc.gridy = 5;gbc.gridx = 1;gbc.fill = GridBagConstraints.BOTH;JTextArea text3 = new JTekstArea (5,40);layout.setConstraints (text3, gbc);add (text3);

gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 1; JLabel label1 = new JLabel ("Hei"); layout.setConstraints (label1, gbc); add (label1); gbc.gridy = 6; gbc.gridwidth = 3; gbc.anchor = GridBagConstraints.WEST; JTextField text2 = new JTextField ("Dette er en test"); layout.setConstraints (text2, gbc); add (text2); gbc.gridy = 4; gbc.gridx = 2; gbc.gridwidth = 2; gbc.fill = GridBagConstraints.HORIZONTAL; JButton button2 = new JButton (”Fin knapp"); layout.setConstraints (button2, gbc); add (button2);

Hei

Hei

Hei

Same for the rest of the components.::

}

gbc.gridy = 6; gbc.gridwidth = 3; gbc.anchor = GridBagConstraints.WEST; JTextField text2 = new JTextField ("Dette er en test"); layout.setConstraints (text2, gbc); add (text2); gbc.gridy = 4; gbc.gridx = 2; gbc.gridwidth = 2; gbc.fill = GridBagConstraints.HORIZONTAL; JButton button2 = new JButton (”Fin knapp"); layout.setConstraints (button2, gbc); add (button2); gbc.gridy = 5; gbc.gridx = 1; gbc.fill = GridBagConstraints.BOTH; JTextArea text3 = new JTekstArea (5,40); layout.setConstraints (text3, gbc); add (text3);

public static void main (String args[]) { Test t = new Test (); t.lagLayout (); JFrame f = new JFrame (); f.getContentPane ().add (t); f.pack (); f.setVisible (true); }}

Hei

Hei

Hei

GridLayout

GridLayout

FlowLayout