如何使创建的VBox和Label对象出现在同一窗口中以及所需的位置? 标签是标题。 VBox对象包含四个按钮。
我想使VBox Button对象出现在所创建窗口的标题下方。
使用此代码,仅显示标签对象“标题”。我还可以使VBox出现,但前提是它的列和行参数低于或等于Label对象的高度。
在我的MainClass类中调用该类以显示主菜单。
package application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class MainMenu extends Stage{
private Button[] buttArr = {new Button("New Game"),
new Button("Save Game"),
new Button("Load Game"),
new Button("Exit Game")};
private Label title = new Label("Bandit King");
private GridPane grid = new GridPane();
private VBox menuItems = new VBox();
MainMenu() {
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(0, 10, 0, 10));
menuItems.getChildren().addAll(buttArr);
menuItems.setAlignment(Pos.CENTER_LEFT);
menuItems.setSpacing(30);
grid.add(title, 9, 7);
grid.add(menuItems, 5, 25);
this.setScene(new Scene(grid, 1600, 900));
this.setTitle("Bandit King");
this.show();
// set title font
Font.loadFont(getClass().getResourceAsStream("OLDSH.TFF"), 1);
title.setFont(new Font("OldStyle 1 HPLHS", 100));
title.getStyleClass().add("title");
// set New Game Button action
buttArr[0].setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
new Hideout();
}
});
// set Save Game Button action
buttArr[1].setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
System.out.println("WIP- save game");
}
});
// set Load Game Button action
buttArr[2].setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
System.out.println("WIP- load game");
}
});
// set Exit Game Button action
buttArr[3].setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
Platform.exit();
System.exit(0);
}
});
}
}
这是我的MainClass类:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
public class MainClass extends Application {
@Override
public void start (Stage stage) {
MainMenu mainMenu = new MainMenu();
stage = mainMenu;
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
import javafx.application.Application;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication282 extends Application
{
@Override
public void start(Stage primaryStage)
{
MainMenu mainMenu = new MainMenu();
primaryStage = mainMenu;
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
我注释了this.show();
和new Hideout();
,因为您没有发布Hideout
类。
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class MainMenu extends Stage
{
private Button[] buttArr = {new Button("New Game"),
new Button("Save Game"),
new Button("Load Game"),
new Button("Exit Game")};
private Label title = new Label("Bandit King");
private GridPane grid = new GridPane();
private VBox menuItems = new VBox();
MainMenu()
{
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(0, 10, 0, 10));
menuItems.getChildren().addAll(buttArr);
menuItems.setAlignment(Pos.CENTER_LEFT);
menuItems.setSpacing(30);
grid.add(title, 9, 7);
grid.add(menuItems, 5, 25);
this.setScene(new Scene(grid, 1600, 900));
this.setTitle("Bandit King");
// this.show();
// set title font
Font.loadFont(getClass().getResourceAsStream("OLDSH.TFF"), 1);
title.setFont(new Font("OldStyle 1 HPLHS", 100));
title.getStyleClass().add("title");
// set New Game Button action
buttArr[0].setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent t)
{
// new Hideout();
}
});
// set Save Game Button action
buttArr[1].setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent t)
{
System.out.println("WIP- save game");
}
});
// set Load Game Button action
buttArr[2].setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent t)
{
System.out.println("WIP- load game");
}
});
// set Exit Game Button action
buttArr[3].setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent t)
{
Platform.exit();
System.exit(0);
}
});
}
}
我运行您的代码的输出。