1、事件处理器
package com.jlwang.swing;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ListenerDemo1
{
private static JFrame frame;
private static JPanel panel;
private static JButton button1;
private static JButton button2;
private class Button1Handler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(frame, "button1 clicked!");
}
}
private class Button2Handler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(frame, "button2 Clicked!");
}
}
public ListenerDemo1()
{
super();
panel = new JPanel();
button1 = new JButton("button1");
button2 = new JButton("button2");
button1.addActionListener(new Button1Handler());
button1.setPreferredSize(new Dimension(200,200));
button2.addActionListener(new Button2Handler());
button2.setPreferredSize(new Dimension(200,200));
panel.add(button1);
panel.add(button2);
}
public static void main(String[] args)
{
ListenerDemo1 listenerDemo1 = new ListenerDemo1();
frame = new JFrame("Demo 1");
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
// TODO Auto-generated method stub
super.windowClosing(e);
System.exit(0);
}
});
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
2、事件处理器
package com.jlwang.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ListenerDemo2
{
private static JFrame frame;
private static JPanel panel;
private static JButton button1;
private static JButton button2;
public ListenerDemo2()
{
super();
panel = new JPanel();
button1 = new JButton("button1");
button2 = new JButton("button2");
ListenerHandler listenerHandler = new ListenerHandler();
button1.addActionListener(listenerHandler);
button2.addActionListener(listenerHandler);
panel.add(button1);
panel.add(button2);
}
private class ListenerHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
String buttonName = e.getActionCommand();
if(buttonName.equals("button1"))
{
JOptionPane.showMessageDialog(frame, "button1 Clicked");
}else if(buttonName.equals("button2"))
{
JOptionPane.showMessageDialog(frame, "button2 Clicked!");
}
}
}
public static void main(String[] args)
{
ListenerDemo2 listenerDemo2 = new ListenerDemo2();
frame = new JFrame("listener demo2");
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.out.println("22222");
// TODO Auto-generated method stub
super.windowClosed(e);
System.exit(0);
}
});
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
3、事件处理器
package com.jlwang.swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class ListenerDemo3
{
private static JFrame frame;
private static JPanel panel;
private static JButton button1;
private static JButton button2;
public ListenerDemo3()
{
super();
panel = new JPanel();
button1 = new JButton("button1");
button2 = new JButton("button2");
button1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(frame, "button1 Clicked!");
}
});
button2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(frame, "button2 Clicked!");
}
});
panel.add(button1);
panel.add(button2);
}
public static void main(String[] args)
{
ListenerDemo3 listenerDemo2 = new ListenerDemo3();
frame = new JFrame("listener demo3");
frame.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
super.windowClosed(e);
System.exit(0);
}
});
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}