import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import javax.swing.*; /** * LayoutTester is a test window that displays rotatable RLabels * and switches between different rotation-aware layout managers. */ public class LayoutTester extends JFrame { // panel containing the RLabels JPanel panel; // poem we use for testing private static final String POEM[] = { "Turning and turning
in the widening gyre", "The falcon cannot
hear the falconer;", "Things fall apart;", "the centre cannot hold;", "Mere anarchy is
loosed upon the world,", "The blood-dimmed tide
is loosed,", "and everywhere", "The ceremony of
innocence is drowned;", "The best lack
all conviction,", "while the worst", "Are full of
passionate intensity.", }; /** * Make a LayoutTester. */ public LayoutTester() { super("The Second Coming (by William Butler Yeats)"); setDefaultCloseOperation(EXIT_ON_CLOSE); // create the UI Container c = getContentPane(); // make buttons that change the layout of the board JPanel buttons = new JPanel(); buttons.add(new JButton(new AbstractAction("Circle") { public void actionPerformed(ActionEvent event) { panel.setLayout(new CircleLayout()); panel.validate(); } })); buttons.add(new JButton(new AbstractAction("Tilt") { public void actionPerformed(ActionEvent event) { panel.setLayout(new TiltLayout()); panel.validate(); } })); buttons.add(new JButton(new AbstractAction("Add Label") { public void actionPerformed(ActionEvent event) { int n = panel.getComponentCount(); if (n < POEM.length) { System.err.println("adding " + POEM[n]); panel.add(makePoemLine(n)); panel.validate(); } } })); buttons.add(new JButton(new AbstractAction("Remove Label") { public void actionPerformed(ActionEvent event) { int n = panel.getComponentCount(); if (n > 0) { panel.remove(n-1); panel.validate(); panel.repaint(); } } })); c.add(buttons, BorderLayout.NORTH); panel = new JPanel(); panel.setLayout(new CircleLayout()); panel.add(makePoemLine(0)); panel.add(makePoemLine(1)); panel.add(makePoemLine(2)); c.add(panel, BorderLayout.CENTER); pack(); } /* * Make an RLabel containing the ith line of the poem. */ private RLabel makePoemLine(int i) { return new RLabel("" + POEM[i] + "", 0); } /** * Main method. Makes and displays a LayoutTester window. * @param args Command-line arguments, ignored. */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run () { new LayoutTester().show(); } }); } }