问题:对话框中的额外边框,我想删除。
描述:我创建了一个简单的应用程序,其中包含一个显示对话框的按钮。我设置了一个绿色背景的锚窗格作为对话框的内容。此锚点窗格包含另一个带有黑色背景的锚定窗格,并且它锚定到父窗口以填充整个空间。从理论上讲,我不应该看到任何绿色区域,只有黑色区域。但它不是,就像绿色边界。
为什么呢?谢谢你的帮助!
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);
}
}
css文件(style.css)。 customDialog类只是删除对话框的按钮栏。
.customDialog > *.button-bar > *.container {
-fx-pref-height: 0;
}
答案 0 :(得分:5)
默认样式表modena.css具有以下规则:
.dialog-pane > .content {
-fx-padding: 0.833em; /* 10 */
}
将10像素填充添加到对话框窗格的内容中。
您可以使用
在对话框中覆盖此内容.customDialog .content {
-fx-padding: 0 ;
}
在你的style.css文件中。