我想在VBox上工作,我想用最多5個包含多達5個按鈕的HBox填充。 另外我需要這些按鈕可調整大小,但我無法完成。 已經試圖用靜態方法返回一個HBox中,從用按鈕填充整個場景JavaFX
primaryStage.getChildren.addAll(hBoxMethod());
幫助稱其將是非常讚賞:)
我想在VBox上工作,我想用最多5個包含多達5個按鈕的HBox填充。 另外我需要這些按鈕可調整大小,但我無法完成。 已經試圖用靜態方法返回一個HBox中,從用按鈕填充整個場景JavaFX
primaryStage.getChildren.addAll(hBoxMethod());
幫助稱其將是非常讚賞:)
不清楚自己盡力去做。但是如果你想用5x5自動調整大小的按鈕來填充你的屏幕(如標題所示):爲什麼不使用GridPane和AnchorPanes的組合來調整大小。
package sample;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
AnchorPane rootPane = new AnchorPane();
Scene scene = new Scene(rootPane, 400, 300);
GridPane grid = new GridPane();
MaximizeInAnchorPane(grid);
// make column and rows resize
for (int i=0;i <5;i++)
{
ColumnConstraints cConstrain = new ColumnConstraints();
cConstrain.setHgrow(Priority.SOMETIMES);
grid.getColumnConstraints().add(cConstrain);
RowConstraints rConstrain = new RowConstraints();
rConstrain.setVgrow(Priority.SOMETIMES);
grid.getRowConstraints().add(rConstrain);
}
for (int i=0;i <5;i++)
{
for (int j=0;j <5;j++)
{
// create button and put it in an AnchorPane, that will resize it
AnchorPane buttonPane = new AnchorPane();
Button button = new Button("Button");
MaximizeInAnchorPane(button);
buttonPane.getChildren().add(button);
grid.add(buttonPane,i,j);
}
}
rootPane.getChildren().add(grid);
primaryStage.setTitle("test");
primaryStage.setScene(scene);
primaryStage.show();
}
private static void MaximizeInAnchorPane(Node toMaximize)
{
AnchorPane.setTopAnchor(toMaximize, 0.0);
AnchorPane.setRightAnchor(toMaximize, 0.0);
AnchorPane.setLeftAnchor(toMaximize, 0.0);
AnchorPane.setBottomAnchor(toMaximize, 0.0);
}
public static void main(String[] args) {
launch(args);
}
}
如果您嘗試手動調整所有設置,則應該查看SplitPane。這一切都取決於你想如何調整大小。
對我來說,它總是可以幫助玩弄JavaFX Scene Builder JavaFX Scene Builder來弄清楚如何做我想要的佈局。
每個按鈕代表商店提供的位於數據庫中的產品,這些產品均歸類。 例如,銷售員選擇銷售蘇打水,那麼屏幕上將填充所有蘇打水,例如可樂,雪碧等。 問題是,並不總是有25種產品,但我將其設置爲在超過25的情況下是限制的,除非它們分類不好,否則不太可能。 謝謝你的回答,我會嘗試操作de-nested,以便只添加所需按鈕的數量。 –
請向我們展示您嘗試過什麼 – Mostafiz