/** * Author: Josiah Yoder et al. * Class: SE1021-031 * Date: 1/7/14 10:31 AM * Lesson: Week 4, Day 1 */ package example5_1_Anon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class GUI extends JFrame { private JButton button; private JLabel label; private final static int WIDTH = 250; private final static int HEIGHT = 100; public static void main(String[] args) { JFrame gui = new GUI(); } /* Constructor */ public GUI() { setTitle("Dog and Pony Show"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new FlowLayout()); label = new JLabel("Dog"); add(label); // Things you can do with label // label.setText("Pony"); // label.setVisible(false); button = new JButton(); // Approach 1 // ActionListener listener = new ActionListener() { // @Override // public void actionPerformed(ActionEvent e) { // System.out.println("Button Listener called"); // label.setText("Pony"); // } // }; // button.addActionListener(listener); // Approach 2: button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Button Listener called"); label.setText("Pony"); } }); add(button); setVisible(true); } }