点击BUTTON后,控制台没有输出要aaa,刚开始接触JAV,请多关照!import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.ActionEvent;public class wyClient extends JFrame implements ActionListener {public wyClient(String title){
super(title);
this.setSize(300,200);
this.setLocation(150,250);
Container con=this.getContentPane();
con.setLayout(new GridLayout(2,1));
JPanel p2=new JPanel();
con.add(p2);
JButton bok=new JButton("OK");
JButton bcancel=new JButton("cancel"); p2.add(bok);
p2.add(bcancel);
}
public static void main(String args[]){
wyClient w=new wyClient("Login");
w.show();
}
public void actionPerformed(ActionEvent e){
System.out.println("aaa");
}
}
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.ActionEvent;public class wyClient extends JFrame implements ActionListener {public wyClient(String title){
super(title);
this.setSize(300,200);
this.setLocation(150,250);
Container con=this.getContentPane();
con.setLayout(new GridLayout(2,1));
JPanel p2=new JPanel();
con.add(p2);
JButton bok=new JButton("OK");
JButton bcancel=new JButton("cancel"); p2.add(bok);
p2.add(bcancel);
}
public static void main(String args[]){
wyClient w=new wyClient("Login");
w.show();
}
public void actionPerformed(ActionEvent e){
System.out.println("aaa");
}
}
bcancel.addActionListener(this);
这样改
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.ActionEvent;public class wyClient extends JFrame implements ActionListener { public wyClient(String title) {
super(title);
this.setSize(300, 200);
this.setLocation(150, 250);
Container con = this.getContentPane();
con.setLayout(new GridLayout(2, 1)); JPanel p2 = new JPanel(); con.add(p2); JButton bok = new JButton("OK");
JButton bcancel = new JButton("cancel");
bok.addActionListener(this);
p2.add(bok);
p2.add(bcancel); } public static void main(String args[]) {
wyClient w = new wyClient("Login");
w.setVisible(true);
} public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}