在对话框上

时间:2009-10-01 20:28:43

标签: java swing dialog

我正在尝试制作一个显示答案的对话框,并将打印到屏幕上但没有成功。代码如下所示:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dialogbox;
import javax.swing.JOptionPane;
/**
 *
 * @author Tyranax87
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String name = "";
        //Setting up a variable name of type string and initialising it to the empty string.
        name = JOptionPane.showInputDialog(null, "Please input your name");
        System.exit(0);
}
}

它会运行得很好,但这只会打开询问问题的对话框。有没有办法让对话框提供代码中预先写好的文本?

1 个答案:

答案 0 :(得分:4)

尝试这样:

name = (String)JOptionPane.showInputDialog(
                    null,
                    "Please input your name",
                    "Title of the window",
                    JOptionPane.PLAIN_MESSAGE,
                    icon,
                    null,
                    "default text");