JOptionPane JComponent 의 하위 클래스입니다. 모달 대화 상자를 만들고 사용자 지정하기 위한 정적 메서드가 포함된 클래스 간단한 코드를 사용하여 JOptionPane JDialog 대신 사용됩니다. 코드의 복잡성을 최소화합니다. JOptionPane 네 가지 표준 아이콘(질문, 정보, 경고 및 오류) 중 하나가 있는 대화 상자를 표시합니다. ) 또는 사용자가 지정한 사용자 정의 아이콘.
JOptionPane 클래스는 네 가지 유형의 대화 상자를 표시하는 데 사용됩니다.
- 메시지 대화 상자 - 사용자에게 경고하는 아이콘을 추가할 수 있도록 하는 메시지를 표시하는 대화 상자.
- 확인 대화 상자 - 메시지를 보내는 것 외에 사용자가 질문에 답할 수 있는 대화 상자.
- 입력 대화 상자 - 메시지를 보내는 것 외에 텍스트를 입력할 수 있는 대화 상자.
- 옵션 대화 상자 - 이전의 세 가지 유형을 다루는 대화 상자.
예시
import javax.swing.*;
public class JoptionPaneTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JoptionPane Test");
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JOptionPane.showMessageDialog(frame, "Hello Java");
JOptionPane.showMessageDialog(frame, "You have less amount, please recharge","Apocalyptic message", JOptionPane.WARNING_MESSAGE);
int result = JOptionPane.showConfirmDialog(null, "Do you want to remove item now?");
switch (result) {
case JOptionPane.YES_OPTION:
System.out.println("Yes");
break;
case JOptionPane.NO_OPTION:
System.out.println("No");
break;
case JOptionPane.CANCEL_OPTION:
System.out.println("Cancel");
break;
case JOptionPane.CLOSED_OPTION:
System.out.println("Closed");
break;
}
String name = JOptionPane.showInputDialog(null, "Please enter your name.");
System.out.println(name);
JTextField userField = new JTextField();
JPasswordField passField = new JPasswordField();
String message = "Please enter your user name and password.";
result = JOptionPane.showOptionDialog(frame, new Object[] {message, userField, passField},
"Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if (result == JOptionPane.OK_OPTION)
System.out.println(userField.getText() + " " + new String(passField.getPassword()));
System.exit(0);
}
} 출력




