2017-10-21 185 views
0

我试图创建一个取消按钮,它看起来是这样的:enter image description here按钮:最小尺寸

因为十字架我目前使用的字母“X”。

现在我试图减少按钮中的空白空间,但我找不到摆脱垂直空间的方法。

目前,我有这样的事情:

package test; 

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.Priority; 
import javafx.scene.layout.Region; 
import javafx.stage.Stage; 

public class CancelButtonTestApplication extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     GridPane gridPane = new GridPane(); 

     Label someLabel = new Label("Some Label:"); 
     Button cancelButton = new Button("x"); 
     cancelButton.setStyle("-fx-padding: 0.083333em 0.083333em 0.083333em 0.083333em;"); /* 1 1 1 1 */ 
     cancelButton.setMaxHeight(Region.USE_PREF_SIZE); 
     someLabel.setLabelFor(cancelButton); 
     gridPane.add(someLabel, 0, 0); 
     gridPane.add(cancelButton, 1, 0); 
     gridPane.setHgap(5.0d); 
     GridPane.setVgrow(cancelButton, Priority.NEVER); 
     GridPane.setHgrow(cancelButton, Priority.NEVER); 

     Scene scene = new Scene(gridPane, 200, 50); 

     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 

它看起来像这样:

enter image description here

回答

0

不知道你为什么不使用插图?

我做了cancelButton.setPadding(new Insets(-5,0,-2,0));,然后我能够删除顶部/底部空间(可用于Captial X)。对于大写字母X,我的意思是,如果您将使用X而不是x,则不需要任何特殊填充,但只需使用Insets.EMPTY即可。

或保持它的动态,你甚至可以尝试FontMetrics

FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(cancelButton.getFont()); 
cancelButton.setPadding(new Insets(-metrics.getDescent()*1.8, 0, -metrics.getDescent(), 0)); 

此代码应scene.show()后一定要得到正确的矩阵。 enter image description here

全码:

package test; 

import com.sun.javafx.tk.FontMetrics; 
import com.sun.javafx.tk.Toolkit; 
import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.Priority; 
import javafx.scene.layout.Region; 
import javafx.stage.Stage; 

public class CloseButtonTestApplication extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     GridPane gridPane = new GridPane(); 

     Label someLabel = new Label("Some Label:"); 


     Button cancelButton = new Button("x"); 

     //cancelButton.setStyle("-fx-padding: 0.083333em 0.083333em 0.083333em 0.083333em;"); /* 1 1 1 1 */ 
     cancelButton.setMaxHeight(Region.USE_PREF_SIZE); 
     //cancelButton.setPadding(new Insets(0,0,0,0)); 
     someLabel.setLabelFor(cancelButton); 

     Button cancelButton2 = new Button("X"); 

     //cancelButton.setStyle("-fx-padding: 0.083333em 0.083333em 0.083333em 0.083333em;"); /* 1 1 1 1 */ 
     cancelButton2.setMaxHeight(Region.USE_PREF_SIZE); 
     cancelButton2.setPadding(new Insets(0,0,0,0)); 
     someLabel.setLabelFor(cancelButton); 
     gridPane.add(someLabel, 0, 0); 
     gridPane.add(cancelButton, 1, 0); 
     gridPane.add(cancelButton2, 2, 0); 

     gridPane.setHgap(5.0d); 
     GridPane.setVgrow(cancelButton, Priority.NEVER); 
     GridPane.setHgrow(cancelButton, Priority.NEVER); 

     Scene scene = new Scene(gridPane, 200, 50); 

     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
     FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(cancelButton.getFont()); 
     cancelButton.setPadding(new Insets(-metrics.getDescent()*1.8, 0, -metrics.getDescent(), 0)); 



    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    } 

} 
+0

我使用套风格填充至1,1,1,1 AFAIK 你说的建议解决方案是使用负值进行填充?为什么这需要? – Puce

+0

将填充设置为空看起来不正确,既不包含小x也不包含大写X. – Puce

+0

我可以共享图像,这对我来说工作正常。我已经上传了包含我所做更改的图片。 – Optional