// Counter1 import javax.swing.*; import java.awt.*; import java.awt.event.*; class Counter1 extends JFrame implements ActionListener { private int count; private Container c; private JButton b; private JLabel l; public static void main(String[] args) { Counter1 c = new Counter1(); c.setVisible(true); // why write setVisible and pack last? they "realize" the frame...see // http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html } public Counter1() { setGUI(); setLayout(); registerListeners(); } private void setGUI() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(200,100); } private void setLayout() { c = getContentPane(); c.setLayout(new FlowLayout(FlowLayout.LEFT)); b = new JButton("Push Me!"); c.add(b); l = new JLabel(generateLabel()); c.add(l); } private void registerListeners() { b.addActionListener(this); } public void actionPerformed(ActionEvent e) { count++; l.setText(generateLabel()); } private String generateLabel() { return "Count: "+count; } }