怎麼樣......
protected JButton x, z, a, b,c;
x = new JButton("add x");
z = new JButton("add z");
a = new JButton("add a");
b = new JButton("add b");
c = new JButton("add c");
x.addActionListener(this);
z.addActionListener(this);
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
然後
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource()==x)
{
//do something
}
else if (evt.getSource()==z)
{
//do something
}
else if (evt.getSource()==a)
{
//do something
}
else if (evt.getSource()==b)
{
//do something
}
else if (evt.getSource()==c)
{
//do something
}
}
這始終是對我的作品,但老實說,我不知道這是否是不錯練習
不要使用ActionListener,請使用Action inste廣告:它的一般規則是總是去爲最高抽象可用 – kleopatra 2011-05-06 13:00:40