Java 如何";翻译;此扫描仪将输入到JOptionPane showOptionDialog中

Java 如何";翻译;此扫描仪将输入到JOptionPane showOptionDialog中,java,swing,joptionpane,Java,Swing,Joptionpane,我正在努力使我的代码更加用户友好。我有这部分代码,想知道如何将其转换为joptionpane。 我发现这个intresult=JOptionPane.showConfirmDialog(框架,“继续打印?”) 但是使用另一个jframe似乎有点奇怪 System.out.println("Make more selections? Type Yes or No"); Scanner scanre = new Scanner( System.in ); String selecend; sele

我正在努力使我的代码更加用户友好。我有这部分代码,想知道如何将其转换为joptionpane。 我发现这个
intresult=JOptionPane.showConfirmDialog(框架,“继续打印?”)
但是使用另一个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();
};      

我认为您可能需要使用JFrame,因为在那里您可以将所有系统输出消息更改为使用标签状态(一个基于消息更改文本的JLabel) 仍然不需要使用框架来使用showConfirmDialog

JOptionPane.showConfirmDialog(null,"choose one", "choose one", JOptionPane.YES_NO_OPTION);
您可以查看以下示例和文档:

示例中的框架是此对话框消息的父级:

parentComponent - 
determines the Frame in which the dialog is displayed; 
if null, or if the parentComponent has no Frame, a default Frame is used

我认为,如果对话框处于活动状态,则此父帧用于防止焦点聚焦在父帧上。类似于windows警报的功能当您在继续使用程序之前必须接受或关闭对话框时,它会锁定gui。

我认为您可能需要使用JFrame,因为在那里您可以将所有系统输出消息更改为使用标签状态(一个根据消息更改文本的JLabel) 仍然不需要使用框架来使用showConfirmDialog

JOptionPane.showConfirmDialog(null,"choose one", "choose one", JOptionPane.YES_NO_OPTION);
您可以查看以下示例和文档:

示例中的框架是此对话框消息的父级:

parentComponent - 
determines the Frame in which the dialog is displayed; 
if null, or if the parentComponent has no Frame, a default Frame is used
我认为,如果对话框处于活动状态,则此父帧用于防止焦点聚焦在父帧上。类似于windows警报的功能当您在继续使用程序之前必须接受或关闭对话框时,它会锁定gui。

尝试了此功能,但效果良好:

int dialogresult = JOptionPane.showConfirmDialog (null, "Continue Selecting?");

                       if(dialogresult == JOptionPane.YES_OPTION){
                           query();
                       }else if(dialogresult == JOptionPane.NO_OPTION) {

                       }
尝试了这个方法,效果很好:

int dialogresult = JOptionPane.showConfirmDialog (null, "Continue Selecting?");

                       if(dialogresult == JOptionPane.YES_OPTION){
                           query();
                       }else if(dialogresult == JOptionPane.NO_OPTION) {

                       }
更方便用户

下一个呢

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;
}

更方便用户

下一个呢

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;
}

我的意思是除了问我们。顺便说一句——“但是使用另一个jframe似乎有点奇怪。”所以使用您拥有的任何框架,但请注意参数也可以是组件,或者
null
。。我是说除了问我们。顺便说一句——“但是使用另一个jframe似乎有点奇怪。”所以使用您拥有的任何框架,但请注意参数也可以是组件,或者
null
。。