它這樣做是因爲您在message[]
中也添加了JTextField
對象。
Object[] message = {"File name", fileName};//sending filename as message
因此,所示的第一JTextField
是使用inputdialog固有之一,另外一個是你自己JTextField
您要發送的消息。
我猜想是你想發送fileName
的內容給消息。在這種情況下,你的代碼應該是這樣的:
public static void main(String[] args) {
JTextField fileName = new JTextField();
Object[] message = {"File name", fileName.getText()};//send text of filename
String option = JOptionPane.showInputDialog(null, message, "Add New", JOptionPane.OK_CANCEL_OPTION);
System.out.println(fileName.getText());
}
UPDATE
如果你想只取輸入,則沒有必要發送對象filename
的消息。您應該簡單地按照以下步驟操作:
public static void main(String[] args) {
//JTextField fileName = new JTextField();
Object[] message = {"File name"};
String option = JOptionPane.showInputDialog(null, message, "Add New", JOptionPane.OK_CANCEL_OPTION);
if (option == null)
System.out.println("Cancell is clicked..");
else
System.out.println(option+ " is entered by user");
}
其實,我想從用戶的輸入。但是這解釋了。 – 2013-03-24 15:36:59
如果你只想使用輸入,那麼不要發送'filename'作爲消息。「JOptionPane.inputDialog」會自動爲你做。 – 2013-03-24 15:39:08
那麼如何檢查用戶是否點擊確定或取消? – 2013-03-24 15:54:34