Java by API/javax.swing/BoxLayout
Версия от 17:43, 31 мая 2010; (обсуждение)
BoxLayout.X_AXIS
import java.awt.BorderLayout;
import java.awt.ruponent;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass {
public static void main(String[] a) {
JFrame frame = new JFrame("Alignment Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String labels[] = { "--", "----", "--------", "------------" };
JPanel container = new JPanel();
BoxLayout layout = new BoxLayout(container, BoxLayout.X_AXIS);
container.setLayout(layout);
for (int i = 0; i < labels.length; i++) {
JButton button = new JButton(labels[i]);
button.setAlignmentX(Component.TOP_ALIGNMENT);
container.add(button);
}
frame.add(container, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
BoxLayout.Y_AXIS
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass {
public static void main(String[] a) {
JFrame frame = new JFrame("Alignment Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String labels[] = { "--", "----", "--------", "------------" };
JPanel container = new JPanel();
BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
container.setLayout(layout);
for (int i = 0; i < labels.length; i++) {
JButton button = new JButton(labels[i]);
container.add(button);
}
frame.add(container, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
new BoxLayout(Container target, int axis)
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass {
public static void main(String[] a) {
JFrame frame = new JFrame("Alignment Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String labels[] = { "--", "----", "--------", "------------" };
JPanel container = new JPanel();
BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
container.setLayout(layout);
for (int i = 0; i < labels.length; i++) {
JButton button = new JButton(labels[i]);
container.add(button);
}
frame.add(container, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.setVisible(true);
}
}