| |
14. 88. 4. 建立刚性区域 |
|
- Struts can be two-dimensional, requiring you to specify the width and height of the component;
- Or, they can be one-dimensional, requiring you to specify either the width or height.
|
public static Component createRigidArea(Dimension dimension)
// Two-dimensional
Component rigidArea = Box. createRigidArea(new Dimension(10, 10));
aBox.add(rigidArea);
public static Component createHorizontalStrut(int width)
// One-dimensional: horizontal
Component horizontalStrut = Box. createHorizontalStrut(10);
aBox.add(horizontalStrut);
public static Component createVerticalStrut(int height)
// One-dimensional: vertical
Component verticalStrut = Box. createVerticalStrut(10);
aBox.add(verticalStrut);
|
|
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BoxLayoutGlueStrutCompare extends JFrame {
public BoxLayoutGlueStrutCompare() {
JPanel p = new JPanel(new GridLayout(3, 1));
JPanel p1 = new JPanel();
Box box1 = new Box(BoxLayout.X_AXIS);
box1.add(Box.createHorizontalGlue());
box1.add(new JButton("glue-strut"));
box1.add(Box.createHorizontalStrut(15));
box1.add(new JButton("strut-glue"));
box1.add(Box.createHorizontalGlue());
p1.add(box1);
p1.setBorder(BorderFactory.createRaisedBevelBorder());
JPanel p2 = new JPanel();
Box box2 = new Box(BoxLayout.X_AXIS);
box2.add(Box.createHorizontalStrut(25));
box2.add(new JButton("strut-glue"));
box2.add(Box.createHorizontalGlue());
box2.add(new JButton("glue-strut"));
box2.add(Box.createHorizontalStrut(25));
p2.add(box2);
p2.setBorder(BorderFactory.createRaisedBevelBorder());
JPanel p3 = new JPanel();
Box box3 = new Box(BoxLayout.X_AXIS);
box3.add(Box.createHorizontalStrut(25));
box3.add(new JButton("strut-glue"));
box3.add(Box.createHorizontalGlue());
box3.add(new JButton("glue-glue"));
box3.add(Box.createHorizontalGlue());
p3.add(box3);
p3.setBorder(BorderFactory.createRaisedBevelBorder());
p.add(p1);
p.add(p2);
p.add(p3);
getContentPane().add(p);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args) {
BoxLayoutGlueStrutCompare ex1 = new BoxLayoutGlueStrutCompare();
ex1.setVisible(true);
}
}
|
|
|