如何创建一个布尔局部变量作为 MouseEvent 的结果?
How to create a boolean local variable as a result of MouseEvent?
我刚刚开始学习 Java,我正在尝试正确初始化来自 MouseEvent 的 4 个变量。
我知道如何获取点击位置,但我不知道如何创建一个布尔型局部变量leftClicked,并在事件期间点击鼠标左键时将其初始化为true。
我已经尝试使用 if 语句来设置一个变量,如果 getButton() 返回 BUTTON1,该变量将结果为 true,但这不适用于我不应该更改的其余代码。
特别是,由于代码第二部分的 if 语句中的 "leftClicked",它不会编译。有人可以在语法和必要元素方面为我指出正确的方向吗?
我意识到这一定是一个非常简单的问题,但我无法理解它。非常感谢您!
/**
* Processes mouse clicks while the program is in browse mode.
* This method is called only after a mouse click is received while in browse mode.
* If a left mouse button click hits a photo, it enlarges the photo to a regular format.
* If a right mouse button click hits a photo, it shrinks the photo to a thumbnail format.
**/
private void doBrowsePhoto(MouseEvent e) {
// please write your code after this line
int x=e.getX();
int y=e.getY();
// boolean?
// code to be executed after the initialization of the variables x, y, leftClicked and rightClicked
ColorImage[] photos = model.getPhotos();
ColorImage photo;
double scale1, scale2, scaleMean;
for (int i=0; i<photos.length; i++) {
photo=photos[i];
if (isInsideImage(x, y, photo)) {
scale1=findThumbnailScale(photo);
scale2=findRegularScale(photo);
if (rightClicked) {
photo.setScale(scale1);
} else if (leftClicked){
photo.setScale(scale2);
}
break;
}
}
}
SwingUtilities.isLeftMouseButton(MouseEvent anEvent)
这个方法returns你想要什么。
你可以这样做:
boolean leftClicked = e.getButton() == MouseEvent.BUTTON1;
boolean rightClicked = e.getButton() == MouseEvent.BUTTON2;
我刚刚开始学习 Java,我正在尝试正确初始化来自 MouseEvent 的 4 个变量。
我知道如何获取点击位置,但我不知道如何创建一个布尔型局部变量leftClicked,并在事件期间点击鼠标左键时将其初始化为true。
我已经尝试使用 if 语句来设置一个变量,如果 getButton() 返回 BUTTON1,该变量将结果为 true,但这不适用于我不应该更改的其余代码。
特别是,由于代码第二部分的 if 语句中的 "leftClicked",它不会编译。有人可以在语法和必要元素方面为我指出正确的方向吗?
我意识到这一定是一个非常简单的问题,但我无法理解它。非常感谢您!
/**
* Processes mouse clicks while the program is in browse mode.
* This method is called only after a mouse click is received while in browse mode.
* If a left mouse button click hits a photo, it enlarges the photo to a regular format.
* If a right mouse button click hits a photo, it shrinks the photo to a thumbnail format.
**/
private void doBrowsePhoto(MouseEvent e) {
// please write your code after this line
int x=e.getX();
int y=e.getY();
// boolean?
// code to be executed after the initialization of the variables x, y, leftClicked and rightClicked
ColorImage[] photos = model.getPhotos();
ColorImage photo;
double scale1, scale2, scaleMean;
for (int i=0; i<photos.length; i++) {
photo=photos[i];
if (isInsideImage(x, y, photo)) {
scale1=findThumbnailScale(photo);
scale2=findRegularScale(photo);
if (rightClicked) {
photo.setScale(scale1);
} else if (leftClicked){
photo.setScale(scale2);
}
break;
}
}
}
SwingUtilities.isLeftMouseButton(MouseEvent anEvent)
这个方法returns你想要什么。
你可以这样做:
boolean leftClicked = e.getButton() == MouseEvent.BUTTON1;
boolean rightClicked = e.getButton() == MouseEvent.BUTTON2;