Java- 读取 JOptionPane 上的输入和输出

Java- Reading input and output on JOptionPane

我在我的代码上苦苦挣扎,它适用于我评论的代码,但当我使用 JOptionPane.showOptionDialog 时它不起作用,我正在使用选项对话框,因为我希望显示该代码跳过或取消按钮,我也不知道如何更改该按钮的字符串,如果有人能为我解决这个问题,我将不胜感激。

import javax.swing.*;
import java.util.Scanner;

public class Login {
    public static void main(String[] args) {
        String userName;

        //This will ask user to input the username
        userName = JOptionPane.showInputDialog(null, "Please enter your name", "Welcome", JOptionPane.INFORMATION_MESSAGE);

        // JOptionPane.showMessageDialog   <<<This code replace the below one then it will work perfectly
        JOptionPane.showOptionDialog(null, "Welcome " + userName + "\n\nWould you like to have a tutorial about this game?", "Welcome", JOptionPane.OK_CANCEL_OPTION);
    }
}

您的问题是 JOptionPane.showOptionDialog 需要比您提供的更多的参数。如果你检查 documentation 你会看到你还必须提供消息类型、图标、选项和默认选项,其中最后 3 个可以为空,所以你的调用必须是

JOptionPane.showOptionDialog(
    null,
    "Welcome " + userName + "\n\nWould you like to have a tutorial about this game?",
    "Welcome",
    JOptionPane.OK_CANCEL_OPTION,
    JOptionPane.INFORMATION_MESSAGE,
    null,
    null,
    null
);