JavaFX自定义对话框集节点布局
我们创建了一个没有FXML文件的自定义对话框。我们正在使用JavaFX8 对话框按预期加载和运行,但我们无法移动按钮和文本字段来增强样式JavaFX自定义对话框集节点布局,javafx,dialog,Javafx,Dialog,我们创建了一个没有FXML文件的自定义对话框。我们正在使用JavaFX8 对话框按预期加载和运行,但我们无法移动按钮和文本字段来增强样式 我们尝试使用tf.setLayoutY(50)这没有效果 我们使用了这个tf.setPrompText(“这个有效吗?”)并且它有效 我们宁愿不使用css来完成这个样式 我们将考虑FXML文件,如果我们可以保存两个事件处理程序,这些数据处理程序会迫使数据输入到TrimeField. 所以问题是:如何设置这个自定义对话框的样式 代码很混乱,因为它包含了我们尝试过
tf.setLayoutY(50)
这没有效果tf.setPrompText(“这个有效吗?”)
并且它有效public void CustomDialog() {
Dialog dialog = new Dialog<>();
dialog.setResizable(false);
final Window window = dialog.getDialogPane().getScene().getWindow();
stage = (Stage) window;
stage.setMinHeight(600);
stage.setMinWidth(400);
TextField tf = new TextField();
tf.setLayoutX(10);
tf.setLayoutY(50);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
dialog.getDialogPane().getChildren().add(tf);
dialog.getDialogPane().setContent(tf);
// Create an event filter that consumes the action if the text is empty
EventHandler<ActionEvent> filter = event -> {
if (tf.getText().isEmpty()) {
event.consume();
}
};
// lookup the buttons
ButtonBase okButton = (Button) dialog.getDialogPane().lookupButton(ButtonType.OK);
Button cancelButton = (Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
// add the event-filter
okButton.addEventFilter(ActionEvent.ACTION, filter);
cancelButton.addEventFilter(ActionEvent.ACTION, filter);
stage.setOnCloseRequest(event -> {
if (tf.getText().isEmpty()) {
event.consume();
}
}
//Scene scene = new Scene(root);
//dialogStage.setScene(scene);
dialog.initModality(Modality.APPLICATION_MODAL);
//dialogStage.setAlwaysOnTop(true);
//dialogStage.setResizable(false);
tf.setPromptText("This Works ?");
tf.requestFocus();// This does not work
dialog.showAndWait();
}
public void CustomDialog(){
Dialog=新建Dialog();
对话框。可设置大小(false);
最终窗口=dialog.getDialogPane().getScene().getWindow();
阶段=(阶段)窗口;
舞台设置高度(600);
舞台设置最小宽度(400);
TextField tf=新的TextField();
tf.setLayoutX(10);
tf.setLayoutY(50);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK,ButtonType.CANCEL);
dialog.getDialogPane().getChildren().add(tf);
dialog.getDialogPane().setContent(tf);
//如果文本为空,则创建使用该操作的事件筛选器
EventHandler筛选器=事件->{
if(tf.getText().isEmpty()){
event.consume();
}
};
//查找按钮
ButtonBase okButton=(按钮)dialog.getDialogPane().lookupButton(ButtonType.OK);
Button cancelButton=(Button)dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
//添加事件过滤器
okButton.addEventFilter(ActionEvent.ACTION,filter);
cancelButton.addEventFilter(ActionEvent.ACTION,filter);
stage.setOnCloseRequest(事件->{
if(tf.getText().isEmpty()){
event.consume();
}
}
//场景=新场景(根);
//dialogStage.setScene(场景);
dialog.initmodel(model.APPLICATION\u model);
//dialogStage.setAlwaysOnTop(真);
//dialogStage.SetResizeable(假);
tf.setpromptext(“这行吗?”);
tf.requestFocus();//这不起作用
dialog.showAndWait();
}
Grendel我们增强了您的答案,因此任何前来查看您在问题中发布的代码的人都会理解您所说的混乱您发布的答案非常老套,但可能比构建FXML文件要省力
此外,知道一些老派的把戏也不错
public void NewDialog(){
Label lblAmt = new Label("Enter Amount");
Button btnOK = new Button("OK");
TextField txtAmt = new TextField();
AnchorPane secondaryLayout = new AnchorPane();
secondaryLayout.setStyle("-fx-border-color:red;-fx-border-width:10px; -fx-background-color: lightblue;");
secondaryLayout.getChildren().addAll(lblAmt,btnOK,txtAmt);
lblAmt.setLayoutX(30);
lblAmt.setLayoutY(30);
txtAmt.setLayoutX(164);
txtAmt.setLayoutY(25);
txtAmt.setMaxWidth(116);
btnOK.setLayoutX(190);
btnOK.setLayoutY(100);
btnOK.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
lblAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
txtAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
Scene secondScene = new Scene(secondaryLayout, 300, 180);
EventHandler<ActionEvent> filter = event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
};
// New window (Stage)
Stage newWindow = new Stage();
newWindow.initStyle(StageStyle.UNDECORATED);
//newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.setResizable(false);
newWindow.setTitle("Second Stage");
newWindow.setScene(secondScene);
btnOK.addEventHandler(ActionEvent.ACTION,filter);
btnOK.setOnAction(evt -> {
String str = txtAmt.getText();
System.out.println("@@@@@@@@@@@@@@@@ str "+str);
if(txtAmt.getText().equals("")) {
evt.consume();
txtAmt.requestFocus();
}else{
newWindow.close();
}
});
newWindow.setOnCloseRequest(event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
});
txtAmt.requestFocus();
newWindow.showAndWait();
}
public void NewDialog(){
标签lblAmt=新标签(“输入金额”);
按钮btnOK=新按钮(“确定”);
TextField txtmat=新建TextField();
锚烷二次布局=新锚烷();
secondaryLayout.setStyle(“-fx边框颜色:红色;-fx边框宽度:10px;-fx背景颜色:浅蓝色;”);
secondaryLayout.getChildren().addAll(lblAmt、btnOK、txtmt);
lblAmt.setLayoutX(30);
lblAmt.setLayoutY(30);
txtAmt.setLayoutX(164);
txtAmt.setLayoutY(25);
txtAmt.setMaxWidth(116);
btnOK.setLayoutX(190);
btnOK.setLayoutY(100);
btnOK.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
lblAmt.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
txtAmt.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
场景secondScene=新场景(secondaryLayout,300180);
EventHandler筛选器=事件->{
if(txtmat.getText().isEmpty()){
event.consume();
}
};
//新窗口(舞台)
Stage newWindow=newstage();
newWindow.initStyle(StageStyle.Undecoraded);
//newWindow.initmodal(MODAL.APPLICATION_MODAL);
newWindow.setResizeable(false);
newWindow.setTitle(“第二阶段”);
newWindow.setScene(第二场景);
btnOK.addEventHandler(ActionEvent.ACTION,filter);
btnOK.setOnAction(evt->{
字符串str=txtmat.getText();
System.out.println(“@@@@@@@@@@@@@@@@@@@@@@@@@@str”+str);
if(txtmat.getText().equals(“”){
evt.consume();
txtmat.requestFocus();
}否则{
newWindow.close();
}
});
newWindow.setOnCloseRequest(事件->{
if(txtmat.getText().isEmpty()){
event.consume();
}
});
txtmat.requestFocus();
newWindow.showAndWait();
}
Grendel我们增强了您的答案,因此任何前来查看您在问题中发布的代码的人都会理解您所说的混乱您发布的答案非常老套,但可能比构建FXML文件要省力
此外,知道一些老派的把戏也不错
public void NewDialog(){
Label lblAmt = new Label("Enter Amount");
Button btnOK = new Button("OK");
TextField txtAmt = new TextField();
AnchorPane secondaryLayout = new AnchorPane();
secondaryLayout.setStyle("-fx-border-color:red;-fx-border-width:10px; -fx-background-color: lightblue;");
secondaryLayout.getChildren().addAll(lblAmt,btnOK,txtAmt);
lblAmt.setLayoutX(30);
lblAmt.setLayoutY(30);
txtAmt.setLayoutX(164);
txtAmt.setLayoutY(25);
txtAmt.setMaxWidth(116);
btnOK.setLayoutX(190);
btnOK.setLayoutY(100);
btnOK.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
lblAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
txtAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
Scene secondScene = new Scene(secondaryLayout, 300, 180);
EventHandler<ActionEvent> filter = event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
};
// New window (Stage)
Stage newWindow = new Stage();
newWindow.initStyle(StageStyle.UNDECORATED);
//newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.setResizable(false);
newWindow.setTitle("Second Stage");
newWindow.setScene(secondScene);
btnOK.addEventHandler(ActionEvent.ACTION,filter);
btnOK.setOnAction(evt -> {
String str = txtAmt.getText();
System.out.println("@@@@@@@@@@@@@@@@ str "+str);
if(txtAmt.getText().equals("")) {
evt.consume();
txtAmt.requestFocus();
}else{
newWindow.close();
}
});
newWindow.setOnCloseRequest(event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
});
txtAmt.requestFocus();
newWindow.showAndWait();
}
public void NewDialog(){
标签lblAmt=新标签(“输入金额”);
按钮btnOK=新按钮(“确定”);
TextField txtmat=新建TextField();
锚烷二次布局=新锚烷();
secondaryLayout.setStyle(“-fx边框颜色:红色;-fx边框宽度:10px;-fx背景颜色:浅蓝色;”);
secondaryLayout.getChildren().addAll(lblAmt、btnOK、txtmt);
lblAmt.setLayoutX(30);
lblAmt.setLayoutY(30);
txtAmt.setLayoutX(164);
txtAmt.setLayoutY(25);
txtAmt.setMaxWidth(116);
btnOK.setLayoutX(190);
btnOK.setLayoutY(100);
btnOK.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
lblAmt.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
txtAmt.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
场景secondScene=新场景(secondaryLayout,300180);
EventHandler筛选器=事件->{
if(txtmat.getText().isEmpty()){
事件