我有一个我无法解决的问题。我创建了JavaFX项目。我创建了主Java类,它将调用项目的所有组件:
public class SQLBrowser extends Application {
// Menu Utilization
CreatingMenus menu;
// Navigation Utilization
Navigation navigation;
// Set window size, color, window label
private final String windowLabel = "SQL Browser";
private final int xSize = 1200;
private final int ySize = 650;
private final Color backgroundColor = Color.WHITE;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
// Set Label
primaryStage.setTitle(windowLabel);
Group root = new Group();
Scene scene = new Scene(root, xSize, ySize, backgroundColor);
// Generate menu from sqlbrowser.menu.CreatingMenus;
menu = new CreatingMenus();
menu.initMenu(primaryStage, root, scene);
// Generate Data table navigation from sqlbrowser.navigation.Navigation;
navigation = new Navigation();
navigation.initNavigation(primaryStage, root, scene);
}
}
我在单独的Java类中创建了简单的菜单:
public class CreatingMenus {
// Generate Menu
public void initMenu(final Stage primaryStage, Group root, Scene scene) {
// Call Menu Actions from Java Method MenuActions
final MenuActions ma = new MenuActions();
MenuBar menuBar = new MenuBar();
// File menu - new, save, close, exit
Menu menu = new Menu("File");
menu.getItems().add(new MenuItem("New"));
menu.getItems().add(new MenuItem("Save"));
menu.getItems().add(new MenuItem("Close"));
menu.getItems().add(new SeparatorMenuItem());
MenuItem menuItem = new MenuItem("Exit");
// Exit from the application
menuItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
// This Java Method is called from the Java Class MenuActions
ma.programExit(); // Exit program
}
});
menu.getItems().add(menuItem);
menuBar.getMenus().add(menu);
// Options menu - Preferences
Menu options = new Menu("Options");
options.getItems().add(new MenuItem("Preferences"));
menuBar.getMenus().add(options);
// Help menu - About
Menu help = new Menu("Help");
MenuItem helpItem = new MenuItem("About");
// Exit from the application
helpItem.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
aboutDialog();
}
});
help.getItems().add(helpItem);
menuBar.getMenus().add(help);
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
root.getChildren().add(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
}
public void aboutDialog() {
final int xSize = 640;
final int ySize = 480;
final String logoImage = "/logo.png";
final Color backgroundColor = Color.WHITE;
final String text = "SQL Browser";
final String version = "Product Version: 1.0";
final String license = "License Information";
final Stage aboutDialog = new Stage();
aboutDialog.initModality(Modality.WINDOW_MODAL);
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 20, 20, 20));
// Logo
Image image = new Image(getClass().getResourceAsStream(logoImage));
ImageView logo = new ImageView(image);
grid.add(logo, 1, 0);
// Product name
Text productName = new Text(text);
productName.setFont(Font.font("Verdana", 12));
grid.add(productName, 0, 2);
// Product version
Text productVersion = new Text(version);
productVersion.setFont(Font.font("Verdana", 12));
grid.add(productVersion, 0, 3);
// Product License
Text productLicense = new Text(license);
productLicense.setFont(Font.font("Verdana", 12));
grid.add(productLicense, 0, 4);
// Close Button
Button closeButton = new Button("Close");
closeButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
// Close the dialog when "Close" button is pressed
aboutDialog.close();
}
});
grid.add(closeButton, 5, 18);
// Configure dialog size and background color
Scene aboutDialogScene = new Scene(grid, xSize, ySize, backgroundColor);
aboutDialog.setScene(aboutDialogScene);
aboutDialog.show();
}
}
然后我添加了将用于导航的手风琴组件:
public class Navigation {
private static final Image BLUE_FISH = new Image("http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Blue-Fish-icon.png");
private static final Image RED_FISH = new Image("http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Red-Fish-icon.png");
private static final Image YELLOW_FISH = new Image("http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Yellow-Fish-icon.png");
private static final Image GREEN_FISH = new Image("http://icons.iconarchive.com/icons/fasticon/fish-toys/128/Green-Fish-icon.png");
public void initNavigation(Stage primaryStage, Group root, Scene scene) {
VBox stackedTitledPanes = createStackedTitledPanes();
ScrollPane scroll = makeScrollable(stackedTitledPanes);
scroll.getStyleClass().add("stacked-titled-panes-scroll-pane");
scroll.setPrefSize(410, 480);
scroll.setLayoutX(0);
scroll.setLayoutY(20);
primaryStage.setTitle("Fishy, fishy");
scene = new Scene(scroll);
//scene.getStylesheets().add(getClass().getResource("fishy-fishy.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
private VBox createStackedTitledPanes() {
final VBox stackedTitledPanes = new VBox();
stackedTitledPanes.getChildren().setAll(
createTitledPane("One Fish", GREEN_FISH),
createTitledPane("Two Fish", YELLOW_FISH, GREEN_FISH),
createTitledPane("Red Fish", RED_FISH),
createTitledPane("Blue Fish", BLUE_FISH));
((TitledPane) stackedTitledPanes.getChildren().get(0)).setExpanded(true);
stackedTitledPanes.getStyleClass().add("stacked-titled-panes");
return stackedTitledPanes;
}
public TitledPane createTitledPane(String title, Image... images) {
FlowPane content = new FlowPane();
for (Image image : images) {
ImageView imageView = new ImageView(image);
content.getChildren().add(imageView);
FlowPane.setMargin(imageView, new Insets(10));
}
content.setAlignment(Pos.TOP_CENTER);
TitledPane pane = new TitledPane(title, content);
pane.getStyleClass().add("stacked-titled-pane");
pane.setExpanded(false);
return pane;
}
private ScrollPane makeScrollable(final VBox node) {
final ScrollPane scroll = new ScrollPane();
scroll.setContent(node);
scroll.viewportBoundsProperty().addListener(new ChangeListener<Bounds>() {
@Override
public void changed(ObservableValue<? extends Bounds> ov, Bounds oldBounds, Bounds bounds) {
node.setPrefWidth(bounds.getWidth());
}
});
return scroll;
}
}
但由于某些未知原因,我得到了这个结果:
由于某种原因,菜单未显示且窗口大小发生变化。你能告诉我如何解决这个问题吗?
答案 0 :(得分:3)
在您的示例中,您将覆盖场景!
primaryStage.setTitle("Fishy, fishy");
// this line are wrong!
scene = new Scene(scroll);
//scene.getStylesheets().add(getClass().getResource("fishy-fishy.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
您应该使用一个场景和一个主要布局。
答案 1 :(得分:1)
在你的initMethods中,不是一次又一次地将场景设置到舞台上,甚至创建一个新场景。我建议您添加到容器中:here it is a tutorial about layouts。
将主要课程改为:
VBox root = new VBox();
root.setAlignment(Pos.TOP_CENTER);
// Generate menu from sqlbrowser.menu.CreatingMenus;
menu = new CreatingMenus();
menu.initMenu(primaryStage, root);
// Generate Data table navigation from sqlbrowser.navigation.Navigation;
navigation = new Navigation();
navigation.initNavigation(primaryStage, root);
Scene scene = new Scene(root, xSize, ySize, backgroundColor);
primaryStage.setScene(scene);
primaryStage.show();
在你的initMethods中,删除场景,因为你不应该在里面使用它,并将你生成的节点添加到Container,在这个例子中是一个VBox。
public void initMenu(final Stage primaryStage, VBox root) {
....
root.getChildren().add(menuBar);
}
public void initNavigation(Stage primaryStage, VBox root) {
....
root.getChildren().add(scroll);
}
希望它有所帮助。