2014-10-27 75 views
1

我正在嘗試使用JDialog作爲String的輸入。但是我得到的文本來自我點擊按鈕之前。使用JDialog作爲輸入

這是我的對話框:

public class AddMemberDialog extends JDialog { 

    private JTextField name; 

    public AddMemberDialog() { 
     super(new JFrame("Add Member"), "Add Member"); 
     this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); 
     this.setMinimumSize(new Dimension(500, 500)); 
     this.name = new JTextField(); 

     JButton add = new JButton("Add"); 
     add.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent arg0) { 
       close();    
      } 
     }); 
     this.setLayout(new GridLayout(2, 1, 5, 5)); 

     this.add(name); 
     this.add(add); 
     this.pack(); 
    } 

    private void close(){ this.dispose(); } 

    public String getName(){ return this.name.getText(); } 
} 

,這裏是我使用去String什麼:

AddMemberDialog input = new AddMemberDialog(); 
input.setLocationRelativeTo(this); 
input.setVisible(true); 
String txt = input.getName(); 
+0

您需要爲您的按鈕製作一個ActionListener,然後僅在按下該按鈕後才執行代碼。我有一個類似的問題與我的代碼[這裏](http://stackoverflow.com/questions/26198989/how-to-wait-for-user-input-when-using-jtextfields) – DreadHeadedDeveloper 2014-10-27 18:16:39

+0

或者說,做另一個按鈕處理,因爲我看到這個按鈕是關閉 – DreadHeadedDeveloper 2014-10-27 18:17:11

+1

在你的構造函數,添加「setModal(true);」 – ControlAltDel 2014-10-27 18:18:03

回答

2
import javax.swing.JDialog; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import java.awt.Dimension; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.GridLayout; 


public class AddMemberDialog extends JDialog 
{ 

    private JTextField name; 

    public static void main(String[] args) 
    { 

     AddMemberDialog input = new AddMemberDialog(); 
     input.setLocationRelativeTo(null); 
     input.setVisible(true); 

    } 

    public AddMemberDialog() 
    { 
     super(new JFrame("Add Member"), "Add Member"); 

     this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE); 
     this.setMinimumSize(new Dimension(500, 500)); 
     this.name = new JTextField(); 

     JButton add = new JButton("Add"); 
     add.addActionListener(
      new ActionListener() { 

       public void actionPerformed(ActionEvent arg0) { 
        close();    
       } 
      }); 



     JButton takeInput = new JButton("takeInput"); 
     takeInput.addActionListener(
      new ActionListener() 
      { 
       public void actionPerformed(ActionEvent e) 
       { 


        String txt = getName(); 
        System.out.println(txt); 

       } 
      } 
      ); 

     this.setLayout(new GridLayout(3, 1, 5, 5)); 

     this.add(name); 
     this.add(add); 
     this.add(takeInput); 
     this.pack(); 
    } 

    private void close() 
    { 

     this.dispose(); 

    } 

    public String getName() 
    {  

     return this.name.getText(); 

    } 

} 

好了,所以基本上,你的問題,這裏要說的是,如果你只需將代碼

   AddMemberDialog input = new AddMemberDialog(); 
       input.setLocationRelativeTo(this); 
       input.setVisible(true); 
       String txt = input.getName(); 

它將自動在您輸入第二個是IDE達到該行代碼。也就是說,除非在IDE到達那裏之前(IDE以毫秒爲單位),否則它將不會再接受任何輸入。所以爲了彌補,我們不會讓程序在我們做好準備之前進行投入,因此需要一個按鈕。在我上面的代碼中,我做了一個新的JButton並將其稱爲takeInput。也給了它ActionListener,並在那ActionListener,使它做你所要求的。現在,我可以控制輸入何時發生。

+0

好吧,謝謝 – CarlosMorgado 2014-10-27 19:34:25

0

可以使用的JOptionPane這一點。

import javax.swing.JOptionPane; 

public class TestClass 
{ 

    public static void main(String[] args) 
    { 
    String input = JOptionPane.showInputDialog("Add Member"); 
    System.out.println(input); 
    } 
}