【问题标题】:Access to Java Swing TextField from other class从其他类访问 Java Swing TextField
【发布时间】:2011-03-12 20:41:41
【问题描述】:

Java Swing 文本输入有问题。我在A 类中有一个方法inputData(),当我调用它时,该方法应该等待,而用户在B 类中填写TextField input 并按ENTER。最后,方法inputData() 应该有用户写的文本。我该如何解决?

class A {
    B b = new B();
    public A() {
        inputData();
    }

    public char[] inputData() {
        // there I would like to get text 
        // from TextField from class B
    }
}

//-------------------------------

class B extends JFrame{
    private JTexField input;

    public B() {
    }

    private void inputKeyPressed(KeyEvent e) {                                   
        if (e.getKeyCode() == 10) {  // pressed ENTER
            input.getText()
            input.setText(null);
        }
    } 
}

【问题讨论】:

    标签: java swing textfield


    【解决方案1】:

    您可能实际上并不需要 JTextField。听起来您正在等待来自用户的一行输入,这实际上应该是一个 JOptionPane。此处描述了如何执行此操作:

    http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html#input

    基本上,JOptionPane.showInputDialog() 将导致弹出一个窗口,其中包含一个文本字段和确定/取消按钮,如果您按下回车,它将接受您的输入。这消除了对另一个类的需要。

    你可以把它放在你的 inputData() 方法中:

    inputData()
    {
        String input = JOptionPane.showInputDialog(...);
        //input is whatever the user inputted
    }
    

    如果这不是您要查找的内容,并且您想要一个保持打开状态的文本字段,那么您可能真正想要的是 JTextField 旁边的“提交”按钮,该按钮允许用户决定何时提交文本。在这种情况下,您可以:

    class B extends JFrame
    {
        private A myA;
    
        private JTextField input;
    
        private JButton submitButton;
    
        public B()
        {
            submitButton.addActionListener(new SubmitListener());
        }
    
        private class SubmitListener
        {
            //this method is called every time the submitButton is clicked
            public void actionPerformed(ActionEvent ae)
            {
                myA.sendInput(inputField.getText());
                //A will need a method sendInput(String)
            }
        }
    }
    

    【讨论】:

    • 关于 JOptionPane (1+) 的优点。 JDialog 将是另一个类似的不错的选择。
    【解决方案2】:

    文本域?既然它是一个 Swing 项目,我希望你的意思是一个 JTextField,对吧?并且不要向其中添加 KeyListener,而是添加 ActionListener,因为这些是在用户按下 Enter 时触发的。解决您的问题的一种方法是为 GUI 类(此处命名为 B)提供一个公共方法,该方法将允许外部类将 ActionListener 添加到 JTextField。也许你可以称它为 addActionListenerToInput(ActionListener listener)。然后A类就可以给B添加监听了,回车的时候会调用actionPerformed代码。

    例如,

    class A {
       B b = new B();
       public A() {
           //inputData();
          b.addActionListenerToInput(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                inputActionPerformed(e);
             }
          });
       }
    
       private void inputActionPerformed(ActionEvent e) {
          JTextField input = (JTextField) e.getSource();
          String text = input.getText();
          input.setText("");
    
          // do what you want with the text String here
       }
    }
    
    //-------------------------------
    
    class B extends JFrame{
       private JTextField input;
    
       public B() {
       }
    
       public void addActionListenerToInput(ActionListener listener) {
          input.addActionListener(listener);
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      相关资源
      最近更新 更多