Hi,
I don't know why you are doing it that way? Event Listeners can be
added to the components like this <abutton.addActionListener(this)>. In a complete program it can simply look like the program below.
Look at AddHandlers method and then go to actionPerformed to have an idea how it is done. These two examples should help you correct your code. Remember Event handlers have to be added to the components before the latter are added to the containers.
//Program ID Callers.
//Author Judex for CIS 210 Assignment 2 of 2003
//6 Feb 2003
//# This program simulates a Telephone Exchange It is done through a pad of
// 8 button that symbolizes a phone . A click on one these butttons starts
// the process of connection request and reply.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
//# compo is a user defined interface that holds the global variable of this
//class
public class Callers extends JFrame implements ActionListener,compo,
MouseListener {
int capacity, numberphoned;
charges newcharge ;
public Callers(){
super("Telephone Exchange Simulation");
initComponents();
AddHandlers();
CreateVisualDisplay();
setContentPane(phonepad);
}
//# This method actually ensures (in a bid for simplicity) that all users are
// eligible
public void initComponents(){
capacity=0;
for(int i=0; i<=7; i++){eligible[i]=true;
status[i]=1; }
}
public void AddHandlers(){
Phone1.addActionListener(this);
Phone2.addActionListener(this);
Phone3.addActionListener(this);
Phone4.addActionListener(this);
Phone5.addActionListener(this);
Phone6.addActionListener(this);
Phone7.addActionListener(this);
Phone8.addActionListener(this);
Phone1.addMouseListener(this);
Phone2.addMouseListener(this);
Phone3.addMouseListener(this);
Phone4.addMouseListener(this);
Phone5.addMouseListener(this);
Phone6.addMouseListener(this);
Phone7.addMouseListener(this);
Phone8.addMouseListener(this);
}
//creation of the pad used to symbolize the phones
public void CreateVisualDisplay(){
GridLayout grid = new GridLayout(4,4,10,20);
phonepad.setLayout(grid);
phonepad.add(Phone1);
phonepad.add(label1);
phonepad.add(label2);
phonepad.add(Phone2);
phonepad.add(Phone3);
phonepad.add(label3);
phonepad.add(label4);
phonepad.add(Phone4);
phonepad.add(Phone5);
phonepad.add(label5);
phonepad.add(label6);
phonepad.add(Phone6);
phonepad.add(Phone7);
phonepad.add(label7);
phonepad.add(label8);
phonepad.add(Phone8);
}
public static void main(String[] args){
JFrame frame = new Callers();
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
}
//#actionPerformed method is to check out which phone is being clicked
public void actionPerformed(ActionEvent evt){ Object source = evt.getSource();
if (source == Phone1) { EvaluateAction(2100);}
if (source == Phone2) { EvaluateAction(2101);}
if (source == Phone3) { EvaluateAction(2102);}
if (source == Phone4) { EvaluateAction(2103);}
if (source == Phone5) { EvaluateAction(2104);}
if (source == Phone6) { EvaluateAction(2105);}
if (source == Phone7) { EvaluateAction(2106);}
if (source == Phone8) { EvaluateAction(2107);}
}
Hope to have help you,
Judex
|