티스토리 뷰

Java - JOptionpane

입력받기

 

 

 

 

우선, JOptionpane 의 사용법에대해서 알아보겠습니다.

ShowInputDialog

가장 기본적인

JOptionPane.showInputDialog();
에 대해서는 영어단어 암기프로그램에서 확인해보시길 바랍니다.

 

String input;

 

input = JOptionPane.showInputDialog("입력해주십시요");

 

 

ShowOptionDialog

 


                           public static int showOptionDialog(Component parentComponent,
                                   Object message,
                                   String title,
                                   int optionType,
                                   int messageType,
                                   Icon icon,
                                   Object[] options,
                                   Object initialValue

                               throws HeadlessException

 

 

 

 위를 보시면  showOptionDialog의 사용방법을 대충 아실수 있을것입니다.

 

 

String[] test = {"버튼1", "버튼2", ...};

int rc = JoptionPane.showOptionDialog(null,

"메세지 박스내용",

"메세지위의 제목",

0,

JOptionPane.INFORMATION_MESSAGE,

null,

test,                <-

test[0]);          <-이 두분은 버튼의 목록이 저장된 배열을 입력하시면 됩니다. 

 

 

아래의 예시를통해 자세히 설명하겠습니다.

 

public class Test {
    
    
    public static void main(String[] args) {

        
        
        
         String[] buttons = {"찬성""반대""기권"};            
         int go = JOptionPane.showOptionDialog(null
                     "담배값 인상"
                     "설문조사",                     
                     0,
                     JOptionPane.INFORMATION_MESSAGE, 
                     null,
                     buttons,
                     buttons[0]);
         

         System.out.println("go="+go);         
         if(go==0){
             System.out.println("수락버튼");
         }else if(go==1){
             System.out.println("거절버튼");             
         }else if(go==2){
             System.out.println("기권버튼");             
         }
    }

 

 

위의 코드를 보시면

버튼에는 번호가 저장되있어서

int 형에 누른버튼의 번호가 저장됩니다.

 

그리고 누른버튼의 번호를 인식해서 누른버튼을 출력하도록 되있습니다.

 

JOptionpane는 이외에도 다양한 창이 있습니다.\

 

 

ShowInputDialog

 

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

         

 

Object[] list = {"찬성", "반대", "기권"};

Object select = JOptionPane.showInputDialog(null,

"담배값 인상",

"투표해주십시요",

JOptionPane.INFORMATION_MESSAGE, 

null,

list,

list[0]);

 

위의 OptionDialog의 사용법과 비슷한데요,

한가지 다른점이 있습니다.

 

버튼의 목록이 String 이 아니라 Object 로 변경한점만 다르네요

 

 

 

public class test {
    
    
    public static void main(String[] args) {

                
        //유저에게 String 의 선택을 요구하는 다이얼로그의 표시
        Object[] list = {"찬성", "반대", "기권"};
        Object select
            = JOptionPane.showInputDialog(null,
                                "담배값 인상", 
                                "투표해주십시요",

                                JOptionPane.INFORMATION_MESSAGE, 
                                null, 
                                list, 

                                list[0]); 

    
         System.out.println("select="+(select));         
        
    }
}

 

 

 

위의 코드를 실행시키면 이런창이 뜹니다.

목록 선택방식으로 입력을 받습니다.

'프로그래밍 > java' 카테고리의 다른 글

Java잡지식 - 네트워킹  (0) 2015.05.17
Java - 객체지향(7) package & import  (0) 2015.04.13
Java - Scanner 입력받기  (0) 2015.04.04
JAVA - 객체지향(6)  (0) 2015.04.04
java - 객체지향(5)  (0) 2015.04.03
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함