/** * LayoutDemoPanel.java * Jeff Ondich, 25 October 2010 * * A little demo to help you experiment with AWT layout managers. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LayoutDemoPanel extends JPanel implements ActionListener { private JLabel messageLabel; public LayoutDemoPanel() { // Try different layout manager classes here. FlowLayout layout = new FlowLayout(); this.setLayout(layout); // Add some buttons and a label. JButton button; button = new JButton("Joke"); button.setActionCommand("joke"); button.addActionListener(this); this.add(button); button = new JButton("Song"); button.setActionCommand("song"); button.addActionListener(this); this.add(button); button = new JButton("Poem"); button.setActionCommand("poem"); button.addActionListener(this); this.add(button); button = new JButton("Story"); button.setActionCommand("story"); button.addActionListener(this); this.add(button); button = new JButton("Equation"); button.setActionCommand("equation"); button.addActionListener(this); this.add(button); button = new JButton("Relax"); button.setActionCommand("relax"); button.addActionListener(this); this.add(button); this.messageLabel = new JLabel("???"); this.add(this.messageLabel); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("joke")) { this.messageLabel.setText("Why is 6 afraid of 7?"); } else if (command.equals("song")) { this.messageLabel.setText("...I use antlers in all of my decorating!..."); } else if (command.equals("poem")) { this.messageLabel.setText("In the world of mules, there are no rules."); } else if (command.equals("story")) { this.messageLabel.setText("In a hole in the ground there lived a hobbit."); } else if (command.equals("equation")) { this.messageLabel.setText("e^(i*pi) + 1 = 0"); } else if (command.equals("relax")) { this.messageLabel.setText("zzzzzzzzzz..."); } } public static void main(String args[]) { JFrame frame = new JFrame("Layout example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); LayoutDemoPanel contentPane = new LayoutDemoPanel(); contentPane.setOpaque(true); frame.setContentPane(contentPane); frame.pack(); frame.setVisible(true); } }