Java基本对话框

时间:2018-06-20 01:07:52

标签: java dialog switch-statement joptionpane

您好,我正在做一个学校项目,但是在用Java显示基本对话框时遇到麻烦。基本上,我需要创建一个警报,说狮子的左爪有切口。我发现可以使用JOptionPane工具,但该工具会在程序开始时弹出。现在,当我在Netbeans中运行此命令时,将弹出对话框,显示消息,然后结束代码。我需要该对话框在用户选择狮子选项后显示 。任何建议将不胜感激!

 package monitoringsystem;
 import java.util.Scanner;
 import javax.swing.JOptionPane;

public class MonitoringSystem {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    boolean mainmenu = true;
    while (mainmenu) {
        System.out.println("Select an option");
        System.out.println("Press 1 to Monitor Animals");
        System.out.println("Press 2 to Monitor Habitats");
        System.out.println("Press 3 to exit");
        int Number = input.nextInt();
        switch (Number) {
            case 1:
                System.out.println("Monitor Animals");
                System.out.println("Select an option");
                System.out.println("Press 1 for Details on Lions ");
                System.out.println("Press 2 for Details on Tigers");
                System.out.println("Press 3 for Details on Bears");
                System.out.println("Press 4 for Details on Giraffes");
                System.out.println("Press 5 to return to Main Menu");
                int animalNum = input.nextInt();
                switch (animalNum) {
                    case 1:
                        System.out.println("Animal - Lion");
                        System.out.println("Name: Leo");
                        System.out.println("Age: 5");
                        System.out.println("Feeding schedule: Twice daily\n");
                        JOptionPane.showInternalMessageDialog(null, "Health concerns: Cut on front left paw", "Alert",JOptionPane.WARNING_MESSAGE);
                        break;

1 个答案:

答案 0 :(得分:0)

这是使用JavaFX显示对话框窗口的非常简单的方法。您也许可以从中学到足够的知识,以使其适合您的程序?

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {

        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        Button btnClickMe = new Button("Click Me!");

        btnClickMe.setOnAction(event -> {
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setHeaderText("The lion has a cut on its paw!");
            alert.setContentText("Run away!!");

            alert.showAndWait();
        });

        root.getChildren().add(btnClickMe);
        primaryStage.setScene(new Scene(root));

        primaryStage.show();
    }
}

这可能对您来说有点高级,但是它确实显示了一个对话框窗口!