Tutorial Addendum on Beat - Layouts of Apparatus
| |
GridBagLayout
java.awt.GridBagLayout - A added circuitous blueprint that:
- Divides the alembic into rows and columns. The amount of rows and
the amount of columns are unlimited. Rows and columns are not appropriately
divided.
- Places apparatus into the defined cells.
- Uses the absence admeasurement of anniversary component.
- Keeps basic sizes banausic if the alembic is resized.
- Provides alone blueprint constraints for anniversary basic to ascendancy
its blueprint behavior.
Once again, I wrote addition program to try to affectation my window
with GridBagLayout:
/**
* GridBagLayoutTest.java
* Absorb (c) 2002 by Dr. Yang
*/
import java.awt.*;
import javax.swing.*;
public chic GridBagLayoutTest {
accessible changeless abandoned main(String[] a) {
JFrame myFrame = new JFrame("GridBagLayout Test");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Alembic myPane = myFrame.getContentPane();
myPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
setMyConstraints(c,0,0,GridBagConstraints.CENTER);
myPane.add(getFieldPanel(),c);
setMyConstraints(c,0,1,GridBagConstraints.CENTER);
myPane.add(getButtonPanel(),c);
myFrame.pack();
myFrame.setVisible(true);
}
clandestine changeless JPanel getFieldPanel() {
JPanel p = new JPanel(new GridBagLayout());
p.setBorder(BorderFactory.createTitledBorder("Details"));
GridBagConstraints c = new GridBagConstraints();
setMyConstraints(c,0,0,GridBagConstraints.EAST);
p.add(new JLabel("Name:"),c);
setMyConstraints(c,1,0,GridBagConstraints.WEST);
p.add(new JTextField(16),c);
setMyConstraints(c,0,1,GridBagConstraints.EAST);
p.add(new JLabel("System:"),c);
setMyConstraints(c,1,1,GridBagConstraints.WEST);
p.add(getSystemPanel(),c);
setMyConstraints(c,0,2,GridBagConstraints.EAST);
p.add(new JLabel("Language:"),c);
setMyConstraints(c,1,2,GridBagConstraints.WEST);
p.add(getLanguagePanel(),c);
setMyConstraints(c,0,3,GridBagConstraints.EAST);
p.add(new JLabel("Year:"),c);
setMyConstraints(c,1,3,GridBagConstraints.WEST);
p.add(new JComboBox(new String[] {"2001","2002","2003"}),c);
acknowledgment p;
}
clandestine changeless JPanel getButtonPanel() {
JPanel p = new JPanel(new GridBagLayout());
p.add(new JButton("OK"));
p.add(new JButton("Cancel"));
acknowledgment p;
}
clandestine changeless JPanel getSystemPanel() {
JRadioButton unixButton = new JRadioButton("Unix",true);
JRadioButton winButton = new JRadioButton("Window",false);
ButtonGroup systemGroup = new ButtonGroup();
systemGroup.add(unixButton);
systemGroup.add(winButton);
JPanel p = new JPanel(new GridBagLayout());
p.add(unixButton);
p.add(winButton);
acknowledgment p;
}
clandestine changeless JPanel getLanguagePanel() {
JPanel p = new JPanel(new GridBagLayout());
p.add(new JCheckBox("Java",true));
p.add(new JCheckBox("C++",true));
p.add(new JCheckBox("Perl",false));
acknowledgment p;
}
clandestine changeless abandoned setMyConstraints(GridBagConstraints c,
int gridx, int gridy, int anchor) {
c.gridx = gridx;
c.gridy = gridy;
c.anchor = anchor;
}
}
Run it. What do you anticipate about the result? About perfect, right?
All apparatus are accumbent accurately in both admonition now.
And all apparatus are appropriately sized.
GridBagLayout seems to be acceptable abundant for my example.
Conclusions:
- BorderLayout, FlowLayout, GridLayout, and BoxLayout are not actual useful.
- GridBagLayout is acceptable abundant for some absolute applications.
- There are some additional layouts in JDK. You can analysis them yourself.
|
gridbagconstraints, jpanel, setmyconstraints, gridbaglayout, static, myframe, components, private, return, jradiobutton, mypane, jlabel, systemgroup, winbutton, jcheckbox, gridx, anchor, gridy, unixbutton, jframe, container, layouts, layout, component, columns, swing, , new gridbaglayout, private static, new jlabel, gridbagconstraints west, gridbagconstraints east, jpanel new, new jpanel, static jpanel, new jcheckbox, private static jpanel, gridbagconstraints center mypane, new gridbagconstraints setmyconstraints, |
Also see ...