2017-10-21 290 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