Java Tutorial/Swing/Box — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:32, 31 мая 2010
Содержание
Box.createVerticalGlue()
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.LayoutManager;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BoxLayoutVerticalGlueTest {
public static void main(String[] args) {
JFrame f = new JFrame("Vertical BoxLayout-managed container");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = new BoxPanel();
f.setContentPane(pane);
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
for (float align = 0.0f; align <= 1.0f; align += 0.25f) {
JButton button = new JButton("X Alignment = " + align);
button.setAlignmentX(align);
pane.add(button);
pane.add(Box.createVerticalGlue());
}
f.setSize(400, 300);
f.setVisible(true);
}
}
class BoxPanel extends JPanel {
public void paintChildren(Graphics g) {
super.paintChildren(g);
Dimension size = getSize();
LayoutManager manager = getLayout();
if ((manager != null) && (manager instanceof BoxLayout)) {
BoxLayout layout = (BoxLayout) manager;
boolean vertical = true;
if (vertical) {
int axis = (int) (layout.getLayoutAlignmentX(this) * size.width);
g.fillRect(axis - 1, 0, 3, size.height);
} else {
int axis = (int) (layout.getLayoutAlignmentY(this) * size.height);
g.fillRect(0, axis - 1, size.width, 3);
}
}
}
}
Box simplifies working with BoxLayout
- Box class is a Container for creating a single row or column of components using the BoxLayout manager.
- The Box container works like a JPanel with default layout manager, BoxLayout.
- You can use an inner class of Box called Box.Filler to better position components within the container.
- You have three ways to create a Box, offered by one constructor and two static factory methods:
public Box(int direction)
Box horizontalBox = new Box(BoxLayout.X_AXIS);
Box verticalBox = new Box(BoxLayout.Y_AXIS);
public static Box createHorizontalBox()
Box horizontalBox = Box.createHorizontalBox();
public static Box createVerticalBox()
Box verticalBox = Box.createVerticalBox();
Creating Rigid Areas: Strut
- 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);
new Box(BoxLayout.Y_AXIS)
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.JTextField;
public class MainClass extends JFrame {
public MainClass() {
Box box = new Box(BoxLayout.Y_AXIS);
box.add(new JButton("Test button"));
box.add(new JSlider());
box.add(new JTextField("Text field with some text", 20));
box.add(new JButton("Another, bigger button"));
getContentPane().add(box);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public static void main(String[] args) {
MainClass ex2 = new MainClass();
ex2.setVisible(true);
}
}
Working with Box.Filler: Glue and Strut
Box.Filler creates invisible components for better component positioning.
Creating Areas that Grow
public static Component createGlue()
// Direction independent
Component glue = Box.createGlue();
aBox.add(glue);
public static Component createHorizontalGlue();
// Direction dependent: horizontal
Component horizontalGlue = Box.createHorizontalGlue();
aBox.add(horizontalGlue);
public static Component createVerticalGlue()
// Direction dependent: vertical
Component verticalGlue = Box.createVerticalGlue();
aBox.add(verticalGlue);