gpt4 book ai didi

java - 我如何在 JavaFX 中的所有场景中显示我的 MenuBar

转载 作者:行者123 更新时间:2023-12-05 03:22:27 25 4
gpt4 key购买 nike

我是 JavaFX 的新手

但是我写了这段代码,我不知道如何在我的所有场景中显示菜单栏。此外,我想在我的 HelloApplication 中使用布局创建/填充我的场景(但这是另一个问题)。

我有一个 Controller ,用于设置舞台并启动它。我的 MenuBar 在 MenuLeiste 类中,但我希望它也出现在我的 Credits 类中。对于缺少评论和此评论的布局,我感到非常抱歉。

    
public class HelloApplication extends Application {

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

@Override
public void start(Stage primaryStage) throws Exception {

//set window as primaryStage
Stage window = primaryStage;


//Layout of MenuLeiste is put in l1 and setted as scene1
MenuLeiste l1 = new MenuLeiste();
//menuscene gets its objects fromsceneViewMenu
Scene menuscene = new Scene(l1.sceneViewMenu());

window.setScene(menuscene);

window.setHeight(600);
window.setWidth(800);
window.setTitle("Game Title");
window.show();
}
}

我的 MenuLeiste 类

    import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class MenuLeiste {

public VBox sceneViewMenu() {

MenuBar menuBar = new MenuBar();
VBox menuBox = new VBox(menuBar);

Menu dataMenu = new Menu("Data");
MenuItem exitItem = new MenuItem("Exit");
exitItem.setOnAction(e -> System.exit(0));

Menu extrasMenu = new Menu("Extras");
MenuItem creditsItem = new MenuItem("Credits");
creditsItem.setOnAction(e -> {
Credits c = new Credits();
Scene scene3 = new Scene(c.sceneView3());
Stage window = (Stage) menuBox.getScene().getWindow();
window.setScene(scene3);
});


extrasMenu.getItems().addAll(creditsItem);
dataMenu.getItems().addAll( exitItem);


menuBar.getMenus().addAll(dataMenu,extrasMenu);

//the scenes layout is saved in layout1
VBox layout1 = new VBox(20);
layout1.getChildren().addAll(menuBox);

return layout1;
}
}

我的学分课

package view;

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 Credits {
public VBox sceneView3()
{

Label label = new Label("Thanks");
Button backButton = new Button("Back");

backButton.setOnAction(e -> {
MenuLeiste l1 = new MenuLeiste();
Scene menuscene = new Scene(l1.sceneViewMenu());
Stage window = (Stage) backButton.getScene().getWindow();
window.setScene(menuscene);
});

VBox layout1 = new VBox(20);
layout1.getChildren().addAll(label, backButton);

return layout1;
}
}

最佳答案

当您希望一个节点或一组节点存在于不同“场景”之间时,通常最好的解决方案是替换场景。相反,您需要修改当前场景中显示的节点。例如,在您的情况下,您可以将 BorderPane 作为场景的根,并将 MenuBar 设置为 top 节点。然后,当您想要更改“ View ”时,您可以替换 center 节点。

这是一个演示此概念的最小可运行示例。它利用“回调”来修改 BorderPane 的中心节点,同时始终使用相同的 MenuBar 实例。尽管请注意该示例仅包含 View 。如果你有一个支持模型(即数据、业务逻辑等),你会想要修改代码以便传递它。此外,如果您编写与此示例类似的应用程序,则让所有 View 在实际应用程序中实现一个公共(public)接口(interface)可能是明智的。

我没有菜单栏的单独“ View 类”,但这并不意味着您不能拥有一个(这样甚至可能更好)。

主.java:

import java.util.function.Consumer;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) {
var root = new BorderPane();
root.setTop(createMenuBar(root::setCenter));
root.setCenter(new TitleView().getNode());

primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.setTitle("Demo");
primaryStage.show();
}

private MenuBar createMenuBar(Consumer<Node> onUpdateView) {
var exitItem = new MenuItem("Exit");
exitItem.setOnAction(e -> Platform.exit());

var creditsItem = new MenuItem("Credits");
creditsItem.setOnAction(e -> {
e.consume();

var view = new CreditsView();
view.setOnGoBack(() -> onUpdateView.accept(new TitleView().getNode()));
onUpdateView.accept(view.getNode());
});

return new MenuBar(
new Menu("File", null, exitItem),
new Menu("Extras", null, creditsItem)
);
}
}

标题 View .java:

import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;

public class TitleView {

private Node node;

public Node getNode() {
if (node == null) {
node = new StackPane(new Label("Welcome!"));
}
return node;
}
}

CreditsView.java:

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;

public class CreditsView {

private Runnable onGoBack;
private Node node;

public Node getNode() {
if (node == null) {
var stack = new StackPane();
stack.setPadding(new Insets(10));

var label = new Label("Credits View");
stack.getChildren().add(label);

var goBackBtn = new Button("Go back");
goBackBtn.setOnAction(e -> {
e.consume();
if (onGoBack != null) {
onGoBack.run();
}
});
stack.getChildren().add(goBackBtn);
StackPane.setAlignment(goBackBtn, Pos.TOP_LEFT);

node = stack;
}
return node;
}

public void setOnGoBack(Runnable action) {
onGoBack = action;
}
}

关于java - 我如何在 JavaFX 中的所有场景中显示我的 MenuBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72722769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com