2012-03-14 4 views
1

私は主なGUIを持っていて、ここではアクティブ化したいところから、新しいクラスからJOptionPaneの値を以下のコードのように取得します。私は既にメインのGUIウィンドウを開いているので、どのようにどこでどこで起動して/呼び出すべきですか?最後に、JOptionPaneから値を取得するにはどうすればいいですか?ヘルプはpreciateされています!ありがとう!別のクラスから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

を「私はすでにメインのGUIウィンドウが開かれていますので、以下のコードのように..」*このコードにはありませんコンパイル。どのように「開かれている」ことができますか? –

答えて

2

私はあなたの質問を見ると思います。このようなものが必要です。 JDialogを作成しました。UserNameAnswerを入力すると、SUBMIT JButtonを押すと、元の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のテキストフィールドでオプションがないので、私はこれをやろうとしていました –

+0

何か助けが欲しいですか? –

+0

私はいくつかの進歩を遂げましたが、私はどのようにactionlistenerを実装し、私が入力する値を取得するための助けをpreciateでしょうか? –

関連する問題