会JAVA的就进吧~鼠标事件提问
使得当鼠标在窗口中点击任何位置时,都会将按钮放在以点击位置为中心的位置,并在文本域中显示中心位置的坐标。importjava.awt.*;importjava.awt.e...
使得当鼠标在窗口中点击任何位置时
,都会将按钮放在以点击位置为中心的位置,并在文
本域中显示中心位置的坐标。
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
public class A{
public static void main(String[] args) {
MyWindow myWin = new MyWindow("鼠标运动事件测试");
}
}
class MyWindow extends Frame implements MouseListener{
Button b;
TextArea ta;
MyWindow(String s){
super(s);
setLayout(new FlowLayout());
setBounds(200, 100, 600, 300);
b = new Button("按钮");
add(b);
ta = new TextArea();
add(ta);
b.addMouseListener(this);
ta.addMouseListener(this);
addMouseListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
validate();
}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){
ta.append("鼠标点击,位置"+"("+e.getX()+","+e.getY()+")\n");
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){
Component c =(Component)e.getSource(); //这句话什么意思??
e = SwingUtilities.convertMouseEvent(c,e,this); // 这个又是什么意思??
int x,y,w,h;
x = e.getX();
y = e.getY();
w=c.getSize().width;
h=c.getSize().height;
c.setLocation(x-w/2,y-h/2);
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
我发现这里移动的位置完全不对,应该怎么改? 展开
,都会将按钮放在以点击位置为中心的位置,并在文
本域中显示中心位置的坐标。
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
public class A{
public static void main(String[] args) {
MyWindow myWin = new MyWindow("鼠标运动事件测试");
}
}
class MyWindow extends Frame implements MouseListener{
Button b;
TextArea ta;
MyWindow(String s){
super(s);
setLayout(new FlowLayout());
setBounds(200, 100, 600, 300);
b = new Button("按钮");
add(b);
ta = new TextArea();
add(ta);
b.addMouseListener(this);
ta.addMouseListener(this);
addMouseListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
validate();
}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){
ta.append("鼠标点击,位置"+"("+e.getX()+","+e.getY()+")\n");
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){
Component c =(Component)e.getSource(); //这句话什么意思??
e = SwingUtilities.convertMouseEvent(c,e,this); // 这个又是什么意思??
int x,y,w,h;
x = e.getX();
y = e.getY();
w=c.getSize().width;
h=c.getSize().height;
c.setLocation(x-w/2,y-h/2);
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
我发现这里移动的位置完全不对,应该怎么改? 展开
展开全部
Component c = (Component) e.getSource(); // 这句话什么意思?? 获得时间初始发生在哪个控件(Object)上。
e = SwingUtilities.convertMouseEvent(c, e, this); // 这个又是什么意思??
e = SwingUtilities.convertMouseEvent(c, e, this); // 这个又是什么意思?? 将在c (component)上发生的事件 传到 另个一个component 上
我发现这里移动的位置完全不对,应该怎么改? 稍后贴代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
public class A {
public static void main(String[] args) {
MyWindow myWin = new MyWindow("鼠标运动事件测试");
}
}
class MyWindow extends Frame implements MouseListener {
Button b;
TextArea ta;
MyWindow(String s) {
super(s);
setLayout(new FlowLayout());
setBounds(200, 100, 600, 300);
b = new Button("按钮");
add(b);
ta = new TextArea();
add(ta);
b.addMouseListener(this);
ta.addMouseListener(this);
addMouseListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
validate();
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
ta.append("鼠标点击,位置" + "(" + e.getX() + "," + e.getY() + ")\n");
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
Component c = (Component) e.getSource(); // 这句话什么意思??
e = SwingUtilities.convertMouseEvent(c, e, this); // 这个又是什么意思??
int x, y, w, h;
x = e.getX();
y = e.getY();
w = c.getSize().width;
h = c.getSize().height;
b.setLocation(x, y);
//c.setLocation(x - w / 2, y - h / 2);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
e = SwingUtilities.convertMouseEvent(c, e, this); // 这个又是什么意思??
e = SwingUtilities.convertMouseEvent(c, e, this); // 这个又是什么意思?? 将在c (component)上发生的事件 传到 另个一个component 上
我发现这里移动的位置完全不对,应该怎么改? 稍后贴代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
public class A {
public static void main(String[] args) {
MyWindow myWin = new MyWindow("鼠标运动事件测试");
}
}
class MyWindow extends Frame implements MouseListener {
Button b;
TextArea ta;
MyWindow(String s) {
super(s);
setLayout(new FlowLayout());
setBounds(200, 100, 600, 300);
b = new Button("按钮");
add(b);
ta = new TextArea();
add(ta);
b.addMouseListener(this);
ta.addMouseListener(this);
addMouseListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
validate();
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
ta.append("鼠标点击,位置" + "(" + e.getX() + "," + e.getY() + ")\n");
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
Component c = (Component) e.getSource(); // 这句话什么意思??
e = SwingUtilities.convertMouseEvent(c, e, this); // 这个又是什么意思??
int x, y, w, h;
x = e.getX();
y = e.getY();
w = c.getSize().width;
h = c.getSize().height;
b.setLocation(x, y);
//c.setLocation(x - w / 2, y - h / 2);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
追问
嗯,好,辛苦了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询