Sat Feb 09, 2013 2:27 pm
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class ComponentEventExample extends Applet
implements ComponentListener,
ActionListener {
TextArea textAreaObj;
Frame frameObj;
public boolean showIt = false;
final static String showMessage = "show";
final static String emptyMessage = "empty";
String newline;
public void init() {
lineSeparator = System.getProperty("line.separator");
setLayout(new BorderLayout());
textAreaObj = new TextArea(5, 25);
textAreaObj.setEditable(false);
add("Center", textAreaObj);
Button btnObj1 = new Button("Start");
btnObj1.setActionCommand(showMessage);
btnObj1.addActionListener(this);
add("North", btnObj1);
Button btnObj2 = new Button("Clear");
btnObj2.setActionCommand(emptyMessage);
btnObj2.addActionListener(this);
add("South", btnObj2);
frameObj = new Frame("My Frame");
comPanel comPanelObj = new comPanel(this);
frameObj.addComponentListener(this);
comPanelObj.addComponentListener(this);
frameObj.add("Center", comPanelObj);
frameObj.pack();
frameObj.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
showIt = false;
frameObj.setVisible(false);
}
});
}
//
public void actionPerformed(ActionEvent e) {
// condition to do action
// print the content of getActionCommand() if you want.
if (e.getActionCommand() == showMessage) {
showIt = true;
frameObj.setVisible(true);
} else { // clear textArea content.
textAreaObj.setText("");
}
}
public void stop() {
frameObj.setVisible(false);
}
public void start() {
if (showIt) {
frameObj.setVisible(true);
}
}
protected void displayMessage(String message) {
try {
textAreaObj.append(message + lineSeparator);
} catch (Exception event) {}
}
public void componentHidden(ComponentEvent e) {
displayMessage("componentHidden event from "
+ e.getComponent().getClass().getName());
}
public void componentMoved(ComponentEvent e) {
displayMessage("componentMoved event from "
+ e.getComponent().getClass().getName());
}
public void componentResized(ComponentEvent e) {
displayMessage("componentResized event from "
+ e.getComponent().getClass().getName());
}
public void componentShown(ComponentEvent e) {
displayMessage("componentShown event from "
+ e.getComponent().getClass().getName());
}
}
class comPanel extends Panel
implements ItemListener {
Label labelObj;
Checkbox checkBoxObj;
comPanel(ComponentEventExample listener) {
setLayout(new BorderLayout());
labelObj = new Label("Label", Label.CENTER);
add("Center", labelObj);
checkBoxObj = new Checkbox("Label visible", true);
checkBoxObj.addItemListener(this);
add("South", checkBoxObj);
labelObj.addComponentListener(listener);
checkBoxObj.addComponentListener(listener);
}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
labelObj.setVisible(true);
} else {
labelObj.setVisible(false);
}
}
}
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.