import javax.swing.*;
import java.awt.event.*;
public class HandleEvent extends JFrame{
public HandleEvent(){
JButton jbtOK = new JButton("OK!");
JButton jbtCancel = new JButton("cancel");
JPanel panel = new JPanel();
panel.add(jbtOK);
panel.add(jbtCancel);
add(panel);//用来在JFrame上添加panel按钮的。
OKListenerClass listener1 = new OKListenerClass();
jbtOK.addActionListener(listener1);
CancelListenerClass listener2 = new CancelListenerClass();//先需要声明一下
jbtCancel.addActionListener(listener2);
}
public static void main(String args[]){
JFrame frame = new HandleEvent();
frame.setTitle(" ");
frame.setSize(400, 200);
frame.setLocation(400,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class OKListenerClass implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println("OK button");
}
}
class CancelListenerClass implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Cancel button clicked");
}
}
import java.awt.event.*;
public class HandleEvent extends JFrame{
public HandleEvent(){
JButton jbtOK = new JButton("OK!");
JButton jbtCancel = new JButton("cancel");
JPanel panel = new JPanel();
panel.add(jbtOK);
panel.add(jbtCancel);
add(panel);//用来在JFrame上添加panel按钮的。
OKListenerClass listener1 = new OKListenerClass();
jbtOK.addActionListener(listener1);
CancelListenerClass listener2 = new CancelListenerClass();//先需要声明一下
jbtCancel.addActionListener(listener2);
}
public static void main(String args[]){
JFrame frame = new HandleEvent();
frame.setTitle(" ");
frame.setSize(400, 200);
frame.setLocation(400,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class OKListenerClass implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println("OK button");
}
}
class CancelListenerClass implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Cancel button clicked");
}
}