2012-10-18 3 views
0

내가위한 JOptionPane의 정적 방법을 사용하여 입력 대화 상자를 만들려고 해요 :호환되지 않는 유형의 오류

public static Object showInputDialog(Component parentComponent, 
            Object message, 
            String title, 
            int messageType, 
            Icon icon, 
            Object[] selectionValues, 
            Object initialSelectionValue) 
           throws HeadlessException 

내 코드는 다음과 같다 :

String username = JOptionPane.showInputDialog(null, 
               "Username", 
               "Pick a name",  
               JOptionPane.PLAIN_MESSAGE, 
               null, 
               null, 
               "default_name"); 

이 나를을 얻는다을 오류 :

ChatController.java:49: incompatible types 
found : java.lang.Object 
required: java.lang.String 

...

내가 부족 간단하게 뭔가가 있어야합니다

답변

2

JOptionPane.showInputDialog() 개체, as specified in the doc를 반환 해결할 것입니다,하지만 당신은 문자열을 기대하고 있습니다. 다시 그 개체 중 하나를 얻을 수 있습니다 있도록 선택 옵션

Object[] selectionValues 

Objects의 배열입니다 있습니다. 그들이 문자열로 지정되었다고 말할 수있는 것은 없습니다. 값이 문자열 인 경우 적절하게 캐스팅 할 수 있습니다.

또한 null 배열을 전달 중입니다. 문서에서 :

The user will able to choose from selectionValues, where null implies the user can input whatever they wish

1
String username =(String) JOptionPane.showInputDialog(null, 
              "Username", 
              "Pick a name",  
              JOptionPane.PLAIN_MESSAGE, 
              null, 
              null, 
              "default_name"); 

어쩌면이는

+0

감사합니다. 게시 한 즉시 문제가 실현되었습니다. P – Dean

관련 문제