[英]Can i make a method that takes arguments that it will execute
I will form my question new. 我将重新提出我的问题。
This is what i currently have(LWJGL not JButton): 这是我目前拥有的(LWJGL不是JButton):
public static boolean isButtonClicked(String buttonName){
Button b = getButton(buttonName);
float mouseY = essentials.Boot.HEIGHT - Mouse.getY() - 1;
if (Mouse.getX() > b.getX() && Mouse.getX() < b.getX() + b.getWidth() &&
mouseY > b.getY() && mouseY < b.getY() + b.getHeight()){
return true;
}
return false;
}
And thats in my main class 那就是我的主要课程
if(Ui.isButtonClicked("buttonName")/*Takes a name of a button as a argument*/){
System.out.println();
}
So i dont want there to be several if statements in my main. 所以我不想在我的主体中有几个if语句。 But its always diffrent what i want to do if the button is clicked.
但是,如果单击按钮,它总是与我要执行的操作有所不同。 So my question is if there is a way to do something like this.
所以我的问题是,是否有办法做这样的事情。 If you still dont know what i mean im just going to delete the post.
如果您仍然不知道我的意思,我就是要删除帖子。
You can use Interface/Lambda to pass a method to another method. 您可以使用Interface / Lambda将一个方法传递给另一个方法。 for example:
例如:
public interface Procedure {
void act();
}
public void executeCommand(Procedure proc){
proc.act();
}
//...
// pass a method using lambda expression:
executeCommand(()->{system.out.println("Hello world"});
//pass a method using traditional way (anonymous class)
executeCommand(new Procedure() {
public void act() {
system.out.println("Hello world");
}
});
Do you mean some thing like this: 你的意思是这样的吗:
public static void doSmth(String value) {
System.out.println(value);
}
//When you call it
doSmth("Hello world");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.