FileDocCategorySizeDatePackage
JButtonDemo.javaAPI DocExample1807Wed Jan 16 16:43:08 GMT 2002None

JButtonDemo.java

//JButtonDemo Swing demo
//ipds 5/2001
import java.awt.*;      //note you still need the awt even though you are using swing
import java.awt.event.*;
import javax.swing.*;    //note it is javax and not java

  
public class JButtonDemo extends JApplet     //note it is a JApplet and not an Applet
implements ActionListener {
  JTextField jtf;

  public void init() {
  //initialise tha applet
    // Get content pane, we need to do this for swing because
    // there are more than one panes (only 1 for an awt applet)
    Container contentPane = getContentPane();
    contentPane.setLayout(new FlowLayout());
	
	
    // Add buttons to content pane
    ImageIcon canada = new ImageIcon("but_register[1].gif"); //load an icon
    JButton jb = new JButton(canada); //attach the icon to a new button
    
    jb.setActionCommand("Register");
    jb.addActionListener(this);
    contentPane.add(jb);  //notice we call the add method of the contentPane (for awt we just called add from the applet itself)

    ImageIcon germany = new ImageIcon("88x31[1].gif");
    jb = new JButton(germany);
    jb.setActionCommand("Swift Desk");
    jb.addActionListener(this);
    contentPane.add(jb);

    ImageIcon uk = new ImageIcon("property_bookit_button[1].gif");
    jb = new JButton(uk);
    jb.setActionCommand("Hotel Booking");
    jb.addActionListener(this);
    contentPane.add(jb);

    ImageIcon egypt = new ImageIcon("logo[1].gif");
    jb = new JButton(egypt);
    jb.setActionCommand("Barclaycard");
    jb.addActionListener(this);
    contentPane.add(jb);

    // Add text field to content pane
    jtf = new JTextField(15);
    contentPane.add(jtf);
  }

  public void actionPerformed(ActionEvent ae) {
    jtf.setText(ae.getActionCommand());
  }
}