Java 如何创建一个布尔局部变量作为MouseEvent的结果?
我刚刚开始学习Java,我正在尝试正确初始化来自MouseeEvent的4个变量 我知道如何检索单击位置,但我不知道如何创建布尔局部变量leftClicked,并在事件期间单击鼠标左键时将其初始化为true 我曾尝试使用if语句设置一个变量,如果getButton()返回BUTTON1,该变量的结果将为true,但这对代码的其余部分不起作用,我不希望对其进行更改 特别是,由于代码第二部分中if语句中的“leftClicked”,它不会编译。有人能给我指出语法和必要元素的正确方向吗 我意识到这一定是一个非常简单的问题,但我无法理解。提前非常感谢Java 如何创建一个布尔局部变量作为MouseEvent的结果?,java,mouseevent,boolean-expression,Java,Mouseevent,Boolean Expression,我刚刚开始学习Java,我正在尝试正确初始化来自MouseeEvent的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;
}
}
}
/**
*在程序处于浏览模式时处理鼠标单击。
*只有在浏览模式下收到鼠标单击后才调用此方法。
*如果鼠标左键点击照片,它会将照片放大为常规格式。
*如果鼠标右键单击照片,则会将照片缩小为缩略图格式。
**/
私人void doBrowsePhoto(MouseEvent e){
//请在这行后面写代码
int x=e.getX();
int y=e.getY();
//布尔值?
//初始化变量x、y、leftClicked和rightClicked后要执行的代码
ColorImage[]photos=model.getPhotos();
彩色图像照片;
双刻度1、刻度2、刻度面;
对于(int i=0;i
此方法返回您想要的内容。您可以这样做:
boolean leftClicked = e.getButton() == MouseEvent.BUTTON1;
boolean rightClicked = e.getButton() == MouseEvent.BUTTON2;
boolean leftClicked = e.getButton() == MouseEvent.BUTTON1;
boolean rightClicked = e.getButton() == MouseEvent.BUTTON2;