2017-03-22 1 views
2

"예"또는 "아니오"버튼이있는 간단한 대화 상자를 만드는 방법을 알고 있습니다.두 개의 질문으로 대화 상자를 만드는 방법

Object[] options = {"yes", "no"}; 

int selection = JOptionPane.showOptionDialog(
    gameView, 
    "choose one", 
    "Message", 
    JOptionPane.YES_NO_OPTION, 
    JOptionPane.QUESTION_MESSAGE, 
    null, 
    options, 
    options[0] 
); 

하지만 지금은 두 가지 질문으로 대화를 만들고 싶습니다. 어떻게해야합니까?

I는 다음과 같이이 대화를하고 싶습니다 :

Dialog

답변

0

당신은 정말 대화에 당신이 원하는 콘텐츠를 삽입 할 수 있습니다. 현재 ("하나를 선택") 문자열을 삽입하고 있지만 실제로 컨트롤의 전체 패널에 전달할 수 : 이것은 다음과 같습니다 팝업 생성

import javax.swing.*; 

public class Main { 
    public static void main(String[] args) { 
     // Create a button group containing blue and red, with blue selected 
     final ButtonGroup color = new ButtonGroup(); 
     final JRadioButton blue = new JRadioButton("Blue"); 
     final JRadioButton red = new JRadioButton("Red"); 
     color.add(blue); 
     blue.setSelected(true); 
     color.add(red); 

     // Create a button group containing triangle and square, with triangle selected 
     final ButtonGroup shape = new ButtonGroup(); 
     final JRadioButton triangle = new JRadioButton("Triangle"); 
     final JRadioButton square = new JRadioButton("Square"); 
     shape.add(triangle); 
     triangle.setSelected(true); 
     shape.add(square); 

     // Create a panel and add labels and the radio buttons 
     final JPanel panel = new JPanel(); 
     panel.add(new JLabel("Choose a color:")); 
     panel.add(blue); 
     panel.add(red); 
     panel.add(new JLabel("Choose a shape:")); 
     panel.add(triangle); 
     panel.add(square); 

     // Show the dialog 
     JOptionPane.showMessageDialog(
      null /*gameView*/, panel, "Message", 
      JOptionPane.QUESTION_MESSAGE 
     ); 

     // Print the selection 
     if (blue.isSelected()) { 
      System.out.println("Blue was selected"); 
     } 
     else { 
      System.out.println("Red was selected"); 
     } 

     if (triangle.isSelected()) { 
      System.out.println("Triangle was selected"); 
     } 
     else { 
      System.out.println("Square was selected"); 
     } 
    } 
} 

:

Dialog

를이 귀하의 이미지와 완전히 똑같지는 않지만 귀하의 질문 범위를 벗어납니다. 이를 변경하려면 다양한 유형의 레이아웃으로 놀 필요가 있습니다. 시동기를 위해 See this question.

관련 문제