从其他类访问 Java Swing TextField
我在使用 Java Swing 文本输入时遇到问题。我在类 A
中有一个方法 inputData()
,当我调用它时,该方法应该等待用户在类 中填充 TextField
并按 ENTER 键。最后,input
>BinputData()
方法应该包含用户编写的文本。我该如何解决呢?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上可能并不需要 JTextField。听起来您正在等待用户的一行输入,这实际上应该是一个 JOptionPane。这里描述了如何执行此操作:
http://download.oracle .com/javase/tutorial/uiswing/components/dialog.html#input
基本上, JOptionPane.showInputDialog() 会弹出一个包含文本字段和“确定”/“取消”按钮的窗口,如果您按 Enter 键它会接受你的输入。这消除了对另一个类的需要。
您可以将其放入 inputData() 方法中:
如果这不是您要寻找的内容并且您想要一个保持打开状态的文本字段,那么您真正想要的是 JTextField 旁边的“提交”按钮,该按钮允许用户决定何时提交文本。在这种情况下,您可以:
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:
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:
文本字段?由于它是一个 Swing 项目,我希望您指的是 JTextField,对吧?并且不要向其中添加 KeyListener,而是添加 ActionListener,因为当用户按 Enter 时会触发这些监听器。解决问题的一种方法是为 GUI 类(此处名为 B)提供一个公共方法,该方法将允许外部类向 JTextField 添加 ActionListener。也许您可以将其称为 addActionListenerToInput(ActionListenerlistener)。然后A类就可以给B添加监听器,按下回车时就会调用actionPerformed代码。
例如,
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.,