2012-03-14 66 views
1

我有一個主要的類,從我想要激活的主GUI,並從像下面的代碼JOptionPane新類獲取值。由於我已經打開了一個主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

*「 ..像下面的代碼,因爲我已經有一個主界面窗口中打開。」 *代碼不編譯。它怎麼可能'開放'? – 2012-03-14 11:23:38

回答

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

謝謝!稍後我會稍微嘗試一下這個代碼。 – 2012-03-14 13:09:20

+0

您的歡迎和保持微笑:-)。如果您有任何疑問,請問我:-) – 2012-03-14 13:22:54

2

JOPtionPane提供了許多preset dialog types可以使用的。然而,當你試圖做一些不適合其中某種類型的東西時,最好通過創建一個JDialog的子類來創建自己的對話框。這樣做將使您完全控制如何佈置控件以及根據需要響應按鈕點擊的能力。您將需要爲OK按鈕添加ActionListener。然後,在該回調中,您可以從文本字段中提取值。

創建自定義對話框的過程應該與您爲GUI創建主窗口的過程非常相似。除了擴展JFrame之外,您應該擴展JDialog。這是一個非常基本的example。在這個例子中,ActionListener只是關閉對話框。您需要添加更多的代碼,以從文本字段中提取值並將其提供到代碼的其餘部分所需的位置。

+0

我試圖做到這一點,因爲沒有multipli textfields選項 – 2012-03-14 11:10:22

+0

有些幫助是preciated因爲我不知道該怎麼辦? – 2012-03-14 11:20:23

+0

我已經取得了一些進展,但我會詳細介紹一下如何實現actionlistener並獲取我輸入的值? – 2012-03-14 11:31:20