/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski
ISBN: 1-893115-78-X
Publisher: APress
*/
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class YAxisDiffAlign {
private static Container makeIt(String title, boolean more) {
JPanel container = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Insets insets = getInsets();
int width = getWidth() - insets.left - insets.right;
int halfWidth = width / 2 + insets.left;
int height = getHeight();
int halfHeight = height / 2 + insets.top;
g.drawLine(halfWidth, 0, halfWidth, height);
}
};
container.setBorder(BorderFactory.createTitledBorder(title));
BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
container.setLayout(layout);
JButton button;
button = new JButton("0.0");
button.setOpaque(false);
button.setAlignmentX(Component.LEFT_ALIGNMENT);
container.add(button);
if (more) {
button = new JButton(".25");
button.setOpaque(false);
button.setAlignmentX(0.25f);
container.add(button);
button = new JButton(".5");
button.setOpaque(false);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
container.add(button);
button = new JButton(".75");
button.setOpaque(false);
button.setAlignmentX(0.75f);
container.add(button);
}
button = new JButton("1.0");
button.setOpaque(false);
button.setAlignmentX(Component.RIGHT_ALIGNMENT);
container.add(button);
return container;
}
public static void main(String args[]) {
JFrame frame = new JFrame("Alignment Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container panel1 = makeIt("Mixed", false);
Container panel2 = makeIt("Mixed", true);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1, 2));
contentPane.add(panel1);
contentPane.add(panel2);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
|