[英]Javafx error not working button
我已經創建了小型應用程序。 這是在Netbeans
使用JavaFX
的AlertBox
。 我的問題是,在單擊“ 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();
}
}
這很好。
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.