Bil*_*son 1 dialog javafx border
问题:对话框中有额外的边框,我想删除.
描述:我创建了一个带有显示对话框的按钮的简单应用程序.我设置了一个绿色背景的锚窗格作为对话框的内容.此锚点窗格包含另一个带有黑色背景的锚定窗格,并且它锚定到父窗口以填充整个空间.从理论上讲,我不应该看到任何绿色区域,只看到黑色区域.但它不是,就像绿色边界.
为什么?谢谢你的帮助!
package testdialog;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Dialog;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class TestDialog extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Show dialog");
btn.setOnAction((ActionEvent event) -> {
AnchorPane content = new AnchorPane();
content.setStyle("-fx-background-color: green; -fx-border-color: red;");
content.setPrefSize(100, 100);
AnchorPane innerAnchorPane = new AnchorPane();
innerAnchorPane.setStyle("-fx-background-color: black;");
AnchorPane.setTopAnchor(innerAnchorPane, 0d);
AnchorPane.setRightAnchor(innerAnchorPane, 0d);
AnchorPane.setBottomAnchor(innerAnchorPane, 0d);
AnchorPane.setLeftAnchor(innerAnchorPane, 0d);
content.getChildren().add(innerAnchorPane);
Dialog dlg = new Dialog();
dlg.getDialogPane().getStyleClass().add("customDialog");
dlg.initOwner(primaryStage);
dlg.getDialogPane().setContent(content);
dlg.initStyle(StageStyle.UNDECORATED);
dlg.initModality(Modality.NONE);
dlg.show();
});
StackPane root = new StackPane();
root.setStyle("-fx-background-color: yellow;");
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
css文件(style.css).customDialog类只是删除对话框的按钮栏.
.customDialog > *.button-bar > *.container {
-fx-pref-height: 0;
}
Run Code Online (Sandbox Code Playgroud)
默认样式表modena.css具有以下规则:
.dialog-pane > .content {
-fx-padding: 0.833em; /* 10 */
}
Run Code Online (Sandbox Code Playgroud)
这会在对话框窗格的内容中添加10个像素的填充.
您可以在对话框中覆盖它
.customDialog .content {
-fx-padding: 0 ;
}
Run Code Online (Sandbox Code Playgroud)
在你的style.css文件中.