Java Reference
In-Depth Information
Fig. 22.20 | GridBagLayout with the weights set to zero.
1
// Fig. 22.21: GridBagFrame.java
2
// Demonstrating GridBagLayout.
3
4
5
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Component;
6
import javax.swing.JFrame;
7
import javax.swing.JTextArea;
8
import javax.swing.JTextField;
9
import javax.swing.JButton;
10
import javax.swing.JComboBox;
11
12
public class GridBagFrame extends JFrame
13
{
14
private final GridBagLayout layout; // layout of this frame
private final GridBagConstraints constraints; // layout's constraints
15
16
17
// set up GUI
18
public GridBagFrame()
19
{
20
super ( "GridBagLayout" );
21
layout = new GridBagLayout();
setLayout(layout); // set frame layout
constraints = new GridBagConstraints(); // instantiate constraints
22
23
24
25
// create GUI components
26
JTextArea textArea1 = new JTextArea( "TextArea1" , 5 , 10 );
27
JTextArea textArea2 = new JTextArea( "TextArea2" , 2 , 2 );
28
29
String[] names = { "Iron" , "Steel" , "Brass" };
30
JComboBox<String> comboBox = new JComboBox<String>(names);
31
32
JTextField textField = new JTextField( "TextField" );
33
JButton button1 = new JButton( "Button 1" );
34
JButton button2 = new JButton( "Button 2" );
35
JButton button3 = new JButton( "Button 3" );
36
37
// weightx and weighty for textArea1 are both 0: the default
38
// anchor for all components is CENTER: the default
39
constraints.fill = GridBagConstraints.BOTH ;
addComponent(textArea1, 0 , 0 , 1 , 3 );
40
Fig. 22.21 | GridBagLayout layout manager. (Part 1 of 2.)
 
Search WWH ::




Custom Search