永发信息网

会JAVA的就进吧~鼠标事件提问

答案:2  悬赏:20  手机版
解决时间 2021-12-29 11:02
  • 提问者网友:嗝是迷路的屁
  • 2021-12-28 10:16
使得当鼠标在窗口中点击任何位置时
,都会将按钮放在以点击位置为中心的位置,并在文
本域中显示中心位置的坐标。

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){}

}

我发现这里移动的位置完全不对,应该怎么改?
最佳答案
  • 五星知识达人网友:千杯敬自由
  • 2021-12-28 10:59
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) {
}
全部回答
  • 1楼网友:duile
  • 2021-12-28 11:21
你的判断有问题。 将 if(current==null) { int x=event.getx(); int y=event.gety(); current.setframe(x-a/2, y-a/2, a, a); repaint(); system.out.println("dragged"); } 改成: if(current!=null) { int x=event.getx(); int y=event.gety(); current.setframe(x-a/2, y-a/2, a, a); repaint(); system.out.println("dragged"); } 然后用鼠标抓取窗口就可以打印dragged。 理由:在mouseframe的构造器中,调用了add(),而add()方法执行过了那么current就不可能为空了。所以你的if(current==null)始终为false,故一直走不到判断里面的语句。就算current真的为null,那么判断中的current.setframe(x-a/2, y-a/2, a, a)也会报空指针异常了。 希望能帮到你。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯