JavaFX-按钮高度不小于';你不能变小吗?

JavaFX-按钮高度不小于';你不能变小吗?,java,css,button,javafx,Java,Css,Button,Javafx,我的按钮看起来是这样的,理论上,它应该完全符合图像(10x10)。然而 它返回按钮的高度约为30px,尽管我对其进行了更改。按钮声明为 Button beachButton=新建按钮() 并且其中没有任何文本值 是否有可能导致高度增加/min上限的原因?需要考虑文本中的任何更改,尤其是自定义字体。要适应任何小于项目字体的图片大小,请将字体大小设置为0px。图标(或文本)与按钮边框的偏移可能是问题所在。文本为默认字体类型和大小的按钮的高度不能小于25px(使用setPrefHeight()或se

我的按钮看起来是这样的,理论上,它应该完全符合图像(10x10)。然而

它返回按钮的高度约为30px,尽管我对其进行了更改。按钮声明为
Button beachButton=新建按钮()
并且其中没有任何文本值


是否有可能导致高度增加/min上限的原因?

需要考虑文本中的任何更改,尤其是自定义字体。要适应任何小于项目字体的图片大小,请将字体大小设置为0px。

图标(或文本)与按钮边框的偏移可能是问题所在。文本为默认字体类型和大小的按钮的高度不能小于25px(使用setPrefHeight()或setStyle(…)。要使高度与其他控件相同,可以减少填充,如下例所示:

.button
{
    -fx-background-image: url("mapDefault.png");
    -fx-pref-width: 10px;
    -fx-pref-height: 10px;
}
导入javafx.application.application;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.CheckBox;
导入javafx.scene.control.Label;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类SmallerButtons扩展了应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
public void start(Stage)引发异常{
标签label1=新标签(“label1”);
标签label2=新标签(“label2”);
复选框checkBox1=新复选框(“复选框1”);
复选框checkBox2=新复选框(“复选框2”);
按钮1=新按钮(“按钮1”);
按钮2=新按钮(“按钮2”);
按钮按钮3=新按钮(“按钮3”);
按钮3.setOnAction(a->System.out.println(label1.getHeight()+“/”+复选框1.getHeight()+“/”+按钮1.getHeight());
标签1.setStyle(“-fx字体大小:10”);
button1.setPadding(新的插入(0,3,0,3));//System.exit(0));
舞台场景;
stage.show();
}   
}

一个缺点:调整一个按钮的填充时,所有其他按钮也需要明确设置填充。

您尝试过最大宽度和最大高度吗?按钮的父级是什么?您发布的图像比10x10大得多。那是按钮图像吗?
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();
    }   
}