【发布时间】:2019-03-01 23:19:53
【问题描述】:
大家好,我对 GUI 完全陌生……到目前为止,我所有的程序都是基于文本的。我刚刚开始使用 JAVAFX。
我想重新创建一个电话键盘。它应该有 4 行,每行 3 个按钮,每个按钮都应该有其相关的数字及其字母。它确实如此。但是按钮的大小不一样。
我的意思是……因为我使用的是 tilePane,所以按钮占用相同数量的像素(或者至少我猜是这样的),但是按钮的实际可见大小是不同的,因为每个按钮只占用显示其内容所需的大小。我将按钮存储在一个数组中。有没有办法让它们都和最大的一样大?
public class PhoneKeyboard extends Application
{
Button buttons [];
@Override
public void start(Stage stage) throws Exception
{
// Create the set of necessary buttons.
buttons = new Button[12];
fillButtons(buttons);
//Create the grid for the buttons.
TilePane pane = new TilePane();
pane.setPadding(new Insets(10,10,10,10));
pane.setPrefColumns(3);
pane.setMaxWidth(Region.USE_PREF_SIZE);// Use the pref size as max size to make sure there will be the expected size
pane.setVgap(5.0);// to set ome spacing between each tile of the pane.
pane.setHgap(5.0);
//Put the buttons on the pane (layout);
pane.getChildren().addAll(buttons);
// JavaFX must have a Scene (window content) inside a Stage (window)
Scene scene = new Scene(new StackPane(pane), 300,400);// In order to use the pfer size of the pane, the tile Pane doesn"t have to be the root in the scene. Therefore we created a scene with a stack pane containing our tile pane as the root.
stage.setTitle("Keyboard");
stage.setScene(scene);
// Show the Stage (window)
stage.show();
}
【问题讨论】:
-
如果您提供的代码包含
fillButtons方法,我们将更容易重现您的问题。