Java Reference
In-Depth Information
10
gridBagFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
11
gridBagFrame.setSize( 300 , 150 );
12
gridBagFrame.setVisible( true );
13
}
14
} // end class GridBagDemo
Fig. 22.22 | Test class for GridBagFrame . (Part 2 of 2.)
GUI Overview
The GUI contains three JButton s, two JTextArea s, a JComboBox and a JTextField . The
layout manager is GridBagLayout . Lines 21-22 create the GridBagLayout object and set
the layout manager for the JFrame to layout . Line 23 creates the GridBagConstraints
object used to determine the location and size of each component in the grid. Lines 26-
35 create each GUI component that will be added to the content pane.
JTextArea textArea1
Lines 39-40 configure JTextArea textArea1 and add it to the content pane. The values
for weightx and weighty values are not specified in constraints , so each has the value
zero by default. Thus, the JTextArea will not resize itself even if space is available. How-
ever, it spans multiple rows, so the vertical size is subject to the weighty values of JButton s
button2 and button3 . When either button is resized vertically based on its weighty value,
the JTextArea is also resized.
Line 39 sets variable fill in constraints to GridBagConstraints.BOTH , causing the
JTextArea to always fill its entire allocated area in the grid. An anchor value is not speci-
fied in constraints , so the default CENTER is used. We do not use variable anchor in this
 
Search WWH ::




Custom Search