我试图在JavaFX中构建一个计算器,并且VBox没有与GridPane中的其他节点对齐。
我使用GridPane作为我的根节点,并在每个单元格位置放置单独的按钮。
最右边的列是一个包含一列按钮的VBox。
问题是这个VBox与其他按钮相差几个像素 - 它不会对齐。这就是我看到的:screenshot
我已尝试设置VBox的对齐方式,但这没有任何区别。我错过了什么?
package calculator;
import java.util.ArrayList;
import java.util.List;
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 appWidth = 200 * 3;
private static int appHeight = 310 * 3;
private static int numRows = 7;
private static int numCols = 8;
List<Button> buttonList = new ArrayList();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Application.setUserAgentStylesheet(STYLESHEET_MODENA);
//Create 0-9 buttons
for (int i = 0; i < 10; i++) {
Button tempButton = new Button(Integer.toString(i));
tempButton.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
HBox.setHgrow(tempButton, Priority.ALWAYS);
buttonList.add(tempButton);
}
//Point button
Button point = new Button(".");
point.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
//Multiply and division buttons
Button multiply = new Button("X");
multiply.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
VBox.setVgrow(multiply, Priority.ALWAYS);
Button divide = new Button("/");
divide.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
VBox.setVgrow(divide, Priority.ALWAYS);
//Plus and minus buttons
Button plus = new Button("+");
plus.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
VBox.setVgrow(plus, Priority.ALWAYS);
Button minus = new Button("-");
minus.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
VBox.setVgrow(minus, Priority.ALWAYS);
//Equals button
Button equals = new Button("=");
equals.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
VBox.setVgrow(equals, Priority.ALWAYS);
//Clear and delete buttons
Button clear = new Button("AC");
clear.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
Button del = new Button("Del");
del.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
//Screen label
Label screen = new Label("0");
screen.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
screen.setAlignment(Pos.CENTER_RIGHT);
//Create GridPane
GridPane grid = new GridPane();
grid.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
//Create VBox to contain right column buttons
VBox rightCol = new VBox(divide, multiply, minus, plus, equals);
VBox.setVgrow(rightCol, Priority.ALWAYS);
rightCol.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
//Add nodes to grid pane
grid.add(buttonList.get(0), 0, 6, 4, 1);
grid.add(point, 4, 6, 2, 1);
//grid.add(equals, 6, 6, 2, 1);
grid.add(buttonList.get(1), 0, 5, 2, 1);
grid.add(buttonList.get(2), 2, 5, 2, 1);
grid.add(buttonList.get(3), 4, 5, 2, 1);
//grid.add(plus, 6, 5, 2, 1);
grid.add(buttonList.get(4), 0, 4, 2, 1);
grid.add(buttonList.get(5), 2, 4, 2, 1);
grid.add(buttonList.get(6), 4, 4, 2, 1);
//grid.add(minus, 6, 4, 2, 1);
grid.add(buttonList.get(7), 0, 3, 2, 1);
grid.add(buttonList.get(8), 2, 3, 2, 1);
grid.add(buttonList.get(9), 4, 3, 2, 1);
//grid.add(multiply, 6, 3, 2, 1);
grid.add(clear, 0, 2, 3, 1);
grid.add(del, 3, 2, 3, 1);
//grid.add(divide, 6, 2, 2, 1);
//Add the right column
grid.add(rightCol, 6, 2, 2, 5);
//Add the screen
grid.add(screen, 0, 0, 8, 2);
//Set row and column constraints
for (int rowIndex = 0; rowIndex < UserInterface.numRows; rowIndex++) {
RowConstraints rc = new RowConstraints();
rc.setVgrow(Priority.ALWAYS);
rc.setFillHeight(true);
rc.setPercentHeight(UserInterface.appHeight / UserInterface.numRows);
grid.getRowConstraints().add(rc);
}
for (int colIndex = 0; colIndex < UserInterface.numCols; colIndex++) {
ColumnConstraints cc = new ColumnConstraints();
cc.setHgrow(Priority.ALWAYS);
cc.setFillWidth(true);
cc.setPercentWidth(UserInterface.appWidth / UserInterface.numCols);
grid.getColumnConstraints().add(cc);
}
grid.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
// Create the scene and the stage
//grid.setGridLinesVisible(true);
Scene scene = new Scene(grid, appWidth, appHeight);
primaryStage.setScene(scene);
primaryStage.setTitle("Calculator");
primaryStage.setResizable(false);
primaryStage.show();
}
}