简体   繁体   English

按钮之间有无法解释的空间

[英]There is an unexplained space between buttons

I've a problem with an exercise. 我做运动有问题。 There is an unexplained space between the first and the second buttons on the scene. 场景中的第一个按钮和第二个按钮之间有无法解释的空间。 I've change the location of the buttons but still the first one has a space from the second one. 我更改了按钮的位置,但第一个按钮与第二个按钮之间有一个空格。 What cause this and how can I make this space shorter? 是什么原因造成的?如何缩小此空间? https://imgur.com/hYolYE2 https://imgur.com/hYolYE2

    public void start(Stage myStage) {

        GridPane myPane = new GridPane();
        myPane.setPadding(new Insets(10, 10, 10, 10));
        myPane.setHgap(10);
        myPane.setVgap(10);

        Button btn1 = new Button("Computer Scinence");
        Button btn2 = new Button("Chemistry");
        Button btn3 = new Button("Arts");

        DrawText txt1 = new DrawText("Computer Scince","Times Roman",FontWeight.BOLD,FontPosture.ITALIC,20,Color.GREEN);
        DrawText txt2 = new DrawText("Chemistry","Times Roman",FontWeight.BOLD,FontPosture.ITALIC,20,Color.BLUE);
        DrawText txt3 = new DrawText("Arts","Times Roman",FontWeight.BOLD,FontPosture.ITALIC,20,Color.YELLOW);

        GridPane.setConstraints(btn1,0,1);
        GridPane.setConstraints(btn2,1,1);
        GridPane.setConstraints(btn3,2,1);
        GridPane.setConstraints(txt1,0,2);
        GridPane.setConstraints(txt2,0,3);
        GridPane.setConstraints(txt3,0,4);

        myPane.getChildren().addAll(btn1,btn2,btn3,txt1,txt2,txt3);

        Scene myScene = new Scene(myPane,350,190);        
        myStage.setScene(myScene);        
        myStage.setTitle("Exercise 3_3");        
        myStage.show();
    }

You can often debug GridPane layout issues by turning on the grid lines: 您通常可以通过打开网格线来调试GridPane布局问题:

myPane.setGridLinesVisible(true);

After recreating your application (you did not include your DrawText class, so I'm just styling Labels instead) and turning on the grid lines reveals the issue: 重新创建应用程序后(您没有包括DrawText类,因此我只是在对Labels进行样式设置),然后打开网格线就会发现问题所在:

屏幕截图1

You can see that the Computer Science Label at GridPane location 0, 1 has a greater width than the Computer Science Button . 您会看到GridPane位置0, 1Computer Science Label宽度大于Computer Science Button宽度。 This causes the GridPane to expand the width of the first column to accommodate the Label . 这将导致GridPane扩展第一列的宽度以容纳Label

You have a couple of options to resolve this, depending on how you want the layout to work. 您有两种选择来解决此问题,具体取决于您希望布局如何工作。

1) Set Column Span : 1)设置列跨度

If you want to keep the Computer Science Button the same size, but have the first column's width not expand to fit the Label , just have that Label span 2 columns: 如果要保持“ 计算机科学” Button的大小相同,但第一列的宽度不能扩展到适合Label ,则只需让Label跨2列即可:

GridPane.setColumnSpan(txt1, 2);

屏幕截图2

2) Increase Button Width : 2)增加按钮宽度

If you'd like the Computer Science Button to expand to fill the rest of the column, you can set its MaxWidth property to use the maximum value: 如果您希望“ 计算机科学” Button展开以填充该列的其余部分,则可以将其“ MaxWidth属性设置为使用最大值:

btn1.setMaxWidth(Double.MAX_VALUE);

屏幕截图3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM