我试图使我的代码更加用户友好。我有这部分代码,想知道如何将其转换为joptionpane。
我发现了这个int result = JOptionPane.showConfirmDialog(frame, "Continue printing?");
但是使用另一个jframe似乎有点奇怪。

System.out.println("Make more selections? Type Yes or No");

Scanner scanre = new Scanner( System.in );
String selecend;
selecend = scanre.next();
if(selecend.equalsIgnoreCase("Yes")) {
    System.out.println("Enter next selection: ");
    query();
};

最佳答案

更人性化


接下来呢?

private Something showMessage() {

    // null for 'console' mode or this if the enclosing type is a frame
    Component parentComponent = null;
    Object message = "Make more selections?";
    String title = "Message";
    int optionType = JOptionPane.YES_NO_OPTION; // 2 buttons
    int messageType = JOptionPane.QUESTION_MESSAGE; // icon from style
    Icon icon = null;

    // String in the buttons!
    Object[] options = { "Yup!", "Nope!" };

    // option saves the index 'clicked'
    int option = JOptionPane.showOptionDialog(
            parentComponent, message, title,
            optionType, messageType, icon,
            options, options[0]);

    switch (option) {
    case 0:
        // button with "Yup!"
        break;
    case 1:
        // button with "Nope!"
        break;
    default:
        // you close the dialog or press 'escape'
        break;
    }

    return Something;
}

10-07 13:17