更新gui而不是新场景

wooyq4lh  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(321)

我现在有

public static void main(String[] args) {
        launch(args);        
    }

    @Override
    public void start(Stage stage) throws Exception {
        String dd = "Hello";

        stage.setTitle("Greetings");  //creates title

        button_roll = new Button("Roll");

        StackPane layout1 = new StackPane();
        layout1.getChildren().add(button_roll);

        Scene scene1 = new Scene(layout1, 600, 600);
        stage.setScene(scene1);

        Label mylab = new Label();
        mylab.setText(dd);
        Scene scene2 = new Scene(mylab, 600, 600);

        button_roll.setOnAction(e -> stage.setScene(scene2));

        stage.show();

    }

我的代码当前将“hello”作为新场景显示到场景中。我想知道是否有办法只更新scene1来显示文本,而不是创建一个只包含文本的全新场景。
我想做的事有术语吗?如果有,是什么?任何帮助都太好了!

kfgdxczn

kfgdxczn1#

我不熟悉javafx,但也许您应该试试eventhandler/actionevent:

button_roll.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent event) {
        mylab.setText("Hello");
      }
    });
fcy6dtqo

fcy6dtqo2#

你的问题是你从不添加 LabelScene . 将根节点作为 StackPane 将堆叠 Label 以及 Button 在对方身上。你需要替换 StackPaneVBox , HBox ,或更合适的 Node . 代码中的注解。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class App extends Application
{
    @Override
    public void start(Stage stage) throws Exception
    {
        String helloString = "Hello";//String use in the lable once the button is pressed.
        Label lblHello = new Label();//Label that will show helloString once the button is pressed.

        Button btnRoll = new Button("Roll");//The button that will trigger the action to change the label from empty string to helloString.
        btnRoll.setOnAction((t) -> {//Set the button's action
            lblHello.setText(helloString);//set the label's text to helloString.
        });

        VBox vbLayoutRoot = new VBox(lblHello, btnRoll);//The root layout is a VBox. Add the label and the btn to the root layout.
        Scene scene = new Scene(vbLayoutRoot, 600, 600);//Add the root layout to the scene.
        stage.setScene(scene);//Set the scene.
        stage.setTitle("Greetings");  //creates title
        stage.show();//Show the stage.
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}
mnowg1ta

mnowg1ta3#

你应该喜欢this:-

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage stage) throws Exception {
    String dd = "Hello";
    stage.setTitle("Greetings");  //creates title

    Button button_roll = new Button("Roll");

    StackPane layout1 = new StackPane();
    layout1.getChildren().add(button_roll);

    Scene scene1 = new Scene(layout1, 600, 600);
    stage.setScene(scene1);

    Label mylab = new Label();
    mylab.setText(dd);

    button_roll.setOnAction(e -> {
        layout1.getChildren().clear();
        layout1.getChildren().add(mylab);
    });

    stage.show();

}

相关问题