从格式化的对话框java中获取输入

时间:2014-06-29 11:53:23

标签: java

首先,我想创建一个带有4个选项的对话框: “跳过”,“替换”,“全部跳过”,“全部替换”

然后我想要检查哪个按钮已被点击并按照它工作。 我怎么能这样做?

我尝试使用此对话框但是无法检查单击了哪个按钮:

String[] choices={"Skip","Replace","Skip All","Replace all"};
                JOptionPane.showOptionDialog(null,"Want to skip File" , "Multiple Files", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);

2 个答案:

答案 0 :(得分:0)

查看JOptionPane的文档:

Returns:
an integer indicating the option chosen by the user, or CLOSED_OPTION if the user closed the dialog 

如果" Skip"它重新出现0被按下,1如果"替换"按下等等。

答案 1 :(得分:-1)

import javax.swing.JOptionPane;

public class JOptionPaneTest {
public static void main(String[] args) {
    String[] choices={"Skip","Replace","Skip All","Replace all"};
    int response = JOptionPane.showOptionDialog(
                           null                          // Center in window.
                         , "Want to skip File"           // Message
                         , "Multiple Files"              // Title in titlebar
                         , JOptionPane.YES_NO_OPTION     // Option type
                         , JOptionPane.QUESTION_MESSAGE  // messageType
                         , null                          // Icon (none)
                         , choices                       // Button text as above.
                         , choices[0]                    // Default button's label
                       );
    switch (response) {
            case 0: 
                // Code for skip
                break;
            case 1:
                // Code for replace
                break;
            case 2:
                // Code for skip all
                break;
            case 3:
                // Code for replace all
                break;
            case -1:
                // Code for the close box(-1)
                System.exit(0);
            default:
                // Code to catch errors
        }
    }
}
相关问题