2013-03-11 3 views
2

간단한 데이터 입력 대화 상자를위한 맞춤 패널을 만들려고합니다. 레이블과 텍스트 필드를 추가하는 사용자 정의 패널을 만들었습니다. 표시를 위해 JOptionPane에 추가하려고합니다.자바가 JOptionPane에 컴포넌트를 추가하는 중

내가 호출하면 내 구성 요소는 표시되지 않지만 JOptionPane 버튼은 수행합니다. 이 작업을 수행하는 올바른 방법은 무엇입니까? 고마워요 !! 당신은 당신의 TestPanel 메인 패널을 추가 할 필요가

public class TestPanel extends JPanel { 

JTextField custIdTextField; 
JTextField companyTextField; 
JTextField firstNameTextField; 
JTextField lastNameTextField; 

public TestPanel() { 
    initUI(); 
} 

public final void initUI() { 

    // create the panel and set the layout 
    JPanel main = new JPanel(); 
    main.setLayout(new GridLayout(4,4)); 

    // create the labels 
    JLabel custIdLabel = new JLabel("Cust Id: "); 
    JLabel companyLabel = new JLabel("Company: "); 
    JLabel firstNameLabel = new JLabel("First Name: "); 
    JLabel lastNameLabel = new JLabel("Last Name: "); 

    // create the text fields 
    custIdTextField = new JTextField(); 
    companyTextField = new JTextField(); 
    firstNameTextField = new JTextField(); 
    lastNameTextField = new JTextField(); 

    // add componets to panel 
    main.add(custIdLabel); 
    main.add(custIdTextField); 
    main.add(companyLabel); 
    main.add(companyTextField); 
    main.add(firstNameLabel); 
    main.add(firstNameTextField); 
    main.add(lastNameLabel); 
    main.add(lastNameTextField); 
} 

답변

4

:

public class ListenCustEdit implements ActionListener { 
    @Override 
    @SuppressWarnings("empty-statement") 
    public void actionPerformed(ActionEvent e) { 
     TestPanel panel = new TestPanel(); 
     int input = JOptionPane.showConfirmDialog(frame, panel, "Edit Customer:" 
         ,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 
     if (input == 0) { 
      // OK 
      JOptionPane.showMessageDialog(frame,"Changes savewd"); 
     } else { 
      // Cancel 
      JOptionPane.showMessageDialog(frame, "No changes were saved"); 
     } 
    } 
} 

여기 내 사용자 지정 패널 클래스입니다 : 내 사용자 정의 패널을 만들고있는 JOptionPane를 호출 할 경우 다음

이다.

public final void initUI() { 
    // ... 
    add(main); 
} 
+0

감사! 정말 감사. 나는 아직도이 GUI 물건들을 모두 배우고있다. – wyoskibum

1

생성자의 TestPanel 인스턴스에 기본 Panel을 추가하지 않았습니다.

+0

감사합니다. 너는 옳았다. – wyoskibum

3

initUI에서 JPanel ("main"이라고 함)을 만들고 구성 요소로 가득 채우지 만 아무 것도하지 않습니다. TestPanel의 실제 인스턴스에 "main"을 추가하거나 "main"패널을 만드는 단계를 건너 뛰면됩니다.

첫 번째 방법 :

public final void initUI() { 
    // ... everything as before, then 
    this.add(main); 
} 

두 번째 방법 :

public final void initUI() { 
    this.setLayout(new GridLayout(4,4)); 

    // create the labels 
    JLabel custIdLabel = new JLabel("Cust Id: "); 
    JLabel companyLabel = new JLabel("Company: "); 
    JLabel firstNameLabel = new JLabel("First Name: "); 
    JLabel lastNameLabel = new JLabel("Last Name: "); 

    // create the text fields 
    custIdTextField = new JTextField(); 
    companyTextField = new JTextField(); 
    firstNameTextField = new JTextField(); 
    lastNameTextField = new JTextField(); 

    // add componets to panel 
    this.add(custIdLabel); 
    this.add(custIdTextField); 
    this.add(companyLabel); 
    this.add(companyTextField); 
    this.add(firstNameLabel); 
    this.add(firstNameTextField); 
    this.add(lastNameLabel); 
    this.add(lastNameTextField); 
} 

(이하 "이."의 엄격 필요하지 않습니다,하지만 난 그림을 위해 그들을을 추가했습니다.)

희망이 도움이됩니다!

+0

감사합니다. 많은 도움이됩니다! – wyoskibum

관련 문제