我正在尝试使用2列创建一个具有特定布局的窗口,但我无法按照我想要的方式工作。
首先,一些简化的示例代码:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Spinner;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class GridTest extends Application {
@Override
public void start(final Stage stage) throws Exception {
final GridPane grid = new GridPane();
grid.setHgap(5);
grid.setVgap(5);
grid.setPadding(new Insets(10, 10, 10, 10));
grid.add(new Label("Something:"), 0, 0);
final Spinner<?> s1 = new Spinner<>();
grid.add(s1, 1, 0);
grid.add(new Label("Another thing:"), 0, 1);
final Spinner<?> s2 = new Spinner<>();
grid.add(s2, 1, 1);
final Button b = new Button("A button");
grid.add(b, 0, 2, 2, 1);
stage.setScene(new Scene(grid, 400, 150));
stage.show();
}
public static void main(final String... args) {
launch(args);
}
}
这就是我想要的:
我尝试使用各种约束,但我无法使一切工作正常。我认为我发现的主要问题是,在让旋转器使用可用宽度后,他们在缩小窗口时拒绝收缩。
我在Linux中使用Oracle JDK 1.8.0_60。
答案 0 :(得分:1)
这似乎是Spinner
bug,如果我改用TextField
,我会得到更好的结果。
示例:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
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.Priority;
import javafx.stage.Stage;
public class GridTest extends Application {
@Override
public void start(final Stage stage) throws Exception {
final GridPane grid = new GridPane();
grid.setHgap(5);
grid.setVgap(5);
grid.setPadding(new Insets(10, 10, 10, 10));
ColumnConstraints cc = new ColumnConstraints();
cc.setMinWidth(GridPane.USE_PREF_SIZE);
grid.getColumnConstraints().add(cc);
cc = new ColumnConstraints();
cc.setMinWidth(GridPane.USE_PREF_SIZE);
cc.setHgrow(Priority.ALWAYS);
grid.getColumnConstraints().add(cc);
grid.add(new Label("Something:"), 0, 0);
final TextField t1 = new TextField();
grid.add(t1, 1, 0);
grid.add(new Label("Another thing:"), 0, 1);
final TextField t2 = new TextField();
grid.add(t2, 1, 1);
final Button b = new Button("A button");
b.setMaxWidth(Double.MAX_VALUE);
grid.add(b, 0, 2, 2, 1);
stage.setScene(new Scene(grid, 400, 150));
stage.show();
}
public static void main(final String... args) {
launch(args);
}
}
但是用TextField
替换Spinner
s并且所有地狱都松了......