簡體   English   中英

JavaFX錯誤不起作用按鈕

[英]Javafx error not working button

我已經創建了小型應用程序。 這是在Netbeans使用JavaFXAlertBox 我的問題是,在單擊“ OK按鈕后顯示AlertBox之后,我的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. 將vb添加到場景。
  2. 使用show代替showAndWait

這很好。

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();
        }
    }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM