我创建了一个小应用程序。在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();
}
}
答案 0 :(得分:0)
这很好。
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();
}
}
}