有什么方法可以删除按钮未聚焦时出现在按钮下方的空白吗?
此按钮的外观已通过使用 JavaFX CSS 进行了更改,但是在自定义或未自定义时,未聚焦的按钮似乎默认具有此外观。有谁能够解决这个问题,因为它在不同颜色背景下会妨碍外观。
尝试使用 CSS 更改焦点外观,但当按钮失去焦点时就会出现问题。
产生此问题的最少代码:
package com.example.buttonexample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws IOException {
Button button1 = new Button("Login");
Button button2 = new Button("Sign Up");
HBox root = new HBox();
root.setStyle("-fx-background-color: orange");
button1.setPrefHeight(50);
button2.setPrefHeight(50);
root.getChildren().addAll(button1, button2);
Scene newScene = new Scene(root);
stage.setWidth(200);
stage.setHeight(200);
stage.setScene(newScene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
跑步:
找到解决方案:将其添加到 CSS -fx-background-insets: 0,1,1; 或者通过方法设置inserts。
答案:更改插图 -fx-background-insets: 0,1,1;