codice:
import java.awt.Component;
import java.awt.Container;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/*
* GridBagDemo.java
*
* Created on 8 aprile 2005, 10.07
*/
/**
*
* @author mau
*/
public class GridBagDemo extends JFrame{
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
/** Creates a new instance of GridBagDemo */
public GridBagDemo() {
super("GridBagLayout");
container = getContentPane();
layout = new GridBagLayout();
container.setLayout(layout);
//istanzia l'oggetto GridBagConstraints
constraints = new GridBagConstraints();
//crea i componenti GUI
JTextArea textAre1 = new JTextArea("TextArea1", 5, 10);
JTextArea textAre2 = new JTextArea("TextArea2", 5, 4);
String names[] = {"Iron", "Steel", "Brass"};
JComboBox comboBox = new JComboBox(names);
JTextField textField = new JTextField("TextField");
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JButton button3 = new JButton("Button 3");
//weightx e weighty per textAre1 sono entrambi 0: il valore di
//anchor per tutti i componenti e' CENTER(il valore di default)
constraints.fill = GridBagConstraints.BOTH;
addComponenet(textAre1, 0, 0, 1, 3);
//wheightx e wheighty per button1 sono entrambi 0(il valore di default)
constraints.fill = GridBagConstraints.HORIZONTAL;
addComponenet(button1 , 0, 1, 2, 1);
//weightx e weighty per comboBox sono entrambi 0(il valore di default)
//fill vale HORIZONTAL
addComponenet(comboBox, 2, 1, 2, 1);
//button2
constraints.weightx = 1000;//puo' diventare piu' largo
constraints.weighty = 1;//puo' diventare piu' alto
constraints.fill = GridBagConstraints.BOTH;
addComponenet(button2, 1, 1, 1, 1);
//fill vale BOTH per button3
constraints.weightx = 0;
constraints.weighty = 0;
addComponenet(button3, 1, 2, 1, 1);
//weightx e weighty per textField sono entrambi 0, fill vale BOTH
addComponenet(textField, 3, 0, 2, 1);
//weightx e weighty per textArea2 sono entrambi 0, fill vale BOTH
addComponenet(textAre2, 3, 2, 1, 1);
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
setBounds(center.x - 300/2, center.y - 150/2, 300, 150);
setVisible(true);
}//fine costruttore
private void addComponenet(Component component, int row, int column, int width, int height){
//imposta gridx e gridy
constraints.gridx = column;
constraints.gridy = row;
//imposta gridWidth e gridHeight;
constraints.gridwidth = width;
constraints.gridheight = height;
//imposta i vincoli e aggiunge i componeneti
layout.setConstraints(component, constraints);
container.add(component);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GridBagDemo application = new GridBagDemo();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}