大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
前提:你容器使用的布局需要null,这样你容器上其它组件都需要自己控制大小和位置了。
成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站制作、网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的都安网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
问题:你需要显示的鼠标坐标是基于屏幕还是容器还是窗口还是按钮?
给你个简单的显示鼠标基于窗口坐标的例子,鼠标坐标显示在窗口标题栏上。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TraceMouse extends JFrame{
JButton btnTrace;
boolean showMousePosition;
Insets insets;
public TraceMouse(){
setSize(640,480);
setLayout(null);
btnTrace=new JButton("点我显示鼠标坐标");
btnTrace.setSize(btnTrace.getPreferredSize());
add(btnTrace);
btnTrace.addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent e){
Point position=getMousePosition();
btnTrace.setLocation(position.x-insets.left-btnTrace.getWidth()/2,position.y-insets.top-btnTrace.getHeight()/2);
TraceMouse.this.setTitle(String.format("当前鼠标坐标为:(%1$d,%2$d)",position.x,position.y));
}
public void mouseMoved(MouseEvent e){
Point position=getMousePosition();
btnTrace.setLocation(position.x-insets.left-btnTrace.getWidth()/2,position.y-insets.top-btnTrace.getHeight()/2);
}
});
addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
Point position=getMousePosition();
btnTrace.setLocation(position.x-insets.left-btnTrace.getWidth()/2,position.y-insets.top-btnTrace.getHeight()/2);
}
});
setVisible(true);
insets=getInsets();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args){
new TraceMouse();
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
public class MouseDrawLine extends JFrame {
int x, y;
int endX, endY;
public MouseDrawLine() {
addMouseListener(new MouseAdapter() {
//鼠标下压事件
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
//鼠标弹起事件
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("鼠标弹起事件");
}
});
addMouseMotionListener(new MouseMotionAdapter() {
//鼠标下压移动事件
@Override
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
g.setColor(Color.BLACK);
endX = e.getX();
endY = e.getY();
g.drawLine(x, y, endX, endY);
x = endX;
y = endY;
}
});
setSize(600, 500);
setLocation(200,100);
setVisible(true);
setDefaultCloseOperation(3);
}
public static void main(String[] args) {
new MouseDrawLine();
}
}
楼主提问的水平很高啊,题目的要求都没有,如何回答。。。
相关类都在
import java.awt.*;
import javax.swing.* ;
动作事件相关类在import java.awt.event.*;中,在代码头import就可以看了
给一个相关的代码吧,包括所有的鼠标事件。
下面代码包括
按下,释放,进入区域,离开区域,点击 这5个事件
鼠标的相关事件会在窗口上方的文本区显示。。。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestMouseEvent extends JFrame implements MouseListener{
JPanel jp = new JPanel();
JTextField jtf = new JTextField();
public TestMouseEvent(){
setTitle("Mouse Event");
getContentPane().add(jtf,BorderLayout.NORTH);
getContentPane().add(jp,BorderLayout.CENTER);
jp.addMouseListener(this);
}
public static void main(String[] args){
TestMouseEvent frame = new TestMouseEvent();
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void mousePressed(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出按下";
jtf.setText(s);
}
public void mouseReleased(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出释放";
jtf.setText(s);
}
public void mouseClicked(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出点击";
jtf.setText(s);
}
public void mouseEntered(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出进入";
jtf.setText(s);
}
public void mouseExited(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出离开";
jtf.setText(s);
}
}