Javafx错误不工作按钮

时间:2017-09-16 08:38:32

标签: java javafx netbeans

我创建了一个小应用程序。在AlertBox中使用JavaFX Netbeans。我的问题是,在点击AlertBox按钮后显示OK后,我的AlertBox未关闭。

代码如下。

newfxmain,java页面代码如下

public class NewFXMain extends Application {
    Stage window;
    Button b1;
    @Override
    public void start(Stage MainEvent)  {
       window=MainEvent;
       window.setTitle("hello");
       b1=new Button("click");
       b1.setOnAction(e -> AlertBox.display("hello", "welcome"));

       StackPane stk= new StackPane();
       stk.getChildren().add(b1);
       Scene sc= new Scene(stk,300,300);
       window.setScene(sc);
       window.show();                      
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

AlertBox的等级

public class AlertBox {
     public static void display(String title, String message) {
         Stage window= new Stage();
         window.initModality(Modality.APPLICATION_MODAL);
         window.setTitle(title);
         window.setMaxWidth(250);
         Label label1=new Label("hello");
         Button b1= new Button("close");
         b1.setOnAction(e -> window.close());
         VBox vb=new VBox(20);
         vb.getChildren().addAll(label1,b1);
         vb.setAlignment(Pos.CENTER);
         Scene sc= new Scene(vb);

         window.showAndWait();
    }
}

1 个答案:

答案 0 :(得分:0)

  1. 将vb添加到场景中。
  2. 使用show而不是showAndWait
  3. 这很好。

    public class NewFXMain extends Application {
            Stage window;
            Button b1;
            @Override
            public void start(Stage MainEvent)  {
               window=MainEvent;
               window.setTitle("hello");
               b1=new Button("click");
               b1.setOnAction(e -> AlertBox.display("hello", "welcome"));
    
               StackPane stk= new StackPane();
               stk.getChildren().add(b1);
               Scene sc= new Scene(stk,300,300);
               window.setScene(sc);
               window.show();                      
            }
    
            /**
             * @param args the command line arguments
             */
            public static void main(String[] args) {
                launch(args);
            }
           static  public class AlertBox {
             public  static void display(String title, String message) {
                 Stage window= new Stage();
                 window.initModality(Modality.APPLICATION_MODAL);
                 window.setTitle(title);
                 window.setMaxWidth(250);
                 Label label1=new Label("hello");
                 Button b1= new Button("close");
                 b1.setOnAction(e -> window.close());
                 VBox vb=new VBox();
                 vb.getChildren().addAll(label1,b1);
                 vb.setAlignment(Pos.CENTER);
    
                 Scene sc= new Scene(vb,300,300);
    
                 window.setScene(sc);
    
                 window.show();
            }
        }
        }