我正在尝试学习JavaFX,我正在构建一个计算器,我正在体验一些需要指导的按钮大小调整行为。
下面是全班学生。我所做的是为每行数字按钮和点按钮创建一个HBox。然后,这三行被放置在VBox中,而VBox又被放置在HBox中的equals按钮旁边。
我正在阅读JavaFX for Dummies,并且看了很多帖子等等,但是无法解决这个问题!
package calculator;
import javafx.application.*;
import static javafx.application.Application.launch;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
public class UserInterface extends Application {
static int buttonHeight = 30;
VBox pane;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
//4, 5, 6 numeric buttons
//Six button
Button b6 = new Button("6");
b6.setMaxWidth(Double.MAX_VALUE);
b6.setPrefHeight(b6.getPrefHeight() + buttonHeight);
HBox.setHgrow(b6, Priority.ALWAYS);
//Five button
Button b5 = new Button("5");
b5.setMaxWidth(Double.MAX_VALUE);
b5.setPrefHeight(b5.getPrefHeight() + buttonHeight);
HBox.setHgrow(b5, Priority.ALWAYS);
//Four button
Button b4 = new Button("4");
b4.setMaxWidth(Double.MAX_VALUE);
b4.setPrefHeight(b4.getPrefHeight() + buttonHeight);
HBox.setHgrow(b4, Priority.ALWAYS);
//Place the buttons in the 456 HBox
HBox row456 = new HBox();
row456.getChildren().addAll(b4, b5, b6);
//1, 2, 3 numeric buttons
//Button three
Button b3 = new Button("3");
b3.setMaxWidth(Double.MAX_VALUE);
b3.setPrefHeight(b3.getPrefHeight() + buttonHeight);
HBox.setHgrow(b3, Priority.ALWAYS);
//Button two
Button b2 = new Button("2");
b2.setMaxWidth(Double.MAX_VALUE);
b2.setPrefHeight(b2.getPrefHeight() + buttonHeight);
HBox.setHgrow(b2, Priority.ALWAYS);
//Button one
Button b1 = new Button("1");
b1.setMaxWidth(Double.MAX_VALUE);
b1.setPrefHeight(b1.getPrefHeight() + buttonHeight);
HBox.setHgrow(b1, Priority.ALWAYS);
//Place the buttons in the 123 HBox
HBox row123 = new HBox();
row123.getChildren().addAll(b1, b2, b3);
//Zero and point buttons
//zero button
Button zero = new Button("0");
zero.setMaxWidth(Double.MAX_VALUE);
zero.setPrefHeight(zero.getPrefHeight() + buttonHeight);
HBox.setHgrow(zero, Priority.ALWAYS);
//point button
Button point = new Button(".");
point.setMaxWidth(Double.MAX_VALUE);
point.setPrefHeight(point.getPrefHeight() + buttonHeight);
HBox.setHgrow(point, Priority.ALWAYS);
//Place the buttons in the zeroPoint HBox
HBox zeroPoint = new HBox();
zeroPoint.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(zeroPoint, Priority.ALWAYS);
zeroPoint.getChildren().addAll(zero, point);
//Numeric button rows in a VBox
VBox intLower = new VBox(row456, row123, zeroPoint);
HBox.setHgrow(intLower, Priority.ALWAYS);
//Equals button
Button equals = new Button("=");
HBox.setHgrow(equals, Priority.ALWAYS);
equals.setMaxWidth(Double.MAX_VALUE);
equals.setMaxHeight(Double.MAX_VALUE);
// Numeric button VBox and equals button inside HBox
HBox lower = new HBox(intLower, equals);
lower.setAlignment(Pos.BOTTOM_CENTER);
//Populate the root pane
pane = new VBox();
pane.getChildren().addAll(lower);
pane.setAlignment(Pos.BOTTOM_CENTER);
//Add the root pane to a scene
Scene scene = new Scene(pane, 210, 285);
// Finish and show the stage
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.setTitle("Calculator");
primaryStage.show();
}
}
编辑:我现在尝试使用GridPane来组装这个GUI,遇到了同样的问题:行对齐不正确,节点之间可见小空间--这就像你用最便宜的报价装修一个厨房,但后来发现没有一个橱柜是齐平关闭的:https://imgur.com/a/lxubhiv
package calculator;
import javafx.application.*;
import static javafx.application.Application.launch;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
public class UserInterface extends Application {
private static int butHeightModifier = 30;
private static int appWidth = 225 * 2;
private static int appHeight = 310 * 2;
private static int numRows = 7;
private static int numCols = 5;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
//7, 8, 9 numeric buttons
//Nine button
Button b9 = new Button("9");
b9.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(b9, Priority.ALWAYS);
VBox.setVgrow(b9, Priority.ALWAYS);
//Eight button
Button b8 = new Button("8");
b8.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(b8, Priority.ALWAYS);
VBox.setVgrow(b8, Priority.ALWAYS);
//Seven button
Button b7 = new Button("7");
b7.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(b7, Priority.ALWAYS);
VBox.setVgrow(b7, Priority.ALWAYS);
//Place the buttons in the 456 HBox
HBox row789 = new HBox();
row789.getChildren().addAll(b7, b8, b9);
//4, 5, 6 numeric buttons
//Six button
Button b6 = new Button("6");
b6.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(b6, Priority.ALWAYS);
VBox.setVgrow(b6, Priority.ALWAYS);
//Five button
Button b5 = new Button("5");
b5.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(b5, Priority.ALWAYS);
VBox.setVgrow(b5, Priority.ALWAYS);
//Four button
Button b4 = new Button("4");
b4.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(b4, Priority.ALWAYS);
VBox.setVgrow(b4, Priority.ALWAYS);
//Place the buttons in the 456 HBox
HBox row456 = new HBox();
row456.getChildren().addAll(b4, b5, b6);
//1, 2, 3 numeric buttons
//Button three
Button b3 = new Button("3");
b3.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(b3, Priority.ALWAYS);
VBox.setVgrow(b3, Priority.ALWAYS);
//Button two
Button b2 = new Button("2");
b2.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(b2, Priority.ALWAYS);
VBox.setVgrow(b2, Priority.ALWAYS);
//Button one
Button b1 = new Button("1");
b1.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(b1, Priority.ALWAYS);
VBox.setVgrow(b1, Priority.ALWAYS);
//Place the buttons in the 123 HBox
HBox row123 = new HBox();
row123.getChildren().addAll(b1, b2, b3);
//Zero and point buttons
//zero button
Button zero = new Button("0");
zero.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(zero, Priority.ALWAYS);
VBox.setVgrow(zero, Priority.ALWAYS);
//point button
Button point = new Button(".");
point.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(point, Priority.ALWAYS);
VBox.setVgrow(point, Priority.ALWAYS);
//Place the buttons in the zeroPoint HBox
HBox zeroPoint = new HBox();
zeroPoint.getChildren().addAll(zero, point);
//Multiply and division buttons
Button multiply = new Button("X");
multiply.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(multiply, Priority.ALWAYS);
VBox.setVgrow(multiply, Priority.ALWAYS);
Button division = new Button("/");
division.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(division, Priority.ALWAYS);
VBox.setVgrow(division, Priority.ALWAYS);
//Place the buttons in the zeroPoint HBox
HBox multiDiv = new HBox();
multiDiv.getChildren().addAll(multiply, division);
//Plus and minus buttons
Button plus = new Button("+");
plus.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(plus, Priority.ALWAYS);
VBox.setVgrow(plus, Priority.ALWAYS);
Button minus = new Button("-");
minus.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(minus, Priority.ALWAYS);
VBox.setVgrow(minus, Priority.ALWAYS);
//Place the buttons in the zeroPoint HBox
HBox plusMinus = new HBox();
plusMinus.getChildren().addAll(plus, minus);
//Equals button
Button equals = new Button("=");
equals.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
// Create GridPane
GridPane grid = new GridPane();
// Set row and column constraints
ColumnConstraints col = new ColumnConstraints();
col.setPercentWidth(20);
grid.getColumnConstraints().addAll(col, col, col, col, col);
RowConstraints row = new RowConstraints();
row.setPercentHeight(14.2857);
grid.getRowConstraints().addAll(row, row, row, row, row, row, row);
//Set filling constraint
for (int r = 0; r <= numRows; r++) {
RowConstraints rc = new RowConstraints();
rc.setFillHeight(true);
rc.setVgrow(Priority.ALWAYS);
grid.getRowConstraints().add(rc);
}
for (int c = 0; c <= numCols; c++) {
ColumnConstraints cc = new ColumnConstraints();
cc.setFillWidth(true);
cc.setHgrow(Priority.ALWAYS);
grid.getColumnConstraints().add(cc);
}
//Add nodes to grid pane
grid.add(zeroPoint, 0, 6, 3, 1);
grid.add(equals, 3, 5, 2, 2);
grid.add(row123, 0, 5, 3, 1);
grid.add(row456, 0, 4, 3, 1);
grid.add(row789, 0, 3, 3, 1);
grid.add(multiDiv, 3, 4, 2, 1);
grid.add(plusMinus, 3, 3, 2, 1);
// Create the scene and the stage
Scene scene = new Scene(grid);
primaryStage.setScene(scene);
primaryStage.setTitle("Calculator");
primaryStage.setMinWidth(appWidth);
primaryStage.setMinHeight(appHeight);
primaryStage.setResizable(false);
primaryStage.show();
}
}
将显示网格线
以便您可以看到按钮
的运行情况,如equal和decimal按钮。
Equal“按钮可扩展一个以上的行,”Decimal“按钮可扩展一个以上的列
buttonLayout.add(buttonList.get(11), 3, 0, 1, 4);//Equal Button
buttonLayout.add(buttonList.get(10), 1, 3, 2, 1);//Decimal Button
add(Node child,int columnIndex,int rowIndex,int colspan,int rowspan)在指定的列、行位置和跨度处向gridpane添加子级。
此代码允许按钮
填充单元格
。这里的代码。
for (int rowIndex = 0; rowIndex < 4; rowIndex++) {
RowConstraints rc = new RowConstraints();
rc.setVgrow(Priority.ALWAYS) ; // allow row to grow
rc.setFillHeight(true); // ask nodes to fill height for row
// other settings as needed...
buttonLayout.getRowConstraints().add(rc);
}
for (int colIndex = 0; colIndex < 4; colIndex++) {
ColumnConstraints cc = new ColumnConstraints();
cc.setHgrow(Priority.ALWAYS) ; // allow column to grow
cc.setFillWidth(true); // ask nodes to fill space for column
// other settings as needed...
buttonLayout.getColumnConstraints().add(cc);
}
完整代码:
import java.util.ArrayList;
import java.util.List;
import javafx.application.*;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
public class UserInterface extends Application {
VBox root = new VBox();
MenuBar menuBar = new MenuBar();
TextField display = new TextField("");
List<Button> buttonList = new ArrayList();
GridPane buttonLayout = new GridPane();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage){
for(int i = 0; i < 10; i++)
{
Button tempButton = new Button(Integer.toString(i));
buttonList.add(tempButton);
}
buttonList.add(new Button("."));
buttonList.add(new Button("="));
for(int i = 0; i < buttonList.size(); i++)
{
buttonList.get(i).setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}
buttonLayout.add(buttonList.get(7), 0, 0);
buttonLayout.add(buttonList.get(8), 1, 0);
buttonLayout.add(buttonList.get(9), 2, 0);
buttonLayout.add(buttonList.get(11), 3, 0, 1, 4);
buttonLayout.add(buttonList.get(4), 0, 1);
buttonLayout.add(buttonList.get(5), 1, 1);
buttonLayout.add(buttonList.get(6), 2, 1);
buttonLayout.add(buttonList.get(1), 0, 2);
buttonLayout.add(buttonList.get(2), 1, 2);
buttonLayout.add(buttonList.get(3), 2, 2);
buttonLayout.add(buttonList.get(0), 0, 3);
buttonLayout.add(buttonList.get(10), 1, 3, 2, 1);
buttonLayout.setGridLinesVisible(true);
buttonLayout.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
for (int rowIndex = 0; rowIndex < 4; rowIndex++) {
RowConstraints rc = new RowConstraints();
rc.setVgrow(Priority.ALWAYS) ; // allow row to grow
rc.setFillHeight(true); // ask nodes to fill height for row
// other settings as needed...
buttonLayout.getRowConstraints().add(rc);
}
for (int colIndex = 0; colIndex < 4; colIndex++) {
ColumnConstraints cc = new ColumnConstraints();
cc.setHgrow(Priority.ALWAYS) ; // allow column to grow
cc.setFillWidth(true); // ask nodes to fill space for column
// other settings as needed...
buttonLayout.getColumnConstraints().add(cc);
}
VBox.setVgrow(buttonLayout, Priority.ALWAYS);
root.getChildren().addAll(display, buttonLayout);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
我一直在使用JavaFX开发一个软件,遇到了一个愚蠢但令人担忧的问题。 在代码的某些部分中,我有一个,其中有三个项目:、和。
我目前正在克隆一个站点,在“输入电子邮件”输入中,文本不会对齐,并粘在输入的底部,我更希望它在中间的左边。我该怎么解决这个?感谢任何帮助<3 null null
我有一个关于JavaFX中的HBox的问题。如果我向HBox添加了一个新组件,它就会自动添加到最后一个组件中。有没有可能得到这样的东西:
最近,我想在容器中添加一个按钮,同时添加图标和文本。但按钮不能在中间对齐。我甚至不确定图标是否在中间对齐。我试着调整上边的%和左边的%但不动。 null null
在Java代码中,我无法在VBox中居中按钮!我使用场景生成器创建了一个SplitPane,左侧的AnchorPane有一个位于VBox中心的按钮。我想用Java代码在右AnchorPane而不是FXML中重新创建一个VBox中的按钮。但右侧按钮没有居中,尽管我使用
我有一个包含大量选项的JDialog,它工作得很好,但是我已经更改了它,默认情况下,除非用户单击Show Advanced按钮,否则某些选项是不可见的。 当他们这样做时,选项就会显示出来,但是因为对话框不够高,因为它的大小是基于那些选项被隐藏的,所以会添加一个垂直滚动条。 我希望对话框的大小足够大,当高级选项启用。我尝试创建显示高级选项的对话框,根据高级选项可见的情况调用pack()来适应 然后调