我写了一个程序,发现一个问题,如下:
public void mouseReleased(MouseEvent e)//鼠标按钮在组件上释放时调用
这个方法是鼠标按钮在组件上释放时调用,为什么我在组件上按下,然后将鼠标拖出组件后才释放也会调用??
程序代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Chen extends JPanel implements MouseListener
{
private JLabel[] l = new JLabel [3];
public Chen()
{
setPreferredSize (new Dimension (300, 100));
l[0] = new JLabel ("我");
l[1] = new JLabel (" 是");
l[2] = new JLabel ("白痴");
for (int i=0; i<3; i++)
{
l[i].setBorder (new EmptyBorder(2, 2, 2, 2));
l[i].addMouseListener (this);
add (l[i]);
}
}
public void mouseClicked(MouseEvent e)//鼠标按键在组件上单击(按下并释放)时调用
{
if (e.getSource() == l[0])
{
if (l[0].getText().equals("我"))
l[0].setText ("你");
else
l[0].setText ("我");
}
if (e.getSource() == l[1])
{
if (l[1].getText().equals(" 是"))
l[1].setText ("不是");
else
l[1].setText (" 是");
}
if (e.getSource() == l[2])
{
if (l[2].getText().equals("白痴"))
l[2].setText ("天才");
else
l[2].setText ("白痴");
}
}
public void mouseEntered(MouseEvent e)//鼠标进入到组件上时调用
{
JLabel l = (JLabel)e.getSource();
l.setBorder (new BevelBorder (BevelBorder.RAISED));
}
public void mouseExited(MouseEvent e)//鼠标离开组件时调用
{
JLabel l = (JLabel)e.getSource();
l.setBorder (new EmptyBorder(2, 2, 2, 2));
}
public void mousePressed(MouseEvent e)//鼠标按键在组件上按下时调用
{
JLabel l = (JLabel)e.getSource();
l.setBorder(new BevelBorder (BevelBorder.LOWERED));
}
public void mouseReleased(MouseEvent e)//鼠标按钮在组件上释放时调用
{
JLabel l = (JLabel)e.getSource();
l.setBorder(new BevelBorder (BevelBorder.RAISED));
}
public static void main (String[] args)
{
JFrame f = new JFrame ("GUI");
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
f.setResizable(false);//设置此窗体不可由用户调整大小
f.getContentPane().add (new Chen());
f.pack();
f.setVisible (true);
}
}