当前位置: 技术问答>java相关
为什么JPanel不响应mouse事件。详情见内。谢谢
来源: 互联网 发布时间:2015-03-31
本文导语: import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class Test extends JFrame { public static void main(String[] args) { JFrame t=new Test(); ...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JFrame
{
public static void main(String[] args)
{
JFrame t=new Test();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setSize(200,300);
t.setVisible(true);
}
public Test()
{
JPanel b=new JMyPanel();
b.setBorder(BorderFactory.createRaisedBevelBorder());
b.setLayout(new BorderLayout());
JPanel p=new JPanel();
p.setBorder(BorderFactory.createLoweredBevelBorder());
p.add(new JButton("1"));
p.add(new JButton("2"));
getContentPane().add(b);
b.add(p,BorderLayout.NORTH);
}
class JMyPanel extends JPanel implements MouseListener{
public void mousePressed(MouseEvent e){
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e){
System.out.println("mouseReleased");
}
public void mouseClicked(MouseEvent e){
System.out.println("mouseClicked");
}
public void mouseEntered(MouseEvent e){
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent e){
System.out.println("mouseExited");
}
}
}
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JFrame
{
public static void main(String[] args)
{
JFrame t=new Test();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setSize(200,300);
t.setVisible(true);
}
public Test()
{
JPanel b=new JMyPanel();
b.setBorder(BorderFactory.createRaisedBevelBorder());
b.setLayout(new BorderLayout());
JPanel p=new JPanel();
p.setBorder(BorderFactory.createLoweredBevelBorder());
p.add(new JButton("1"));
p.add(new JButton("2"));
getContentPane().add(b);
b.add(p,BorderLayout.NORTH);
}
class JMyPanel extends JPanel implements MouseListener{
public void mousePressed(MouseEvent e){
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e){
System.out.println("mouseReleased");
}
public void mouseClicked(MouseEvent e){
System.out.println("mouseClicked");
}
public void mouseEntered(MouseEvent e){
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent e){
System.out.println("mouseExited");
}
}
}
|
你没有加Listener--------addMouseListener(this)
|
注意下面代码的变化
public Test()
{
JPanel b=new JPanel();
b.setBorder(BorderFactory.createRaisedBevelBorder());
b.setLayout(new BorderLayout());
JPanel p=new JMyPanel();
p.addMouseListener(p); ////// 把这一句加上试试
p.setBorder(BorderFactory.createLoweredBevelBorder());
p.add(new JButton("1"));
p.add(new JButton("2"));
getContentPane().add(b);
b.add(p,BorderLayout.NORTH);
}
public Test()
{
JPanel b=new JPanel();
b.setBorder(BorderFactory.createRaisedBevelBorder());
b.setLayout(new BorderLayout());
JPanel p=new JMyPanel();
p.addMouseListener(p); ////// 把这一句加上试试
p.setBorder(BorderFactory.createLoweredBevelBorder());
p.add(new JButton("1"));
p.add(new JButton("2"));
getContentPane().add(b);
b.add(p,BorderLayout.NORTH);
}