我在Javafx中创建了一个自定义对话框。我面临的问题是,每当我按任意键时,对话框的大小都会缩小。
自定义对话框的代码如下
package javafxcustomdialog;
import java.util.List;
import java.util.Optional;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class JavafxCustomDialog extends Application {
@Override
public void start(Stage primaryStage) {
Dialog dialog = new Dialog();
DialogPane dialogPane = dialog.getDialogPane();
dialog.setHeaderText(null);
dialog.setGraphic(null);
dialog.initStyle(StageStyle.UNDECORATED);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.getDialogPane().setStyle("-fx-background-color: #fff; -fx-border-color: #e7eaec #e7eaec #e7eaec #e7eaec; -fx-border-width: 6;");
// Set the button types.
ButtonType okButtonType = new ButtonType("Ok", ButtonBar.ButtonData.OK_DONE);
ButtonType cancelButtonType = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
dialog.getDialogPane().getButtonTypes().addAll(okButtonType, cancelButtonType);
// VBox
VBox alertVBox = new VBox();
VBox.setVgrow(alertVBox, Priority.ALWAYS);
alertVBox.setPrefWidth(400.0);
alertVBox.setSpacing(10);
Label label = new Label("Enter Some Text");
TextField textField = new TextField();
alertVBox.getChildren().addAll(label, textField);
// END VBox
dialog.getDialogPane().setContent(alertVBox);
// Request focus on the username field by default.
Platform.runLater(() -> textField.requestFocus());
// result
Optional<List<String>> result = dialog.showAndWait();
if (result.isPresent()) {
System.out.println("Good");
} else {
System.out.println("Something is wrong..!!");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
问题出在这一行
dialog.getDialogPane().setStyle("-fx-background-color: #fff; -fx-border-color: #e7eaec #e7eaec #e7eaec #e7eaec; -fx-border-width: 6;");
截图:
答案 0 :(得分:0)
对于遇到相同问题的人,解决方案只是将您的java SE从8u40更新到8u60或更高版本。 这是java SE中的一个错误 https://bugs.openjdk.java.net/browse/JDK-8095678