java的鼠标监听代码,语法没错,但是运行却错了?
packagepackage1;importjava.awt.BorderLayout;importjava.awt.Button;importjava.awt.Cont...
package package1;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MutiListenerTest implements MouseListener, MouseMotionListener
{
JTextField jf = new JTextField(30);
public void MutiListenerTest()
{
JFrame f = new JFrame("多重监听器");
JLabel a = new JLabel("请按下鼠标且拖动");
f.add(jf,"North");
f.add(a,"South");
f.addMouseListener(this);
f.addMouseMotionListener(this);
f.setBounds(300, 300, 300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args)
{
new MutiListenerTest();
}
public void mouseMoved(MouseEvent arg0){
}
public void mouseClicked(MouseEvent arg0)
{
}
public void mousePressed(MouseEvent arg0){
}
public void mouseReleased(MouseEvent arg0)
{
}
public void mouseDragged(MouseEvent arg0)
{
//String str = "鼠标被拖动,当前位置:"+arg0.getX()+","+arg0.getY();
//jf.setText(str);
}
public void mouseEntered(MouseEvent arg0)
{
//String str = "鼠标进入窗体";
//jf.setText(str);
}
public void mouseExited(MouseEvent arg0)
{
//String str = "鼠标离开窗体";
//jf.setText(str);
}
} 展开
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MutiListenerTest implements MouseListener, MouseMotionListener
{
JTextField jf = new JTextField(30);
public void MutiListenerTest()
{
JFrame f = new JFrame("多重监听器");
JLabel a = new JLabel("请按下鼠标且拖动");
f.add(jf,"North");
f.add(a,"South");
f.addMouseListener(this);
f.addMouseMotionListener(this);
f.setBounds(300, 300, 300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args)
{
new MutiListenerTest();
}
public void mouseMoved(MouseEvent arg0){
}
public void mouseClicked(MouseEvent arg0)
{
}
public void mousePressed(MouseEvent arg0){
}
public void mouseReleased(MouseEvent arg0)
{
}
public void mouseDragged(MouseEvent arg0)
{
//String str = "鼠标被拖动,当前位置:"+arg0.getX()+","+arg0.getY();
//jf.setText(str);
}
public void mouseEntered(MouseEvent arg0)
{
//String str = "鼠标进入窗体";
//jf.setText(str);
}
public void mouseExited(MouseEvent arg0)
{
//String str = "鼠标离开窗体";
//jf.setText(str);
}
} 展开
2015-03-15
展开全部
public void MutiListenerTest() 如果是构造函数,不应该要void关键字
界面中,被 f.add(jf,"North");
f.add(a,"South");
占据了,
却只添加jframe 的事件
f.addMouseListener(this);
f.addMouseMotionListener(this);
这样是点击不到jframe的。
界面中,被 f.add(jf,"North");
f.add(a,"South");
占据了,
却只添加jframe 的事件
f.addMouseListener(this);
f.addMouseMotionListener(this);
这样是点击不到jframe的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询