简体   繁体   English

JavaFX-按钮高度不会变小?

[英]JavaFX- Button Height Doesn't Get Smaller?

.button
{
    -fx-background-image: url("mapDefault.png");
    -fx-pref-width: 10px;
    -fx-pref-height: 10px;
}

My button looks like this, and theoretically, it should fit the image (10x10) exactly.我的按钮看起来像这样,理论上,它应该完全适合图像 (10x10)。 However然而

纽扣

It returns that the button is about 30px in height, despite that I changed it.它返回按钮的高度约为 30 像素,尽管我对其进行了更改。 The button is declared like Button beachButton = new Button();按钮声明为Button beachButton = new Button(); and does not have any text values in it.并且其中没有任何文本值。

Any possible causes to this increase/min-cap in height?高度增加/最小上限的任何可能原因?

Any changes in text need to be taken into account, especially custom font.需要考虑文本的任何更改,尤其是自定义字体。 To accommodate for any picture sizes smaller than the font of the item, set the font size equal to 0px as well.要适应任何小于项目字体的图片大小,请将字体大小设置为等于 0px。

The offset of the icon(or text) from the buttons border may be the problem.图标(或文本)与按钮边框的偏移可能是问题所在。 A Button with text in default font type & size cannot get smaller in height than 25px (using setPrefHeight() or setStyle(...)).默认字体类型和大小的文本按钮的高度不能小于 25 像素(使用 setPrefHeight() 或 setStyle(...))。 To make the height the same as other controls the padding could be reduced as in the following example:为了使高度与其他控件相同,可以如下例所示减少填充:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SmallerButtons  extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        Label  label1= new Label ("Label1");
        Label  label2= new Label ("Label2");
        
        CheckBox  checkBox1= new CheckBox ("CheckBox1");
        CheckBox  checkBox2= new CheckBox ("CheckBox2");
        
        Button  button1= new Button ("Button1");
        Button  button2= new Button ("Button2");
        Button  button3= new Button ("Button3");
        
        button3.setOnAction(a-> System.out.println(label1.getHeight() + " / " +checkBox1.getHeight() + " / " +button1.getHeight()));
        
        label1.setStyle("-fx-font-size:10"); 
        button1.setPadding(new Insets(0,3,0,3)); // <----- this one works. Standard value is 5.0.
        button1.setMaxWidth(200);
        button2.setPrefHeight(17); // no effect
        button3.setStyle("-fx-pref-height:35px"); // or also setPrefHeight(35)
        
        VBox vb1=new VBox(label1,label2);
        VBox vb2=new VBox(checkBox1,checkBox2);
        VBox vb3=new VBox(button1, button2, button3);
        vb3.setMaxWidth(80);
        
        vb1.setSpacing(1);
        vb2.setSpacing(1);
        vb3.setSpacing(1);
        HBox hb = new HBox(vb1,vb2,vb3);
        hb.setSpacing(15);
        Scene scene = new Scene(hb);
        stage.setOnCloseRequest((e)->System.exit(0));
        stage.setScene(scene);
        stage.show();
    }   
}

One catch: With one buttons padding adjusted all other buttons need the padding explicitly set, too.一个问题:调整一个按钮的内边距后,所有其他按钮也需要显式设置内边距。

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

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