【问题标题】:Using a JDialog as input使用 JDialog 作为输入
【发布时间】:2014-10-27 18:10:58
【问题描述】:

我正在尝试使用JDialog 作为String 的输入。但我得到的文字是在我点击按钮之前的。

这是我的对话:

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();

【问题讨论】:

  • 您需要为您的按钮制作一个 ActionListener,然后仅在按下按钮后获取代码。我的代码 HERE 也有类似的问题
  • 或者更确切地说,制作另一个按钮来处理它,因为我看到这个按钮用于关闭
  • 在你的构造函数中,添加“setModal(true);”
  • 但我的意思是String txt = input.getName(); 在对话框打开时获取值,我想在它关闭时获取它
  • 正确,因为它是自动执行的,从字面上看,它打开的那一秒,它就会接受输入。我是说,在按下按钮或其他东西之前不要接受输入,否则,你将无法获取输入

标签: java swing jdialog


【解决方案1】:
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。还给了它一个ActionListenerActionListener,让它按照你的要求去做。现在,我可以控制输入发生的时间。

【讨论】:

    【解决方案2】:

    您可以为此使用 JOptionPane。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 2016-10-06
      • 2011-12-04
      • 1970-01-01
      相关资源
      最近更新 更多