Here's an applet version of the BangPopAp
from Lecture 12:
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
/**
* @author joanna
*
* Applet version, from Barbara Johnston's "Java Programming Today"
*/
public class BangPopApplet extends JApplet implements ActionListener {
JButton bangButton = new JButton ("Bang");
JButton popButton = new JButton ("Pop");
// public BangPopAp() { // JFrame
//this.setTitle("Bangs and Pops!"); //JFrame
public void init () { // JApplet
Container canvas = getContentPane();
canvas.setLayout (new GridLayout(2,1));
canvas.add(bangButton);
canvas.add(popButton);
bangButton.addActionListener(this);
popButton.addActionListener(this);
this.setSize(250,150); // not nec for applet?
// this.show(); // not meaningful for applet
}
/* JFrame need this, applets don't --
public static void main(String[] args) {
BangPopAp theApp = new BangPopAp();
theApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource()==bangButton) {
JOptionPane.showMessageDialog(this, "Bang!");
} else if (e.getSource()== popButton) {
JOptionPane.showMessageDialog(this, "Pop!");
}
}
} // class