为什么 JOptionPane.showMessageDialog 只在 main 工作?

why JOptionPane.showMessageDialog just working in main?

我使用的是最新版本的 NetBeans 和 macOS。我试试这是我的代码,JOptionPane.showMessageDialog 不工作。如果我将语法放在 main 中,它就可以工作。请告诉我为什么。我尝试 ,我遇到了同样的问题

import javax.swing.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class App extends Application {

    TextField txt;

    public void start(Stage primaryStage) {
        Label lb1 = new Label();
        TextField txt = new TextField("Type here");
        RadioButton rb1 = new RadioButton();
        RadioButton rb2 = new RadioButton();
        Button bt = new Button("click");
        Button bt1 = new Button("anas aljaghbeer");
        MyHandlerClass handler1 = new MyHandlerClass();

        bt.setOnAction(handler1);
        txt.setPrefSize(10, 10);
        lb1.setText("Enter here");
        txt.getText();

        VBox box = new VBox();
        Scene scene = new Scene(box, 1000, 1000);

        box.getChildren().addAll(lb1, txt, bt);
        primaryStage.setTitle("anas");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    class MyHandlerClass implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent e) {
            JOptionPane.showMessageDialog(null, " Hello");
        }
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

JOptionPane.showMessageDialog()…It's working if I put the syntax in the main. Please tell me why.

当您在 main() 中调用 JOptionPane.showMessageDialog() 时,它会在 initial thread 上执行。在 Swing 程序中,您可以像这样从 main() 调用它:

public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
        JOptionPane.showMessageDialog(null, "Click to continue…");
        …
    });
}

JavaFX 程序中,您 not to "mix Swing and JavaFX," unless you account for JavaFX-Swing Interoperability. Instead, evoke an Alert as shown here 及以下:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class App extends Application {

    TextField txt;

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label();
        TextField text = new TextField("Type here");
        Button button = new Button("Click");
        MyHandlerClass handler = new MyHandlerClass();

        button.setOnAction(handler);
        text.setPrefSize(10, 10);
        label.setText("Enter here");
        text.getText();

        VBox box = new VBox();
        box.getChildren().addAll(label, text, button);
        Scene scene = new Scene(box, 320, 240);
        primaryStage.setScene(scene);
        primaryStage.setTitle("anas");
        primaryStage.show();
    }

    class MyHandlerClass implements EventHandler<ActionEvent> {

        @Override
        public void handle(ActionEvent e) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION, "You clicked the button.");
            alert.showAndWait();
        }
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}