java鼠标单击的监听问题

求个大牛帮忙解答一下:
我写的这个程序中,Mytestawt_1类继承Frame并且实现了MouseListenner接口,可是运行之后鼠标点击按钮b1,b2,b3却没有被监听到过,求一位大神解答,再次谢过!
代码如下:

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyTestawt_1 extends Frame implements MouseListener {
//定义按钮以及面板布局
private Button b1 = new Button("第一页");
private Button b2 = new Button("第二页");
private Button b3 = new Button("第三页");
private Panel p2 = new Panel();
private Panel p1 = new Panel();
private CardLayout cl = new CardLayout();
//构造方法
public MyTestawt_1(String str){
super(str);
init();
}
//定义初始化方法
public void init(){
//设置属性
setSize(400, 300);
setLayout(new BorderLayout());
//设置上方的控制按钮
add(BorderLayout.NORTH,p1);
p1.setLayout(new FlowLayout());
p1.add(b1);
p1.add(b2);
p1.add(b3);

add(BorderLayout.CENTER,p2);
p2.setLayout(cl);
p2.add("第一页",new Button("第一页的内容"));
p2.add("第二页",new Button("第二页的内容"));
p2.add("第三页",new Button("第三页的内容"));
//设置可见
setVisible(true);
//实现窗口关闭功能
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we){
setVisible(false);
dispose();
System.out.println("执行到此3");
System.exit(0);
}
});
}

public static void main(String[] args) {
//程序入口
new MyTestawt_1("网格型布局");

}
@Override
public void mouseClicked(MouseEvent e) {
//监听鼠标点击动作
if (e.getSource()==b1) {
System.out.println("执行到此1");
cl.first(p2);
System.out.println("执行到此2");
}else if (e.getSource()==b2) {
cl.first(p2);
cl.next(p2);
} else if (e.getSource()==b3) {
cl.last(p2);
}

}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

}

第1个回答  2014-02-03
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyTestawt_1 extends Frame {//implements MouseListener {
//定义按钮以及面板布局
private Button b1 = new Button("第一页");
private Button b2 = new Button("第二页");
private Button b3 = new Button("第三页");
private Panel p2 = new Panel();
private Panel p1 = new Panel();
private CardLayout cl = new CardLayout();
//构造方法
public MyTestawt_1(String str){
super(str);
init();
}
//定义初始化方法
public void init(){
//设置属性
setSize(400, 300);
setLayout(new BorderLayout());
//设置上方的控制按钮
add(BorderLayout.NORTH,p1);
p1.setLayout(new FlowLayout());
p1.add(b1);
p1.add(b2);
p1.add(b3);

add(BorderLayout.CENTER,p2);
p2.setLayout(cl);
p2.add("第一页",new Button("第一页的内容"));
p2.add("第二页",new Button("第二页的内容"));
p2.add("第三页",new Button("第三页的内容"));
//设置可见
setVisible(true);
//实现窗口关闭功能
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we){
setVisible(false);
dispose();
System.out.println("执行到此3");
System.exit(0);
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("执行到此1");
cl.first(p2);
System.out.println("执行到此2");
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cl.first(p2);
cl.next(p2);
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
cl.last(p2);
}
});
}

public static void main(String[] args) {
//程序入口
new MyTestawt_1("网格型布局");

}

}追问

你好,这个方法我也知道,但是太过于繁琐了,感觉不利于处理大批的组件,不知道有没有继承接口的方法?

追答

public class MyTestawt_1 extends Frame implements ActionListener {
// 定义初始化方法
public void init() {
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(b1)) {
System.out.println("执行到此1");
cl.first(p2);
System.out.println("执行到此2");
} else if (e.getSource().equals(b2)) {
cl.first(p2);
cl.next(p2);
} else if (e.getSource().equals(b3)) {
cl.last(p2);
}
}

第2个回答  2014-02-03

你只是实现了一个MouseListener,但是却没有将这个MouseListener添加到任何一个控件上,添加代码

b1.addMouseListener(this);
b2.addMouseListener(this);
b3.addMouseListener(this);

追问

是的,我忘了添加了。能否告知一下这个this是指代什么?

追答

当前对象自身,因为当前对象实现了MouseListener。

追问

多谢,明白了

本回答被提问者采纳
第3个回答  2014-02-03
你这个监听也没加到按钮上啊 你是覆盖的JFrame的追问

但是三个按钮是添加到了Frame容器下的panel上的,点击按钮的时候,在frame监听不到的吗?

追答

肯定监听不到啊 那是两个东西 JButton的监听 必须要加载JBuuton的实例上b1.addActionListener(new ActionListener() {}

比如你身上去个苍蝇 打你肯定不是苍蝇疼 你得打苍蝇 苍蝇才会监听处理

相似回答