JavaFX setPrefSize未更改vBox大小
我是JavaFX的初学者,我面临这个问题: 我想做如下布局: 这就是我所拥有的: 似乎我尝试了很多不同的布局,但我就是做不好。我最终得到了VBox's,这是我能得到的最好的结果。尽管我用“PrefSize”来描述“vBox2”大小是整个场景的一半,但它根本没有反应 这是我的代码:JavaFX setPrefSize未更改vBox大小,java,javafx,layout,netbeans,Java,Javafx,Layout,Netbeans,我是JavaFX的初学者,我面临这个问题: 我想做如下布局: 这就是我所拥有的: 似乎我尝试了很多不同的布局,但我就是做不好。我最终得到了VBox's,这是我能得到的最好的结果。尽管我用“PrefSize”来描述“vBox2”大小是整个场景的一半,但它根本没有反应 这是我的代码: @Override // Override the start method in the Application class public void start(Stage primaryStage)
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a border pane
BorderPane pane = new BorderPane();
// Place nodes in the pane
pane.setLeft(getVBox());
pane.setBottom(getVBox2());
pane.setRight(getVBox3());
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 1000,800);
primaryStage.setTitle("ShowHBoxVBox"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
private VBox getVBox() {
VBox vBox = new VBox(15);
vBox.setPadding(new Insets(15, 5, 5, 5));
vBox.getChildren().add(new Label("vbox"));
Label[] courses = {new Label("CSCI 1301"), new Label("CSCI 1302"),
new Label("CSCI 2410"), new Label("CSCI 3720")};
for (Label course : courses) {
VBox.setMargin(course, new Insets(0, 0, 0, 15));
vBox.getChildren().add(course);
}
vBox.setStyle("-fx-border-style: solid inside;");
vBox.setPrefSize(500, 400);
return vBox;
}
private VBox getVBox2() {
VBox vBox2 = new VBox(15);
vBox2.setPadding(new Insets(15, 5, 5, 5));
vBox2.getChildren().add(new Label("Vbox2"));
Label[] courses = {new Label("CSCI 1301"), new Label("CSCI 1302"),
new Label("CSCI 2410"), new Label("CSCI 3720")};
for (Label course : courses) {
VBox.setMargin(course, new Insets(5, 5, 5, 15));
vBox2.getChildren().add(course);
}
vBox2.setStyle("-fx-border-style: solid inside;");
vBox2.setPrefSize(500, 400);
return vBox2;
}
private VBox getVBox3() {
VBox vBox3 = new VBox(15);
vBox3.setPadding(new Insets(15, 5, 5, 5));
vBox3.getChildren().add(new Label("vbox3"));
Label[] courses = {new Label("CSCI 1301"), new Label("CSCI 1302"),
new Label("CSCI 2410"), new Label("CSCI 3720")};
for (Label course : courses) {
VBox.setMargin(course, new Insets(0, 0, 0, 15));
vBox3.getChildren().add(course);
}
vBox3.setStyle("-fx-border-style: solid inside;");
vBox3.setPrefSize(500, 800);
return vBox3;
}
}
我不能使用FXML或样式在CSS
谢谢你的建议 我能想到的最简单的解决方案是构造两个HBox并将VBox放在其中。 关于Sebastian Damm如何创建这个简单的示例:
导入javafx.application.application;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
/**
*
*创建日期:20.03.2012
*@作者塞巴斯蒂安·达姆
*/
公共类HBoxandVBoxExample扩展了应用程序
{
@凌驾
public void start(Stage primaryStage)引发异常
{
HBox HBox=新的HBox(50);
hbox.setAlignment(Pos.CENTER);//默认左上角
VBox vbox1=新的VBox();
vbox1.设置对齐(位置底部\中心);
vbox1.setStyle(“-fx边框样式:实心;”
+“-fx边框宽度:1;”
+“-fx边框颜色:黑色”);
VBox vbox2=新的VBox(10);
vbox2.设置对齐(位置中心);
vbox2.setStyle(“-fx边框样式:实心;”
+“-fx边框宽度:1;”
+“-fx边框颜色:黑色”);
VBox vbox3=新的VBox(20);
vbox3.设置对齐(位置顶部\中心);
vbox3.setStyle(“-fx边框样式:实心;”
+“-fx边框宽度:1;”
+“-fx边框颜色:黑色”);
对于(int i=0;i<5;i++)
{
按钮bt=新按钮(“按钮”+(i+1));
Button bt2=新建按钮(“按钮”+(i+1));//不幸的是,没有“克隆”或“复制”方法
按钮bt3=新按钮(“按钮”+(i+1));
vbox1.getChildren().add(bt);
vbox2.getChildren().add(bt2);
vbox3.getChildren().add(bt3);
}
hbox.getChildren().addAll(vbox1、vbox2、vbox3);
Scene Scene=新场景(hbox,350250);//hbox是根节点
setTitle(“HBox和VBox示例”);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args)
{
应用程序启动(args);
}
}
我能想到的解决问题的最简单方法是构造两个HBox并将VBox放在其中。
关于Sebastian Damm如何创建这个简单的示例:
导入javafx.application.application;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
/**
*
*创建日期:20.03.2012
*@作者塞巴斯蒂安·达姆
*/
公共类HBoxandVBoxExample扩展了应用程序
{
@凌驾
public void start(Stage primaryStage)引发异常
{
HBox HBox=新的HBox(50);
hbox.setAlignment(Pos.CENTER);//默认左上角
VBox vbox1=新的VBox();
vbox1.设置对齐(位置底部\中心);
vbox1.setStyle(“-fx边框样式:实心;”
+“-fx边框宽度:1;”
+“-fx边框颜色:黑色”);
VBox vbox2=新的VBox(10);
vbox2.设置对齐(位置中心);
vbox2.setStyle(“-fx边框样式:实心;”
+“-fx边框宽度:1;”
+“-fx边框颜色:黑色”);
VBox vbox3=新的VBox(20);
vbox3.设置对齐(位置顶部\中心);
vbox3.setStyle(“-fx边框样式:实心;”
+“-fx边框宽度:1;”
+“-fx边框颜色:黑色”);
对于(int i=0;i<5;i++)
{
按钮bt=新按钮(“按钮”+(i+1));
Button bt2=新建按钮(“按钮”+(i+1));//不幸的是,没有“克隆”或“复制”方法
按钮bt3=新按钮(“按钮”+(i+1));
vbox1.getChildren().add(bt);
vbox2.getChildren().add(bt2);
vbox3.getChildren().add(bt3);
}
hbox.getChildren().addAll(vbox1、vbox2、vbox3);
Scene Scene=新场景(hbox,350250);//hbox是根节点
setTitle(“HBox和VBox示例”);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args)
{
应用程序启动(args);
}
}
布局窗格(例如VBox
或BorderPane
)布局其子节点的一般过程如下所示:
VBox
的所有子项的最小高度之和大于VBox
本身的高度,则根本无法在布局中容纳所有子项。相反,如果VBox
的高度大于子项的所有首选高度之和,则将以某种方式分配额外的垂直空间。大多数布局窗格都有配置如何处理这些情况的设置:其中一些设置应用于整个布局窗格,其他设置是
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* Created on: 20.03.2012
* @author Sebastian Damm
*/
public class HBoxandVBoxExample extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
HBox hbox = new HBox(50);
hbox.setAlignment(Pos.CENTER); // default TOP_LEFT
VBox vbox1 = new VBox();
vbox1.setAlignment(Pos.BOTTOM_CENTER);
vbox1.setStyle("-fx-border-style: solid;"
+ "-fx-border-width: 1;"
+ "-fx-border-color: black");
VBox vbox2 = new VBox(10);
vbox2.setAlignment(Pos.CENTER);
vbox2.setStyle("-fx-border-style: solid;"
+ "-fx-border-width: 1;"
+ "-fx-border-color: black");
VBox vbox3 = new VBox(20);
vbox3.setAlignment(Pos.TOP_CENTER);
vbox3.setStyle("-fx-border-style: solid;"
+ "-fx-border-width: 1;"
+ "-fx-border-color: black");
for (int i = 0; i < 5; i++)
{
Button bt = new Button("Button " + (i+1));
Button bt2 = new Button("Button " + (i+1)); // unfortunately there´s no "clone" or "copy" method
Button bt3 = new Button("Button " + (i+1));
vbox1.getChildren().add(bt);
vbox2.getChildren().add(bt2);
vbox3.getChildren().add(bt3);
}
hbox.getChildren().addAll(vbox1, vbox2, vbox3);
Scene scene = new Scene(hbox, 350, 250); // the hbox is the root node
primaryStage.setTitle("HBox and VBox Example");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
Application.launch(args);
}
}
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Layout extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a border pane
BorderPane pane = new BorderPane();
// Place nodes in the pane
VBox left = new VBox(getVBox(), getVBox2());
pane.setLeft(left);
pane.setCenter(getVBox3());
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowHBoxVBox"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
private VBox getVBox() {
VBox vBox = new VBox(15);
vBox.setPadding(new Insets(15, 5, 5, 5));
vBox.getChildren().add(new Label("vbox"));
Label[] courses = {new Label("CSCI 1301"), new Label("CSCI 1302"),
new Label("CSCI 2410"), new Label("CSCI 3720")};
for (Label course : courses) {
VBox.setMargin(course, new Insets(0, 0, 0, 15));
vBox.getChildren().add(course);
}
vBox.setStyle("-fx-border-style: solid inside;");
vBox.setPrefSize(500, 400);
return vBox;
}
private VBox getVBox2() {
VBox vBox2 = new VBox(15);
vBox2.setPadding(new Insets(15, 5, 5, 5));
vBox2.getChildren().add(new Label("Vbox2"));
Label[] courses = {new Label("CSCI 1301"), new Label("CSCI 1302"),
new Label("CSCI 2410"), new Label("CSCI 3720")};
for (Label course : courses) {
VBox.setMargin(course, new Insets(5, 5, 5, 15));
vBox2.getChildren().add(course);
}
vBox2.setStyle("-fx-border-style: solid inside;");
vBox2.setPrefSize(500, 400);
return vBox2;
}
private VBox getVBox3() {
VBox vBox3 = new VBox(15);
vBox3.setPadding(new Insets(15, 5, 5, 5));
vBox3.getChildren().add(new Label("vbox3"));
Label[] courses = {new Label("CSCI 1301"), new Label("CSCI 1302"),
new Label("CSCI 2410"), new Label("CSCI 3720")};
for (Label course : courses) {
VBox.setMargin(course, new Insets(0, 0, 0, 15));
vBox3.getChildren().add(course);
}
vBox3.setStyle("-fx-border-style: solid inside;");
vBox3.setPrefSize(500, 800);
return vBox3;
}
public static void main(String[] args) { Application.launch(args); }
}