gpt4 book ai didi

java - Java 中的基本对话框

转载 作者:太空宇宙 更新时间:2023-11-04 10:22:38 24 4
gpt4 key购买 nike

您好,我正在开展一个学校项目,但在用 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;

最佳答案

这是使用 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();
}
}

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

关于java - Java 中的基本对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50938833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com