Sat Feb 09, 2013 1:43 pm
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TwoListeners extends Applet
implements ActionListener {
TextArea textAreaObj1;
TextArea textAreaObj2;
Button btnObj1, btnObj2;
public void init() {
Label labelObj = null;
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints gridBagConstrain = new GridBagConstraints();
setLayout(gridbag);
gridBagConstrain.fill = GridBagConstraints.BOTH;
gridBagConstrain.gridwidth = GridBagConstraints.REMAINDER;
labelObj = new Label("Listener one:");
gridbag.setConstraints(labelObj, gridBagConstrain);
add(labelObj);
gridBagConstrain.weighty = 1.0;
textAreaObj1 = new TextArea(5, 20);
textAreaObj1.setEditable(false);
gridbag.setConstraints(textAreaObj1, gridBagConstrain);
add(textAreaObj1);
gridBagConstrain.weightx = 0.0;
gridBagConstrain.weighty = 0.0;
labelObj = new Label("Listener two:");
gridbag.setConstraints(labelObj, gridBagConstrain);
add(labelObj);
gridBagConstrain.weighty = 1.0;
textAreaObj2 = new TextArea(5, 20);
textAreaObj2.setEditable(false);
gridbag.setConstraints(textAreaObj2, gridBagConstrain);
add(textAreaObj2);
gridBagConstrain.weightx = 1.0;
gridBagConstrain.weighty = 0.0;
gridBagConstrain.gridwidth = 1;
gridBagConstrain.insets = new Insets(10, 10, 0, 10);
btnObj1 = new Button(" Button Obj1");
gridbag.setConstraints(btnObj1, gridBagConstrain);
add(btnObj1);
gridBagConstrain.gridwidth = GridBagConstraints.REMAINDER;
btnObj2 = new Button("Button Obj2");
gridbag.setConstraints(btnObj2, gridBagConstrain);
add(btnObj2);
btnObj1.addActionListener(this);
btnObj2.addActionListener(this);
btnObj2.addActionListener(new Listener2(textAreaObj2));
}
public void actionPerformed(ActionEvent e) {
textAreaObj1.append(e.getActionCommand() + "\n");
}
}
class Listener2 implements ActionListener {
TextArea myTextArea;
public Listener2(TextArea ta) {
myTextArea = ta;
}
public void actionPerformed(ActionEvent e) {
myTextArea.append(e.getActionCommand() + "\n");
}
}
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.