简体   繁体   English

无法从另一个JFrame在JTextField中使用setText()

[英]unable setText() in JTextField from another JFrame

I have two JFrames named first and second respectively. 我有两个分别命名为first和second的JFrame。 Both frames have jtextfields to getText() and setText(). 两个框架都有用于getText()和setText()的jtextfields。 I am able to setText from first to second JFrame, but unable setText from second to first. 我能够从第一个到第二个JFrame设置setText,但是不能从第二个到第一个设置setText。

In first and second frame I have made all JTextFields public static 在第一帧和第二帧中,我将所有JTextFields设为public static

first.java first.java

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    second sec = new second();  
    sec.jTextField1.setText(this.jTextField1.getText()); // this works
    sec.jTextField2.setText(this.jTextField2.getText()); 
    sec.setVisible(true);

    public static javax.swing.JTextField jTextField1;
    public static javax.swing.JTextField jTextField2;
    private javax.swing.JButton jButton1;

second.java second.java

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   first f = new first();
   f.jTextField1.setText(this.jTextField3.getText()); //not working
   f.jTextField2.setText(this.jTextField4.getText());
   }
   public static javax.swing.JTextField jTextField1;
   public static javax.swing.JTextField jTextField2;
   public static javax.swing.JTextField jTextField3;
   public static javax.swing.JTextField jTextField4;
   private javax.swing.JButton jButton1;

on click of second frames button first frame should get updated. 在单击第二帧按钮时,第一帧应该得到更新。 I don't want open first frame again. 我不想再次打开第一帧。

Before I begin, have a look at The Use of Multiple JFrames, Good/Bad Practice?` 在我开始之前,先看看“使用多个JFrame,良好/不良实践”。

"on click of second frames button first frame should get updated. I don't want open first frame again." “单击第二帧按钮时,第一帧应该会更新。我不想再次打开第一帧。”

It's not working because you're creating a new JFrame ( first ). 它不起作用,因为您正在创建一个新的JFramefirst )。 Of course it's going to open a new frame. 当然它将打开一个新的框架。 Instead of creating a new frame, pass the same one as a reference. 代替创建新框架,而传递相同的框架作为参考。

I'll give you an example using a JDialog instead of a JFrame . 我将为您提供一个使用JDialog而不是JFrame的示例。 If after you read the above link and you still decide a JFrame is what you want, the same code can be implemented into a JFrame . 如果在阅读了以上链接之后仍然确定要使用JFrame ,则可以将相同的代码实现到JFrame They basicially have the same constructs. 它们基本上具有相同的构造。

So basically what I mean by passing a reference is this. 因此,基本上,通过引用是我的意思。 Pass the JFrame to the constructor of the JDialog . JFrame传递给JDialog的构造函数。 This way the JFrame you pass to it, will be the same referenced JFrame . 这样,您传递给它的JFrame将与引用的JFrame相同。

public class MyDialog extends JDialog {
    private First frame;
    private JTextField thisTextField;

    public MyDilaog(final First frame, boolean modal) {
        super(frame, modal);
        this.frame = frame;

        thisTextFiled = new JTextField(20);
    }
}

Int your First class just create it like this 诠释你的First类只是创建它像这样

MyDialog dialog = new MyDialog(First.this, true);

Now you have a reference to the frame. 现在,您可以参考框架。 You also want a getter in the JFrame class that will access the JTextField from the JFrame class. 你也想一getterJFrame将访问类JTextFieldJFrame类。 Then you can set its text, as it also is referenced. 然后,您可以设置它的文本,也可以参考它。 So you can do something like this from the JDialog 因此,您可以从JDialog执行类似的操作

public void actionPerformed(ActionEvent e) {
    String text = thisTextField.getText();

    JTextField fieldFromFrame = frame.getTextField();
    fieldFromFrame.setText(text):
}

Also, you need to use Java naming convention. 另外,您需要使用Java命名约定。 Class names being with capital letter. 类名以大写字母表示。 So First not first 所以First不是first

If you "don't want open first frame again" then you must avoid creating a new instance of first and use a reference of your previous instance instead. 如果“不想再次打开第一帧”,则必须避免创建first的新实例,而应使用先前实例的引用。

So you need a reference... one way to give your second a reference to first is provide it in the constructor, like: 因此,您需要一个参考...一种方法,可以在构造函数中提供第二个参考,例如:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){                                         
  second sec = new second(this);  //notice I'm passing a reference to this as an argument second's contstructor   
  sec.jTextField1.setText(this.jTextField1.getText()); // this works
  sec.jTextField2.setText(this.jTextField2.getText()); 
  sec.setVisible(true);

Then you need to store the reference to first in your second class and use it to access textfields: 然后,您需要将对first的引用存储在第二个类中,并使用它来访问文本字段:

private first f;
public second(first referenceToFirst){
  this.f=referenceToFirst;
}

ok, then use f field instead of instantiating a new first when clicking the button on second... 好的,然后在单击第二个按钮时使用f字段而不是首先实例化一个新的...

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){                                         
  //first f = new first();  //don't instantiate a new first, use the one you got in the constructor
  f.jTextField1.setText(this.jTextField3.getText());
  f.jTextField2.setText(this.jTextField4.getText());
}

Notes: 笔记:

I wouldn't make textfields public static, I'd rather provide getter methods. 我不会使文本字段成为公共静态对象,而是提供getter方法。

I suggest you adhere to naming conventions: use capital letters for classes and use significant names for variables. 我建议您遵守命名约定:对类使用大写字母,对变量使用有效的名称。

尝试将第二个jframe作为第一个jframe中的参数传递,而不是使用它来更改文本字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM