我正在尝试将根菜单设置为顶部&然后在下面做一个分屏。
我在网上找到了一个网站“http://code.makery.ch/library/javafx-8-tutorial/part1/”。 它有我想要做的例子。 我想用真实的代码进行硬编码,而不是像他那样使用拖放的东西。
到目前为止,我能够编写程序&通过root& amp;显示菜单运行一切,但我无法弄清楚如何在它下面添加“分屏”而不会引起问题。
每当我尝试在root中添加分屏实现和菜单时,它都不起作用。
我可以在JFrame
中完成,但我想在JavaFX root中完成它。
我在网上找不到任何关于它的信息,严重的是被困了4个小时。
(项目名称为“WorkAir”) 这是我目前的代码:
package application;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.Separator;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class WorkAir extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
primaryStage.setTitle("WorkAir");
Scene scene = new Scene(root, 1000, 550, Color.WHITE);
MenuBar menuBar = new MenuBar();
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
root.setTop(menuBar);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
System.out.println("Platform & System Exit");
Platform.exit();
System.exit(0);
}
});
// File Menu
Menu fileMenu = new Menu("File");
MenuItem newMenuItem = new MenuItem("New");
MenuItem saveMenuItem = new MenuItem("Save");
MenuItem exitMenuItem = new MenuItem("Exit");
fileMenu.getItems().addAll(newMenuItem, saveMenuItem,
new SeparatorMenuItem(), exitMenuItem);
//File Menu Trees
exitMenuItem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
//File Menu Trees Exit
Label secondLabel = new Label("Are you sure you want to exit?");
Button buttonNo = new Button();
buttonNo.setStyle("-fx-font: 10 arial; -fx-base: #d3d3d3;");
buttonNo.setTranslateX(-55);
buttonNo.setTranslateY(30);
Button buttonYes = new Button();
buttonYes.setStyle("-fx-font: 10 arial; -fx-base: #d3d3d3;");
buttonYes.setTranslateX(+55);
buttonYes.setTranslateY(30);
buttonNo.setText("Exit, No");
buttonNo.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("No");
Stage secondStage = (Stage) buttonNo.getScene().getWindow();
secondStage.close();
}
});
buttonYes.setText("Exit, Yes");
buttonYes.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Yes");
Platform.exit();
}
});
StackPane secondaryLayout = new StackPane();
secondaryLayout.getChildren().add(secondLabel);
secondaryLayout.getChildren().add(buttonNo);
secondaryLayout.getChildren().add(buttonYes);
Scene secondScene = new Scene(secondaryLayout, 350, 150);
Stage secondStage = new Stage();
secondStage.setTitle("Exit");
secondStage.setScene(secondScene);
//Set position of second window, related to primary window.
secondStage.setX(primaryStage.getX() + 250);
secondStage.setY(primaryStage.getY() + 100);
secondStage.centerOnScreen();
secondStage.show();
}
});
//2nd Menu
Menu secondMenu = new Menu("2nd");
CheckMenuItem htmlMenuItem = new CheckMenuItem("HTML");
htmlMenuItem.setSelected(true);
secondMenu.getItems().add(htmlMenuItem);
CheckMenuItem cssMenuItem = new CheckMenuItem("CSS");
cssMenuItem.setSelected(true);
secondMenu.getItems().add(cssMenuItem);
//3rd Menu
Menu thirdMenu = new Menu("3rd");
ToggleGroup tGroup = new ToggleGroup();
RadioMenuItem mysqlItem = new RadioMenuItem("MySQL");
mysqlItem.setToggleGroup(tGroup);
RadioMenuItem oracleItem = new RadioMenuItem("Oracle");
oracleItem.setToggleGroup(tGroup);
oracleItem.setSelected(true);
thirdMenu.getItems().addAll(mysqlItem, oracleItem,
new SeparatorMenuItem());
Menu Menu = new Menu("Sub Menu");
Menu.getItems().addAll(
new CheckMenuItem("1"),
new CheckMenuItem("2"),
new CheckMenuItem("3"));
secondMenu.getItems().add(Menu);
//Menu Bar Get All
menuBar.getMenus().addAll(fileMenu, secondMenu, thirdMenu);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
为此目的使用SplitPane
。
这是一个简短的例子。当然,在实际代码中,您将保留对某些变量中需要访问的Node
的引用:
@Override
public void start(Stage primaryStage) {
SplitPane splitPane = new SplitPane(new TableView(),
new VBox(new Label("some other content")));
splitPane.setOrientation(Orientation.HORIZONTAL);
// place splitPane as center
BorderPane borderPane = new BorderPane(splitPane);
borderPane.setTop(new MenuBar(new Menu("File")));
Scene scene = new Scene(borderPane, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}