从其他类访问 Java Swing TextField

发布于 2024-10-21 07:59:40 字数 770 浏览 1 评论 0原文

我在使用 Java Swing 文本输入时遇到问题。我在类 A 中有一个方法 inputData() ,当我调用它时,该方法应该等待用户在类 中填充 TextField input >B 并按 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);
        }
    } 
}

I have a problem with Java Swing text input. I have a method inputData() in class A and when I call it, the method should wait while user fill TextField input in class B and press ENTER. Finally, the method inputData() should have the text that user wrote. How could I solve it?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花肆意 2024-10-28 07:59:40

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

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

基本上, JOptionPane.showInputDialog() 会弹出一个包含文本字段和“确定”/“取消”按钮的窗口,如果您按 Enter 键它会接受你的输入。这消除了对另一个类的需要。

您可以将其放入 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)
        }
    }
}

You may not actually want a JTextField. It sounds like you're waiting for a line of input from the user, which should really be a JOptionPane. How to do this is described here:

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

Basically, JOptionPane.showInputDialog() will cause a window to pop up that contains a text field and OK/Cancel buttons, and if you press enter it will take your input. This eliminates the need for another class.

You'd put it in your inputData() method:

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

If that's not what you're looking for and you want a text field that stays open, perhaps what you really want is a "Submit" button next to your JTextField that allows the user to decide when to submit the text. In that case, you could have:

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)
        }
    }
}
初相遇 2024-10-28 07:59:40

文本字段?由于它是一个 Swing 项目,我希望您指的是 JTextField,对吧?并且不要向其中添加 KeyListener,而是添加 ActionListener,因为当用户按 Enter 时会触发这些监听器。解决问题的一种方法是为 GUI 类(此处名为 B)提供一个公共方法,该方法将允许外部类向 JTextField 添加 ActionListener。也许您可以将其称为 addActionListenerToInput(ActionListenerlistener)。然后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);
   }

}

TextField? Since it's a Swing project, I hope you mean a JTextField, right? And don't add a KeyListener to it, but rather an ActionListener since these are triggered when the user presses Enter. One way to solve your problem is to give the GUI class (here named B) a public method that will allow outside classes to add an ActionListener to the JTextField. Perhaps you can call it addActionListenerToInput(ActionListener listener). Then Class A can add the listener to B, and the actionPerformed code will be called when enter is pressed.

e.g.,

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文