本帖最后由 goldenking56 于 2012-3-28 11:27 编辑
一个事件源可以安装多个监听器吗?,如以下代码,我给button加了两个监听器,这样可以吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test
{
public static void main(String[] args)
{
final JFrame frame=new JFrame();
final JPanel panel=new JPanel();
final JButton button=new JButton("Test");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
frame.setTitle("Listener Test");
}
});
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
panel.setBackground(Color.GREEN);
}
});
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
button.setBackground(Color.RED);
frame.setResizable(false);
}
});
panel.add(button);
frame.add(panel);
frame.setSize(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
} |
|