class
EventListener1
extends
JFrame
implements
ActionListener {
private
JButton btBlue, btDialog;
public
EventListener1() {
setTitle(
"Java GUI 事件监听处理"
);
setBounds(100, 100, 500, 350);
setLayout(
new
FlowLayout());
btBlue =
new
JButton(
"蓝色"
);
btDialog =
new
JButton(
"弹窗"
);
btBlue.addActionListener(this);
btDialog.addActionListener(this);
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public
void actionPerformed(ActionEvent e) {
if
(e.getSource() == btBlue) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
else
if
(e.getSource() == btDialog) {
JDialog dialog =
new
JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
}
}
class
EventListener3
extends
JFrame {
private
JButton btBlue, btDialog;
public
EventListener3() {
setTitle(
"Java GUI 事件监听处理"
);
setBounds(100, 100, 500, 350);
setLayout(
new
FlowLayout());
btBlue =
new
JButton(
"蓝色"
);
btDialog =
new
JButton(
"弹窗"
);
btBlue.addActionListener(
new
ColorEventListener());
btDialog.addActionListener(
new
DialogEventListener());
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class
ColorEventListener
implements
ActionListener {
@Override
public
void actionPerformed(ActionEvent e) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
}
class
DialogEventListener
implements
ActionListener {
@Override
public
void actionPerformed(ActionEvent e) {
JDialog dialog =
new
JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
}
}
class
EventListener2
extends
JFrame {
private
JButton btBlue, btDialog;
public
EventListener2() {
setTitle(
"Java GUI 事件监听处理"
);
setBounds(100, 100, 500, 350);
setLayout(
new
FlowLayout());
btBlue =
new
JButton(
"蓝色"
);
btDialog =
new
JButton(
"弹窗"
);
btBlue.addActionListener(
new
ActionListener() {
@Override
public
void actionPerformed(ActionEvent e) {
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
});
btDialog.addActionListener(
new
ActionListener() {
@Override
public
void actionPerformed(ActionEvent e) {
JDialog dialog =
new
JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
});
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class
EventListener4
extends
JFrame {
private
JButton btBlue, btDialog;
public
EventListener4() {
setTitle(
"Java GUI 事件监听处理"
);
setBounds(100, 100, 500, 350);
setLayout(
new
FlowLayout());
btBlue =
new
JButton(
"蓝色"
);
btDialog =
new
JButton(
"弹窗"
);
btBlue.addActionListener(
new
ColorEventListener(this));
btDialog.addActionListener(
new
DialogEventListener());
add(btBlue);
add(btDialog);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class
ColorEventListener
implements
ActionListener {
private
EventListener4 el;
ColorEventListener(EventListener4 el) {
this.el = el;
}
@Override
public
void actionPerformed(ActionEvent e) {
Container c = el.getContentPane();
c.setBackground(Color.BLUE);
}
}
class
DialogEventListener
implements
ActionListener {
@Override
public
void actionPerformed(ActionEvent e) {
JDialog dialog =
new
JDialog();
dialog.setBounds(300, 200, 400, 300);
dialog.setVisible(true);
}
}
public
class
ActionListenerTest
{
public
static
void main(String args[])
{
new
EventListener2();
}
}