【问题标题】:JavaFX GridLayout Object Hides NodesJavaFX GridLayout 对象隐藏节点
【发布时间】:2019-03-18 00:50:54
【问题描述】:

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

我想让 VBox Button 对象显示在创建的窗口中的标题下方。

使用此代码,仅显示 Label 对象“title”。我也可以让 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);
   }

}

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

【问题讨论】:

  • 我没有看到你的问题或者我不明白你的问题。
  • @Sedrick 我的问题是我希望 VBox 对象内的按钮出现在标签标题下方,但我似乎无法让两者都出现在正确的位置。我希望标签出现在菜单项上方,而 java 给我带来了问题。根据我在 GridPane 中设置它们的位置,有时一个或另一个不显示,或者它们位于错误的位置。
  • 尝试最大化窗口。
  • 高 col/row 值看起来很奇怪 - 为什么不将它们彼此“靠近”呢?不明白它应该是什么样子......另外,您的代码还有其他问题(不要扩展 Stage 等,并且 stage = new Stage 是无操作的)所以可能是您的期望不符合提供的布局。
  • @kleopatra 你的意思是“高”,如行/列的数量,还是它在窗口中的位置?这些按钮彼此相当接近。我希望 VBox 对象出现。 VBox 对象中有 Button 对象。这些按钮应该出现在窗口上。这就是它应该的样子。我将编辑我的问题以明确说明。

标签: java eclipse user-interface javafx graphics


【解决方案1】:
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);
            }
        });

    }
}

我运行你的代码的输出。

【讨论】:

  • 我没有得到相同的结果。你确定你没有改变任何东西?我已经发布了我得到的图片。
  • 我更新了我的代码并尝试最大化窗口。窗口最大化,但没有任何改变。
猜你喜欢
  • 2014-05-01
  • 2012-12-30
  • 1970-01-01
  • 2014-07-08
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 2022-07-27
相关资源
最近更新 更多