joptionpane默认值showinputdialog

时间:2014-01-04 13:28:43

标签: java swing default-value joptionpane

我能这样做吗?我想设置我的选项窗格的默认值是9090 .. 这是我的选项窗格代码

private static int setPortNumber()
{
    String portNumber = JOptionPane.showInputDialog(frame,
            "Enter the Port number for server creation","Server Connection\n",
            JOptionPane.OK_CANCEL_OPTION);
    int PORT = Integer.parseInt(portNumber);

    return PORT;

}   

2 个答案:

答案 0 :(得分:6)

是的,你可以做到。

private static int setPortNumber()
{

    String [] possiblePorts = { "9090", "8080", "8081" }; 
    String selectedPort = (String) JOptionPane.showInputDialog(frame, "Select the Port number for server creation", "Server Connection\n", JOptionPane.OK_CANCEL_OPTION, null, possiblePorts, possiblePorts[0]);

    int PORT = Integer.parseInt(selectedPort);

    return PORT;

}

通过这种方式,用户无需只编写选择。

答案 1 :(得分:3)

String portNumber = (String) JOptionPane.showInputDialog(frame,
        "Enter the Port number for server creation",
        "Server Connection\n", JOptionPane.OK_CANCEL_OPTION, null,
        null, "9090");

阅读docs