I have a problem with my javafx application.
我的javafx应用程序有问题。
It works in just command line, but when making a javafx setup I get a problem. Every time I have a button that e.g. when clicked should hide another button, it won't update or refresh. I have been told that animationtimer should resolve my problem, but my question goes how to example refresh every time a button is clicked?
它只在命令行中工作,但在进行javafx设置时我遇到了问题。我每次都有一个按钮,例如点击时应该隐藏另一个按钮,它不会更新或刷新。我被告知动画制作者应该解决我的问题,但我的问题是如何在每次单击按钮时刷新示例?
1 个解决方案
#1
1
Please include a Minimal, Complete, and Verifiable example so that others get a chance to see the actual problems in your code. Below is an answer to your question 'how to update every time a button is clicked?'.
请包含一个Minimal,Complete和Verifiable示例,以便其他人有机会查看代码中的实际问题。以下是对“每次点击按钮时如何更新?”问题的回答。
You can use an AnimationTimer
to implement this behaviour, but the simplest solution is often to use a callback. A callback is a function that is called when the button is clicked, which you get to write. Thus you can do anything when the button is clicked.
您可以使用AnimationTimer来实现此行为,但最简单的解决方案通常是使用回调。回调是一个在单击按钮时调用的函数,您可以编写该函数。因此,您可以在单击按钮时执行任何操作。
You can set this callback by calling the Button.setOnAction()
function. You can pass e.g. a lambda function or an EventHandler
. Here is an example with two buttons which hide each other when clicked (as you mentioned):
您可以通过调用Button.setOnAction()函数来设置此回调。你可以通过,例如lambda函数或EventHandler。这是一个示例,其中有两个按钮在单击时相互隐藏(如您所述):
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.control.Button;
public class main extends Application{
@Override public void start(Stage primaryStage) {
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
button1.setOnAction(event -> {
// This code is executed when button1 is pressed
if(button2.isVisible())
button2.setVisible(false);
else button2.setVisible(true);
});
button2.setOnAction(event -> {
// This code is executed when button2 is pressed
if(button1.isVisible())
button1.setVisible(false);
else button1.setVisible(true);
});
button2.setLayoutX(75);
Group root = new Group(button1, button2);
Scene scene = new Scene(root, 200, 150);
primaryStage.setScene(scene);
primaryStage.setWidth(200);
primaryStage.setHeight(150);
primaryStage.show();
}
public static void main(String[] args){
launch();
}
}
I recommend having a look at the callback concept and first class functions. These are very useful concepts with diverse applications.
我建议看看回调概念和第一类函数。这些是具有不同应用的非常有用的概念。
#1
1
Please include a Minimal, Complete, and Verifiable example so that others get a chance to see the actual problems in your code. Below is an answer to your question 'how to update every time a button is clicked?'.
请包含一个Minimal,Complete和Verifiable示例,以便其他人有机会查看代码中的实际问题。以下是对“每次点击按钮时如何更新?”问题的回答。
You can use an AnimationTimer
to implement this behaviour, but the simplest solution is often to use a callback. A callback is a function that is called when the button is clicked, which you get to write. Thus you can do anything when the button is clicked.
您可以使用AnimationTimer来实现此行为,但最简单的解决方案通常是使用回调。回调是一个在单击按钮时调用的函数,您可以编写该函数。因此,您可以在单击按钮时执行任何操作。
You can set this callback by calling the Button.setOnAction()
function. You can pass e.g. a lambda function or an EventHandler
. Here is an example with two buttons which hide each other when clicked (as you mentioned):
您可以通过调用Button.setOnAction()函数来设置此回调。你可以通过,例如lambda函数或EventHandler。这是一个示例,其中有两个按钮在单击时相互隐藏(如您所述):
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.control.Button;
public class main extends Application{
@Override public void start(Stage primaryStage) {
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
button1.setOnAction(event -> {
// This code is executed when button1 is pressed
if(button2.isVisible())
button2.setVisible(false);
else button2.setVisible(true);
});
button2.setOnAction(event -> {
// This code is executed when button2 is pressed
if(button1.isVisible())
button1.setVisible(false);
else button1.setVisible(true);
});
button2.setLayoutX(75);
Group root = new Group(button1, button2);
Scene scene = new Scene(root, 200, 150);
primaryStage.setScene(scene);
primaryStage.setWidth(200);
primaryStage.setHeight(150);
primaryStage.show();
}
public static void main(String[] args){
launch();
}
}
I recommend having a look at the callback concept and first class functions. These are very useful concepts with diverse applications.
我建议看看回调概念和第一类函数。这些是具有不同应用的非常有用的概念。