我希望通过向窗格中添加gridPanes来使用JavaFX来建模一个简单的微波。微波
上面是结果应该是什么样子的,但是我在正确布置网格窗格时遇到了麻烦。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Pane;
import javafx.scene.control.Label;
public class Microwave extends Application
{
@Override
public void start(Stage primaryStage)
{
Pane root = new Pane();
GridPane gPane1 = new GridPane();
GridPane gPane2 = new GridPane();
TextField time = new TextField("Time to be displayed here");
Label food = new Label("Place food here");
Button start_button = new Button("Start");
Button stop_button = new Button("Stop");
Button button_0 = new Button("0");
Button button_1 = new Button("1");
Button button_2 = new Button("2");
Button button_3 = new Button("3");
Button button_4 = new Button("4");
Button button_5 = new Button("5");
Button button_6 = new Button("6");
Button button_7 = new Button("7");
Button button_8 = new Button("8");
Button button_9 = new Button("9");
gPane1.add(start_button, 1, 3);
gPane1.add(stop_button, 2, 3);
gPane1.add(button_0, 0, 3);
gPane1.add(button_1, 0, 0);
gPane1.add(button_2, 1, 0);
gPane1.add(button_3, 2, 0);
gPane1.add(button_4, 0, 1);
gPane1.add(button_5, 1, 1);
gPane1.add(button_6, 2, 1);
gPane1.add(button_7, 0, 2);
gPane1.add(button_8, 1, 2);
gPane1.add(button_9, 2, 2);
gPane2.add(time,0,0);
root.getChildren().addAll(gPane1,gPane2,food);
Scene scene = new Scene(root,200,50);
primaryStage.setTitle("Microwave Oven");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
下面是在SceneBuilder中创建的一个示例。您可以使用fxml
来了解如何在代码中执行此操作。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<HBox alignment="CENTER" prefHeight="300.0" prefWidth="500.0" style="-fx-border-color: black; -fx-border-width: 3px;" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label alignment="CENTER" text="Place Food Here">
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</Label>
<VBox style="-fx-border-color: black; -fx-border-width: 3px;" HBox.hgrow="ALWAYS">
<children>
<TextField prefHeight="50.0" promptText="Time to be displayed here">
<VBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</VBox.margin>
</TextField>
<Pane prefHeight="10.0" style="-fx-background-color: black;" />
<GridPane VBox.vgrow="ALWAYS">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="1">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="2" GridPane.columnIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="3" GridPane.columnIndex="2">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="4" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="5" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="6" GridPane.columnIndex="2" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="7" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="8" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="9" GridPane.columnIndex="2" GridPane.rowIndex="2">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="0" GridPane.rowIndex="3">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Start" GridPane.columnIndex="1" GridPane.rowIndex="3">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
<Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Stop" GridPane.columnIndex="2" GridPane.rowIndex="3">
<GridPane.margin>
<Insets bottom="10.0" left="15.0" right="5.0" top="10.0" />
</GridPane.margin>
</Button>
</children>
</GridPane>
</children>
</VBox>
</children>
</HBox>
我是一个Java/JavaFX程序员新手,我正在开发一个简单的JavaFX建筑设计工具,在这个工具中,您可以画出墙壁、地板等,因此对象(主要是线、圆、多边形、矩形图像)是在屏幕上绘制和创建的,而不是在运行之前创建的。
我有一个小问题。我试图用JavaFX2.0创建一个好的GUI(可以调整大小),但我有一个小问题。我使用的是网格窗格,但当我在其中放置多个按钮时,当我将屏幕大小调整为较小尺寸时,按钮将以屏幕左侧的大小相互滑动。我可以为单元格或其他东西设置最小大小吗?我正在使用FXML进行设计。非常感谢你!
我正在使用BorderPane实现JavaFX中的布局。 假设BorderPane的maxPrefWidth设置为1000。左窗格中有一个按钮,右窗格中有一个按钮。在中心窗格中,还有一个大小未知的按钮。 如果:中间元素的宽度是500,那么左右节点的宽度应该是250。 如果:中间元素的宽度是600,那么左右节点的宽度应该是200。 有没有一种方法告诉左右窗格自动增长(水平)直到中心节点的被击中?
我有一个背景窗格,用于绘制与BorderPane、VBox和HBox一起使用时不显示的对象。此外,我的BorderPane没有正确定位布局(左、下、右)。我尝试使用多个不同的窗格并向其中添加VBox和HBox,但是我遇到了类似的错误,布局与预期不符,主绘图窗格(画布)消失了。 TilePane和BorderPane是我最近的实验。我使用查看是否可以显示画布,尽管我希望画布是整个背景,而不仅仅是右边
我无法在SceneBuilder(eclipse)中使用GridPane,当我尝试在项目中插入GridPane时,SceneBuilder会在没有提示任何消息的情况下退出。 这就是我在日志文件中找到的:
在构建场景中的所有必需节点之后,我们通常会按顺序排列它们。 容器内部件的这种布置称为容器的布局。 我们还可以说我们遵循布局,因为它包括将所有组件放置在容器内的特定位置。 JavaFX提供了几种预定义的布局,如HBox, VBox, Border Pane, Stack Pane, Text Flow, Anchor Pane, Title Pane, Grid Pane, Flow Panel等。