我正在使用字母' 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);
}
}
看起来像这样:
答案 0 :(得分:0)
不确定你为什么不使用Insets?
我做了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));
完整代码:
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);
}
}