MouseDragged 有效或 mouseMoved 有效,但不能同时有效
MouseDragged works OR mouseMoved works, but not both
我目前正在做一项作业。我必须在框架上显示一组与鼠标相关的坐标。我不允许使用 Swing 组件。当使用鼠标而不点击时,文本应该是红色的。单击并拖动鼠标后,文本应为黑色。在这两种情况下,文本都应跟随鼠标。我可以让文本改变颜色,我可以让它显示适当的信息。当我尝试在 MouseMoved 和 MousedDragged 中设置标签的位置时,它无法识别我何时拖动。如果我为标签取出一个或另一个设置位置,另一个就可以正常工作。我究竟做错了什么?我正在研究跑步者 class,我会把它包括在内。 main class 只是实例化了我的 runner class 的一个对象。
import java.awt.Button;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.*;
import java.awt.Rectangle ;
import java.awt.Label;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
class Proj06Runner extends Frame
{
//Proj06Runner Constructor
public Proj06Runner() {
System.out.println("Proj06");
System.out.println("I certify that this program is my own work and is not the work of others. ");
System.out.println("I agree not to share my solution with others.");
System.out.println("Eric Jackson.");
GUI gui = new GUI();//instantiate a GUI
}}
//End Proj06Runner Class
//________________________________________________________________________
class GUI extends Frame {
public GUI()//constructor
{
Rectangle bounds = this.getBounds();
//Create Frame
Frame displayWindow = new Frame("Eric Jackson");
//Set size of Frame
displayWindow.setSize(300,200);
displayWindow.setLayout(null);
displayWindow.setLocation(30 + bounds.x, 30 + bounds.y);
//Create Top button
Button TopButton = new Button("This Button does nothing");
TopButton.setBounds(7+bounds.x,30+bounds.y,285,25);
displayWindow.add(TopButton);
//Create Left button
Button LeftButton = new Button("Button");
LeftButton.setBounds(7+bounds.x,54+bounds.y,50,122);
displayWindow.add(LeftButton);
//Create Textfield
TextField myTextField=new TextField("This TextField does nothing");
myTextField.setBounds(7+bounds.x,175+bounds.y,285,22);
displayWindow.add(myTextField);
//Create Label to display coord text
Label myLabel=new Label();
myLabel.setBounds(50,50,100,20);
myLabel.setForeground(Color.red);
myLabel.setText(" ");
displayWindow.add(myLabel);
displayWindow.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} );
displayWindow.addMouseMotionListener(new MyMouseMotionProcessor(myLabel));
displayWindow.addMouseListener(new MyMouseProcessor(myLabel));
displayWindow.setVisible(true);
}
}
//End GUI Definition
//=========================================================
//=========================================================
//This class recognizes mousePressed(). This method is
// used to determine the starting position of the mouse
// pointer.
class MyMouseProcessor extends MouseAdapter{
Label theLabel;
int MouseX, MouseY;
//Constructor
MyMouseProcessor( Label inLabel){
//save references to the input objects
theLabel = inLabel;
}//end constructor
public void mousePressed(MouseEvent e){
}//end mousePressed()
}//end MyMouseProcessor
//=======================================================
class MyMouseMotionProcessor extends MouseMotionAdapter{
Label theLabel;
int MouseX, MouseY;
//Constructor
MyMouseMotionProcessor(Label inLabel){
//save incoming object reference
theLabel = inLabel;
}// end constructor
public void mouseDragged(MouseEvent e){
System.out.println("Drag = " + e);
MouseX= e.getX()-2;
if (MouseX<0)
{
MouseX=0;
}
MouseY= e.getY()-15;
if (MouseY <0)
{
MouseY=0;
}
//move label to the new location
theLabel.setLocation(MouseX,MouseY);
theLabel.setForeground(Color.black);
String coordtextClick = MouseX+" , " + MouseY;
theLabel.setText(coordtextClick);
}//end mouseDragged()
public void mouseMoved(MouseEvent e){
System.out.println("Move = " + e);
theLabel.setForeground(Color.red);
MouseX= e.getX()-2;
if (MouseX<0)
{
MouseX=0;
}
MouseY= e.getY()-4;
if (MouseY <0)
{
MouseY=0;
}
String coordtext = MouseX+" , " + MouseY;
theLabel.setLocation(MouseX,MouseY);
theLabel.setText(coordtext);
}//
}//end class MyMouseMotionProcessor
mouseDragged
和mouseMoved
一起使用没有问题。他们工作得很好。问题在于您设置为 theLabel
的位置。
问题:
问题是,当您在 mouseMoved
方法中设置 theLabel
的位置时,theLabel
位于鼠标指针的正下方。所以你不能创建一个 healthy mouseDrag 手势,它应该发生在你的 displayWindow
组件上。换句话说,你的 mouseDrag 被应用到 theLabel
本身而不是 displayWindow
,因为 theLabel
位于displayWindow
.
我是怎么发现的?我又使出了老招数!我刚刚将背景颜色设置为 theLabel
:
myLabel.setBackground(Color.lightGray);
这帮助我了解了 myLabel
所在的位置,从而解决了问题!
解法:
解决方案是为 theLabel
设置一个不在鼠标指针正下方的位置。这将有助于从鼠标位置启动健康的 mouseDrag。像这样:
public void mouseDragged(MouseEvent e) {
System.out.println("Drag = " + e);
MouseX = e.getX() + 5; // <- note here
if (MouseX < 0) {
MouseX = 0;
}
MouseY = e.getY() - 15; // <- note here
if (MouseY < 0) {
MouseY = 0;
}
// move label to the new location
theLabel.setLocation(MouseX, MouseY);
theLabel.setForeground(Color.black);
String coordtextClick = MouseX + " , " + MouseY;
theLabel.setText(coordtextClick);
}// end mouseDragged()
public void mouseMoved(MouseEvent e) {
System.out.println("Move = " + e);
theLabel.setForeground(Color.red);
MouseX = e.getX() + 5; // <- note here
if (MouseX < 0) {
MouseX = 0;
}
MouseY = e.getY() - 15; // <- note here
if (MouseY < 0) {
MouseY = 0;
}
String coordtext = MouseX + " , " + MouseY;
theLabel.setLocation(MouseX, MouseY);
theLabel.setText(coordtext);
}//
希望对您有所帮助!
我目前正在做一项作业。我必须在框架上显示一组与鼠标相关的坐标。我不允许使用 Swing 组件。当使用鼠标而不点击时,文本应该是红色的。单击并拖动鼠标后,文本应为黑色。在这两种情况下,文本都应跟随鼠标。我可以让文本改变颜色,我可以让它显示适当的信息。当我尝试在 MouseMoved 和 MousedDragged 中设置标签的位置时,它无法识别我何时拖动。如果我为标签取出一个或另一个设置位置,另一个就可以正常工作。我究竟做错了什么?我正在研究跑步者 class,我会把它包括在内。 main class 只是实例化了我的 runner class 的一个对象。
import java.awt.Button;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.*;
import java.awt.Rectangle ;
import java.awt.Label;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
class Proj06Runner extends Frame
{
//Proj06Runner Constructor
public Proj06Runner() {
System.out.println("Proj06");
System.out.println("I certify that this program is my own work and is not the work of others. ");
System.out.println("I agree not to share my solution with others.");
System.out.println("Eric Jackson.");
GUI gui = new GUI();//instantiate a GUI
}}
//End Proj06Runner Class
//________________________________________________________________________
class GUI extends Frame {
public GUI()//constructor
{
Rectangle bounds = this.getBounds();
//Create Frame
Frame displayWindow = new Frame("Eric Jackson");
//Set size of Frame
displayWindow.setSize(300,200);
displayWindow.setLayout(null);
displayWindow.setLocation(30 + bounds.x, 30 + bounds.y);
//Create Top button
Button TopButton = new Button("This Button does nothing");
TopButton.setBounds(7+bounds.x,30+bounds.y,285,25);
displayWindow.add(TopButton);
//Create Left button
Button LeftButton = new Button("Button");
LeftButton.setBounds(7+bounds.x,54+bounds.y,50,122);
displayWindow.add(LeftButton);
//Create Textfield
TextField myTextField=new TextField("This TextField does nothing");
myTextField.setBounds(7+bounds.x,175+bounds.y,285,22);
displayWindow.add(myTextField);
//Create Label to display coord text
Label myLabel=new Label();
myLabel.setBounds(50,50,100,20);
myLabel.setForeground(Color.red);
myLabel.setText(" ");
displayWindow.add(myLabel);
displayWindow.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} );
displayWindow.addMouseMotionListener(new MyMouseMotionProcessor(myLabel));
displayWindow.addMouseListener(new MyMouseProcessor(myLabel));
displayWindow.setVisible(true);
}
}
//End GUI Definition
//=========================================================
//=========================================================
//This class recognizes mousePressed(). This method is
// used to determine the starting position of the mouse
// pointer.
class MyMouseProcessor extends MouseAdapter{
Label theLabel;
int MouseX, MouseY;
//Constructor
MyMouseProcessor( Label inLabel){
//save references to the input objects
theLabel = inLabel;
}//end constructor
public void mousePressed(MouseEvent e){
}//end mousePressed()
}//end MyMouseProcessor
//=======================================================
class MyMouseMotionProcessor extends MouseMotionAdapter{
Label theLabel;
int MouseX, MouseY;
//Constructor
MyMouseMotionProcessor(Label inLabel){
//save incoming object reference
theLabel = inLabel;
}// end constructor
public void mouseDragged(MouseEvent e){
System.out.println("Drag = " + e);
MouseX= e.getX()-2;
if (MouseX<0)
{
MouseX=0;
}
MouseY= e.getY()-15;
if (MouseY <0)
{
MouseY=0;
}
//move label to the new location
theLabel.setLocation(MouseX,MouseY);
theLabel.setForeground(Color.black);
String coordtextClick = MouseX+" , " + MouseY;
theLabel.setText(coordtextClick);
}//end mouseDragged()
public void mouseMoved(MouseEvent e){
System.out.println("Move = " + e);
theLabel.setForeground(Color.red);
MouseX= e.getX()-2;
if (MouseX<0)
{
MouseX=0;
}
MouseY= e.getY()-4;
if (MouseY <0)
{
MouseY=0;
}
String coordtext = MouseX+" , " + MouseY;
theLabel.setLocation(MouseX,MouseY);
theLabel.setText(coordtext);
}//
}//end class MyMouseMotionProcessor
mouseDragged
和mouseMoved
一起使用没有问题。他们工作得很好。问题在于您设置为 theLabel
的位置。
问题:
问题是,当您在 mouseMoved
方法中设置 theLabel
的位置时,theLabel
位于鼠标指针的正下方。所以你不能创建一个 healthy mouseDrag 手势,它应该发生在你的 displayWindow
组件上。换句话说,你的 mouseDrag 被应用到 theLabel
本身而不是 displayWindow
,因为 theLabel
位于displayWindow
.
我是怎么发现的?我又使出了老招数!我刚刚将背景颜色设置为 theLabel
:
myLabel.setBackground(Color.lightGray);
这帮助我了解了 myLabel
所在的位置,从而解决了问题!
解法:
解决方案是为 theLabel
设置一个不在鼠标指针正下方的位置。这将有助于从鼠标位置启动健康的 mouseDrag。像这样:
public void mouseDragged(MouseEvent e) {
System.out.println("Drag = " + e);
MouseX = e.getX() + 5; // <- note here
if (MouseX < 0) {
MouseX = 0;
}
MouseY = e.getY() - 15; // <- note here
if (MouseY < 0) {
MouseY = 0;
}
// move label to the new location
theLabel.setLocation(MouseX, MouseY);
theLabel.setForeground(Color.black);
String coordtextClick = MouseX + " , " + MouseY;
theLabel.setText(coordtextClick);
}// end mouseDragged()
public void mouseMoved(MouseEvent e) {
System.out.println("Move = " + e);
theLabel.setForeground(Color.red);
MouseX = e.getX() + 5; // <- note here
if (MouseX < 0) {
MouseX = 0;
}
MouseY = e.getY() - 15; // <- note here
if (MouseY < 0) {
MouseY = 0;
}
String coordtext = MouseX + " , " + MouseY;
theLabel.setLocation(MouseX, MouseY);
theLabel.setText(coordtext);
}//
希望对您有所帮助!