import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Compute { public Compute () { JFrame frame = new JFrame("Event Test"); Container content = frame.getContentPane(); content.setLayout(new FlowLayout()); frame.setSize(300,300); final JTextField tf = new JTextField(10); content.add(tf); final JLabel label = new JLabel(); content.add(label); final JButton button = new JButton("Compute"); content.add(button); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae) { String text = tf.getText(); int value = 0; try { value = Integer.parseInt(text); } catch (NumberFormatException nfe) { return; } label.setText(String.valueOf(value * value)); } }; button.addActionListener(al); frame.setVisible(true); } public static void main(String[] args) { new Compute(); } }