1、
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class MyFrame extends JFrame {
public MyFrame(){
//
setDefaultCloseOperation(EXIT_ON_CLOSE);
//
JPanel jp = new JPanel();
//
JButton jb = new JButton(" ");
//
this.add(jp);
//
jp.add(jb);
//
jb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
}
});
}
}
//2、
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class MyFrame extends JFrame {
/**
*
*/
static class InnerClass implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
}
}
public MyFrame(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jp = new JPanel();
JButton jb = new JButton(" ");
this.add(jp);
jp.add(jb);
jb.addActionListener(new InnerClass());
}
}
//3、
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class OutClass implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
}
}
class MyFrame extends JFrame {
public MyFrame(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jp = new JPanel();
JButton jb = new JButton(" ");
this.add(jp);
jp.add(jb);
jb.addActionListener(new OutClass());
}
}
//4、 addActionListener
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
class MyFrame extends JFrame implements ActionListener {
public MyFrame(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jp = new JPanel();
JButton jb = new JButton(" ");
this.add(jp);
jp.add(jb);
jb.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
}
}