Download presentation
Presentation is loading. Please wait.
Published byOrion Hollaway Modified over 10 years ago
1
GridLayout, n*m grid
2
Column width/row height adjusted to fith the widest/highest Component
3
Components can span multiple columns/rows
4
”Tall” components can create greater vertical distance
5
Components will keep their original size as default
6
Components can also be stretched to fill the available space
7
Components can be placed towards any side of the cell
8
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 ();
9
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 ();
10
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);
11
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);
12
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);
13
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);
14
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);
15
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
23
GridLayout FlowLayout
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.