2012-03-14 3 views
1

나는 아래의 코드와 같은 JOptionPane을 사용하여 새 클래스의 값을 가져오고 활성화 할 기본 GUI가있는 메인 클래스를 가지고 있습니다. 내가 메인 GUI 윈도우를 이미 열었으므로, 어떻게 그리고 어디에서 클래스를 활성화/호출해야합니까? 그리고 마지막으로 JOptionPane에서 값을 얻으려면 어떻게해야합니까? 도움은 preciated입니다! 감사!다른 클래스에서 JOptionPane을 활성화하는 방법은 무엇입니까?

import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class OptionPaneTest { 

    JPanel myPanel = new JPanel(); 
    JTextField field1 = new JTextField(10); 
    JTextField field2 = new JTextField(10); 
    myPanel.add(field1); 
    myPanel.add(field2); 
    JOptionPane.showMessageDialog(null, myPanel); 

} 

편집 :

InputNewPerson nyPerson = new InputNewPerson(); 
JOptionPane.showMessageDialog(null, nyPerson); 
String test = nyPerson.inputName.getText(); 
+1

* * 그 코드는하지 않습니다 엮다. 어떻게 '열린'수 있을까요? –

답변

2

질문에 대한 답을 찾았 으면 이런 것이 필요합니다. 내가 작은 JDialog을 만들었는데 UserNameAnswer을 입력하면 SUBMIT JButton을 누를 때마다 원래의 GUI로 전달되어 해당 필드에 표시됩니다.

이 코드에 손을 시도하고 발생할 수있는 모든 질문 질문 : ". 나는 이미 메인 GUI 창이 열립니다 가지고 있기 때문에 .. 아래의 코드처럼 ..."

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

/* 
* This is the actual GUI class, which will get 
* values from the JDIalog class. 
*/ 
public class GetDialogValues extends JFrame 
{ 
    private JTextField userField; 
    private JTextField questionField; 

    public GetDialogValues() 
    { 
     super("JFRAME"); 
    } 

    private void createAndDisplayGUI(GetDialogValues gdv) 
    {  
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLocationByPlatform(true); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new GridLayout(0, 2)); 

     JLabel userName = new JLabel("USERNAME : "); 
     userField = new JTextField(); 
     JLabel questionLabel = new JLabel("Are you feeling GOOD ?"); 
     questionField = new JTextField(); 

     contentPane.add(userName); 
     contentPane.add(userField); 
     contentPane.add(questionLabel); 
     contentPane.add(questionField); 

     getContentPane().add(contentPane); 
     pack(); 
     setVisible(true); 

     InputDialog id = new InputDialog(gdv, "Get INPUT : ", true); 
    } 

    public void setValues(final String username, final String answer) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       userField.setText(username); 
       questionField.setText(answer); 
      } 
     }); 
    } 

    public static void main(String... args) 
    { 
     Runnable runnable = new Runnable() 
     { 
      public void run() 
      { 
       GetDialogValues gdv = new GetDialogValues(); 
       gdv.createAndDisplayGUI(gdv); 
      } 
     }; 
     SwingUtilities.invokeLater(runnable); 
    } 
} 

class InputDialog extends JDialog 
{ 
    private GetDialogValues gdv; 
    private JTextField usernameField; 
    private JTextField questionField; 
    private JButton submitButton; 
    private ActionListener actionButton = new ActionListener() 
    { 
     public void actionPerformed(ActionEvent ae) 
     { 
      if (usernameField.getDocument().getLength() > 0 
       && questionField.getDocument().getLength() > 0) 
      { 
       gdv.setValues(usernameField.getText().trim() 
        , questionField.getText().trim()); 
       dispose(); 
      } 
      else if (usernameField.getDocument().getLength() == 0) 
      { 
       JOptionPane.showMessageDialog(null, "Please Enter USERNAME." 
        , "Invalid USERNAME : ", JOptionPane.ERROR_MESSAGE); 
      } 
      else if (questionField.getDocument().getLength() == 0) 
      { 
       JOptionPane.showMessageDialog(null, "Please Answer the question" 
        , "Invalid ANSWER : ", JOptionPane.ERROR_MESSAGE); 
      } 
     } 
    }; 

    public InputDialog(GetDialogValues gdv, String title, boolean isModal) 
    { 
     this.gdv = gdv; 
     setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
     setLayout(new BorderLayout()); 
     setModal(isModal); 
     setTitle(title); 

     JPanel panel = new JPanel(); 
     panel.setLayout(new GridLayout(0, 2)); 
     JLabel usernameLabel = new JLabel("Enter USERNAME : "); 
     usernameField = new JTextField(); 
     JLabel questionLabel = new JLabel("How are you feeling ?"); 
     questionField = new JTextField(); 

     panel.add(usernameLabel); 
     panel.add(usernameField); 
     panel.add(questionLabel); 
     panel.add(questionField); 

     submitButton = new JButton("SUBMIT"); 
     submitButton.addActionListener(actionButton); 

     add(panel, BorderLayout.CENTER); 
     add(submitButton, BorderLayout.PAGE_END); 

     pack(); 
     setVisible(true); 
    } 
} 
+0

감사합니다. 나중에 코드를 시도해 봅니다. –

+0

환영하고 계속 미소를 지으며 :-). 의심스러운 점이 있다면 나에게 물어보십시오. –

2

JOPtionPane 사용할 수 있습니다 preset dialog types을 제공합니다. 그러나 이러한 유형 중 하나의 곰팡이에 적합하지 않은 무언가를하려 할 때 JDialog 하위 클래스를 만들어 자신 만의 대화 상자를 만드는 것이 가장 좋습니다. 이렇게하면 컨트롤이 배치되는 방법과 원하는대로 단추 클릭에 응답하는 기능을 완벽하게 제어 할 수 있습니다. OK (확인) 버튼에 ActionListener을 추가 할 수 있습니다. 그 콜백에서 텍스트 필드에서 값을 추출 할 수 있습니다.

사용자 정의 대화 상자를 만드는 프로세스는 GUI의 기본 창을 작성한 방법과 매우 비슷해야합니다. 단, JFrame을 확장하는 대신 JDialog을 확장해야합니다. 다음은 매우 기본적인 것입니다 example. 이 예에서 ActionListener은 대화 상자를 닫습니다. 텍스트 필드의 값을 추출하고 나머지 코드에서 필요한 위치에 코드를 제공하는 코드를 추가해야합니다.

+0

multipli textfields 옵션이 없으므로이 작업을 시도했습니다 –

+0

어떻게 해야할지 모르기 때문에 일부 도움이 필요합니까? –

+0

나는 약간의 진전을 보였지만 actionListener를 구현하고 내가 입력 한 값을 얻는 방법에 대해 약간의 도움이 필요 하겠는가? –