当前位置: 技术问答>java相关
测试event响应,,为什么没动静?
来源: 互联网 发布时间:2015-07-30
本文导语: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class test extends JPanel { private ActionListener al=null; test(){ addMouseListener(new MH()); } public void addActionListener(ActionListener a){ System.out.println("registing a action...
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class test extends JPanel {
private ActionListener al=null;
test(){
addMouseListener(new MH());
}
public void addActionListener(ActionListener a){
System.out.println("registing a actionlistener");
al=a;
System.out.println("register listener over"); System.out.print(al);
}
class MH extends MouseAdapter{
//ActionEvent a =new ActionEvent(test.this,ActionEvent.ACTION_PERFORMED, null);
public void mousePressed(MouseEvent e){
System.out.print("here al"+al);
al.actionPerformed(new ActionEvent(test.this,ActionEvent.ACTION_PERFORMED, null));
}
}
public static void main(String args[]){
JFrame jf=new JFrame("test");
Container c=jf.getContentPane();
c.setLayout(new FlowLayout());
test t=new test();
System.out.println("i want add a listener");
t.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
System.out.println("actionevent 1");
}
});
// t.addActionListener(new ActionListener(){
// public void actionPerformed(ActionEvent a){
// System.out.println("actionevent 2");
// }
// });
c.add(t);
jf.setSize(400,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//因该输出actionevent 2,可是怎么没响应,请高手指教,谢了.
import java.awt.event.*;
import java.awt.*;
public class test extends JPanel {
private ActionListener al=null;
test(){
addMouseListener(new MH());
}
public void addActionListener(ActionListener a){
System.out.println("registing a actionlistener");
al=a;
System.out.println("register listener over"); System.out.print(al);
}
class MH extends MouseAdapter{
//ActionEvent a =new ActionEvent(test.this,ActionEvent.ACTION_PERFORMED, null);
public void mousePressed(MouseEvent e){
System.out.print("here al"+al);
al.actionPerformed(new ActionEvent(test.this,ActionEvent.ACTION_PERFORMED, null));
}
}
public static void main(String args[]){
JFrame jf=new JFrame("test");
Container c=jf.getContentPane();
c.setLayout(new FlowLayout());
test t=new test();
System.out.println("i want add a listener");
t.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
System.out.println("actionevent 1");
}
});
// t.addActionListener(new ActionListener(){
// public void actionPerformed(ActionEvent a){
// System.out.println("actionevent 2");
// }
// });
c.add(t);
jf.setSize(400,200);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//因该输出actionevent 2,可是怎么没响应,请高手指教,谢了.
|
代码本身没有多大的问题。问题出在布局管理器的使用,如使用默认的BorderLayout,效果非常明显。但即便使用FlowLayout也有响应,只是响应的区间非常狭小。原因大概是FlowLayout这种布局管理器适合于可视部件,如button或checkbox,亦或该例程对FlowLayout这种布局管理器的控制不当所致。
|
think in java有这样一句话:
All components will be compacted to their smallest size in a FlowLayout, so you might get a little bit of surprising behavior. For example, because a JLabel will be the size of its string, attempting to right-justify its text yields an unchanged display when using FlowLayout。
而panel是作为容器组件使用的,当他本身不容纳其他组件时,大小为零,所以无法响应你的动作。而BorderLayout布局方式不同,他会占据尽可能大的空间,你的frame那没有别的组件,它就把它占满了,你点击任何地方都会得到响应的。
All components will be compacted to their smallest size in a FlowLayout, so you might get a little bit of surprising behavior. For example, because a JLabel will be the size of its string, attempting to right-justify its text yields an unchanged display when using FlowLayout。
而panel是作为容器组件使用的,当他本身不容纳其他组件时,大小为零,所以无法响应你的动作。而BorderLayout布局方式不同,他会占据尽可能大的空间,你的frame那没有别的组件,它就把它占满了,你点击任何地方都会得到响应的。
|
你什么意思?
System.out.println("actionevent 2");
这句不是被你屏蔽了吗?
System.out.println("actionevent 2");
这句不是被你屏蔽了吗?
|
我觉得你对事件的理解不太对,在这里你的程序继承了Jpanel,但Jpanel不会产生action事件。还有好像addActionListener()是不要自己去实现的吧?
[我也不太懂,仅供参考]
[我也不太懂,仅供参考]
|
JPanel 的事件应该是ComponentListener而不是ActionListener,所以actionPerformed事件从来不会被触发,如果你把public class test extends JPanel 换成public class test extends JButton,则在按钮上点击时,会输出
“actionevent 1”,但
System.out.println("registing a actionlistener");
al=a;
System.out.println("register listener over"); System.out.print(al);
这段代码是没有意义的,也许只会在程序运行的开始会执行,
t.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
System.out.println("actionevent 1");
}
实际上是用到了匿名类,addMouseListener(new MH());这样的写法才是正确的!
“actionevent 1”,但
System.out.println("registing a actionlistener");
al=a;
System.out.println("register listener over"); System.out.print(al);
这段代码是没有意义的,也许只会在程序运行的开始会执行,
t.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
System.out.println("actionevent 1");
}
实际上是用到了匿名类,addMouseListener(new MH());这样的写法才是正确的!