/** * A GUI that has various components in it, to demonstrate * UI components and layout managers (rendering). * Now with Interactivity! **/ import javax.swing.*; // Need this to reach Swing components import java.awt.*; // Need this to reach FlowLayout import java.awt.event.*; // Need this for listeners and events public class GUItreeInteractive extends JFrame { public GUItreeInteractive(){ super("GUI Tree Interactive Example"); FileChooser.setMediaPath("D:/cs1316/mediasources/"); //CHANGE ME! this.getContentPane().setLayout(new FlowLayout()); /* Put in a panel with a label in it */ JPanel panel1 = new JPanel(); this.getContentPane().add(panel1); JLabel label = new JLabel("This is panel 1!"); panel1.add(label); /* Put in another panel with two buttons in it */ JPanel panel2 = new JPanel(); this.getContentPane().add(panel2); JButton button1 = new JButton("Make a sound"); button1.addActionListener( new ActionListener() { // Here's the listener // Here's the method we're overriding public void actionPerformed(ActionEvent e) { Sound s = new Sound(FileChooser.getMediaPath("warble-h.wav")); s.play(); } } ); panel2.add(button1); JButton button2 = new JButton("Make a picture"); button2.addActionListener( new ActionListener() { // Here's the listener // Here's the method we're overriding public void actionPerformed(ActionEvent e) { Picture p = new Picture(FileChooser.getMediaPath("shops.jpg")); p.show(); } } ); panel2.add(button2); this.pack(); this.setVisible(true); } }