Sat Feb 09, 2013 7:22 pm
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowEventExample extends Applet
implements WindowListener,
ActionListener {
String lineSep;
TextArea textAreaObj;
Frame frameObj;
Button btnOne, btnTwo;
static final String SHOW = "show";
static final String CLEAR = "clear";
public void init() {
btnOne = new Button("Click to bring up a window.");
btnOne.setActionCommand(SHOW);
btnOne.addActionListener(this);
btnTwo = new Button("Click to clear the display.");
btnTwo.setActionCommand(CLEAR);
btnTwo.addActionListener(this);
textAreaObj = new TextArea(5, 20);
textAreaObj.setEditable(false);
setLayout(new BorderLayout());
add("North", btnOne);
add("Center", textAreaObj);
add("South", btnTwo);
frameObj = new Frame("Frame with Window Event ");
frameObj.addWindowListener(this);
frameObj.add("Center",
new Label("The applet listens to this window for window events."));
frameObj.pack();
lineSep = System.getProperty("line.separator");
}
public void stop() {
frameObj.setVisible(false);
}
public void windowClosing(WindowEvent e) {
frameObj.setVisible(false);
printMessage("Window closing", e);
}
public void windowClosed(WindowEvent e) {
printMessage("Window closed", e);
}
public void windowOpened(WindowEvent e) {
printMessage("Window opened", e);
}
public void windowIconified(WindowEvent e) {
printMessage("Window iconified", e);
}
public void windowDeiconified(WindowEvent e) {
printMessage("Window deiconified", e);
}
public void windowActivated(WindowEvent e) {
printMessage("Window activated", e);
}
public void windowDeactivated(WindowEvent e) {
printMessage("Window deactivated", e);
}
void printMessage(String prefix, WindowEvent e) {
textAreaObj.append(prefix
+ ": "
+ e.getWindow()
+ lineSep);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == SHOW) {
frameObj.pack();
frameObj.setVisible(true);
} else {
textAreaObj.setText("");
}
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.