티스토리 뷰

배운 것 기록/java

JOptionPane

키죽 2022. 7. 27. 10:34

JOptionPane

- 다이얼로그(팝업창)을 생성하는 객체

- JOptionPane 클래스의 static 메서드인 show[다이얼로그 종류 이름]Dialog()메서드를 호출해서 표시한다

 

- 생성자 파라미터에 따라 각각 다른 옵션 설정이 가능하다

1. MessageDialog : 사용자에게 메세지(팝업창)를 표시하기 위한 다이얼로그 (alert) 

2. ConfirmDialog : 사용자의 선택을 받도록 버튼을 표시하는 다이얼로그 (confirm)

3. InputDialog : 사용자로부터 데이터를 입력받기 위한 다이얼로그 (prompt) 

 

JFrame f = new JFrame("Dialog 기초");
f.setBounds(400, 300, 300, 200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// JPanel 객체 생성 후 CENTER에 부착
JPanel p = new JPanel();
f.add(p, BorderLayout.CENTER);

// 버튼 생성
JButton btnMessage = new JButton("Message");
JButton btnConfirm = new JButton("Confirm");
JButton btnInput = new JButton("Input");

p.add(btnMessage);
p.add(btnConfirm);
p.add(btnInput);


f.setVisible(true);

 

 

 

테스트~

// 각 버튼 이벤트 처리 - 4단계
// => ActionListener 구현체 내부에서 버튼 3개를 판별
ActionListener listener = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == btnMessage) {
            System.out.println("message");
        } else if(e.getSource() == btnConfirm) {
            System.out.println("confirm");
        } else if(e.getSource() == btnInput) {
            System.out.println("input");
        }
    }
};

btnMessage.addActionListener(listener);
btnConfirm.addActionListener(listener);
btnInput.addActionListener(listener);

f.setVisible(true);

 

잘 뜬다!

 

 

에러메시지 형태 설정 시

int로 보내는 건(여기서는 JOPtionPane.ERROR_MESSAGE 부분이다) 상수라고 생각하기!

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == btnMessage) {
        JOptionPane.showMessageDialog(
                f,				// 다이얼로그 표시 대상 객체
                "다이얼로그 메세지",		 // 본문
                "다이얼로그 제목", 		 // 제목
                JOptionPane.ERROR_MESSAGE);	// 메시지 형태
}

 

메세지 버튼을 누르면 이렇게 뜬다!

 

 

btnConfirm, btnInput도 설정

ActionListener listener = new ActionListener() {
			
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == btnMessage) {
            JOptionPane.showMessageDialog(f, "다이얼로그 메세지");

        } else if(e.getSource() == btnConfirm) {

            JOptionPane.showConfirmDialog(f, "종료할래?");
            int select = JOptionPane.showConfirmDialog(
                    f,			// 표시할 대상 객체
                    "종료할래?",	// 메세지
                    "종료!",		// 제목
                    JOptionPane.YES_NO_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE);

            System.out.println(select);	// 0(Yes), 1(No), 2(Cancle)
            if(select == JOptionPane.YES_OPTION) {		// 0
                System.out.println("종료!");
            } else if(select == JOptionPane.NO_OPTION) {	// 1
                System.out.println("종료 안함!");
            } else if(select == JOptionPane.CANCEL_OPTION) {	// 2
                System.out.println("종료 취소!");
            } else if(select == JOptionPane.CLOSED_OPTION) {	// -1
                System.out.println("Confirm 창 닫음");
            }

        } else if(e.getSource() == btnInput) {	// prompt

            String input = JOptionPane.showInputDialog(f, "검색할 이름 입력");
            if(input != null) {	// 무조건 확인 버튼을 누름
                System.out.println("입력 데이터 : " + input);
            }

            System.out.println(input);	// null
        }
    }
};

btnConfirm, btnInput, console

 

 

'배운 것 기록 > java' 카테고리의 다른 글

JTable  (0) 2022.07.28
JCheckBox, JComboBox, JSpinner  (0) 2022.07.27
JTextField, JPanel  (0) 2022.07.27
레이아웃(layout)  (0) 2022.07.26
Event  (0) 2022.07.20
댓글
최근에 올라온 글
«   2025/02   »
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
글 보관함