如何在javafx中设置带有子窗口的菜单栏?
这里是代码部分,我添加了带有边框窗格的菜单栏,但它挂起了我的应用程序,因为我无法登录或做任何事情,我不得不添加子窗口,以供我附加图像时参考如何在javafx中设置带有子窗口的菜单栏?,java,javafx,menubar,childwindow,Java,Javafx,Menubar,Childwindow,这里是代码部分,我添加了带有边框窗格的菜单栏,但它挂起了我的应用程序,因为我无法登录或做任何事情,我不得不添加子窗口,以供我附加图像时参考 这是一个非常基本的示例,通过MenuItems在边框窗格的中心处理视图: import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.s
这是一个非常基本的示例,通过
MenuItem
s在边框窗格的中心处理视图:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
// create the menu bar with a menu and its items
MenuBar menuBar = new MenuBar();
Menu mainMenu = new Menu("Window");
MenuItem browserItem = new MenuItem("browser");
MenuItem imageItem = new MenuItem("image");
MenuItem closeItem = new MenuItem("exit");
// create some different contents for the center of the border pane
Label imagePlaceHolder = new Label("IMAGE TO BE SHOWN");
WebView browser = new WebView();
WebEngine browserEngine = browser.getEngine();
// set the actions for the different items
closeItem.setOnAction(action -> {
System.exit(0);
});
imageItem.setOnAction(action -> {
root.setCenter(imagePlaceHolder);
});
browserItem.setOnAction(action -> {
root.setCenter(browser);
browserEngine.load("http://www.google.com");
});
// add items to the menu, then the menu to the menu bar
mainMenu.getItems().addAll(closeItem, browserItem, imageItem);
menuBar.getMenus().add(mainMenu);
// set the scene
Scene scene = new Scene(root,400,400);
root.setTop(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
我希望它能帮助
编辑
我刚刚看到你的最新编辑…如果你真的需要不同的窗口(场景或舞台),方法会变得更复杂。读者必须获得有关不同窗口的更多信息(如如何创建窗口、如何处理窗口内容等)。这是一个非常基本的、最简单的处理边框窗格中央视图的示例,由菜单项
s提供:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
// create the menu bar with a menu and its items
MenuBar menuBar = new MenuBar();
Menu mainMenu = new Menu("Window");
MenuItem browserItem = new MenuItem("browser");
MenuItem imageItem = new MenuItem("image");
MenuItem closeItem = new MenuItem("exit");
// create some different contents for the center of the border pane
Label imagePlaceHolder = new Label("IMAGE TO BE SHOWN");
WebView browser = new WebView();
WebEngine browserEngine = browser.getEngine();
// set the actions for the different items
closeItem.setOnAction(action -> {
System.exit(0);
});
imageItem.setOnAction(action -> {
root.setCenter(imagePlaceHolder);
});
browserItem.setOnAction(action -> {
root.setCenter(browser);
browserEngine.load("http://www.google.com");
});
// add items to the menu, then the menu to the menu bar
mainMenu.getItems().addAll(closeItem, browserItem, imageItem);
menuBar.getMenus().add(mainMenu);
// set the scene
Scene scene = new Scene(root,400,400);
root.setTop(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
我希望它能帮助
编辑
我刚刚看到你的最新编辑…如果你真的需要不同的窗口(场景或舞台),方法会变得更复杂。读者必须获得有关不同窗口的更多信息(如如何创建窗口、如何处理窗口内容等)。图像链接无效!您是否尝试过使用fxml方法来设计您的gui?没有,我不知道fxml,我会先询问菜单栏,然后询问子窗口。您可以创建一个边框窗格
,并将菜单栏
设置为其顶部
,下面(作为中心
)您可以将始终位于菜单栏下方的视图切换到另一个视图…使用fxml进行布局非常有用,请尝试使用SceneBuilder()。没有fxml有什么方法吗?指向图像的链接不起作用!您是否尝试过使用fxml方法来设计您的gui?没有,我不知道fxml,我会先询问菜单栏,然后询问子窗口。您可以创建一个边框窗格
,并将菜单栏
设置为其顶部
,下面(作为中心
)您可以将始终位于菜单栏下方的视图切换到另一个视图…使用fxml布局非常有用,请尝试SceneBuilder()因此,没有fxml有什么办法吗?its是正确的,但很难与不同项目上的舞台上的完整动作集成,因为它的相互关系是正确的,但很难与不同项目上的舞台上的完整动作集成,因为它相互关联