从其他类访问 Java Swing TextField
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5285364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access to Java Swing TextField from other class
提问by Martynas
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?
我有 Java Swing 文本输入的问题。我inputData()
在类中有一个方法,A
当我调用它时,该方法应该等待,同时用户input
在类中填充 TextFieldB
并按 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);
}
}
}
采纳答案by donnyton
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:
您可能实际上并不想要 JTextField。听起来您正在等待来自用户的一行输入,这实际上应该是一个 JOptionPane。此处描述了如何执行此操作:
http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html#input
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.
基本上, JOptionPane.showInputDialog() 将导致弹出一个包含文本字段和确定/取消按钮的窗口,如果您按 Enter 它将接受您的输入。这消除了对另一个类的需要。
You'd put it in your inputData() method:
你可以把它放在你的 inputData() 方法中:
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:
如果这不是您要查找的内容并且您想要一个保持打开状态的文本字段,那么您可能真正想要的是 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)
}
}
}
回答by Hovercraft Full Of Eels
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.
文本域?由于它是一个 Swing 项目,我希望您指的是 JTextField,对吗?并且不要向其中添加 KeyListener,而是添加 ActionListener,因为当用户按下 Enter 键时会触发这些。解决您的问题的一种方法是为 GUI 类(此处命名为 B)提供一个公共方法,该方法将允许外部类将 ActionListener 添加到 JTextField。也许你可以称之为 addActionListenerToInput(ActionListener listener)。然后A类可以给B添加监听器,回车时会调用actionPerformed代码。
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);
}
}