首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JavaFX GridLayout对象隐藏节点

JavaFX GridLayout对象隐藏节点
EN

Stack Overflow用户
提问于 2018-10-13 02:48:45
回答 1查看 63关注 0票数 -2

怎样才能让我创建的VBox和Label对象出现在相同的窗口和我想要的位置?标签就是标题。VBox对象包含四个按钮。

我想让VBox按钮对象出现在所创建的窗口中的标题下面。

使用此代码,仅显示标签对象"title“。我也可以让VBox出现,但前提是它的列和行参数比Label对象低或者高度与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);
   }

}

以下是此代码显示的内容:

EN

回答 1

Stack Overflow用户

发布于 2018-10-13 03:01:42

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);
            }
        });

    }
}

我运行你的代码的输出。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52785460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档