将jTextField值传递给另一个类. [英] Passing a jTextField value to another class.

查看:122
本文介绍了将jTextField值传递给另一个类.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会解释.
当前在框架"FBS"中.单击Button2时,将不再启用它,并且Button3会显示在框架上. jTextField1未启用,方法:GetText();运行.
GetText方法将jTextField1中的文本存储到字符串v中.
单击button3时,将不再启用它,它将打开另一个框架并关闭当前框架.在"DispResults"框架中.我的问题是v在jTextArea中不显示任何内容..我的语法对我来说很好..


I''ll explain.
Currently in frame "FBS". When Button2 is clicked.. it is no longer enabled and Button3 shows up on the frame. jTextField1 is not enabled and method: GetText(); is run.
GetText method stores the text in jTextField1 into string v.
When button3 is clicked, it is no longer enabled, it opens another frame and the current frame closes. In frame "DispResults". My problem is v is not displaying anything in the jTextArea.. My syntax seems to be fine by me..


private void jButton2MouseClicked(java.awt.event.MouseEvent evt)
      {
        jButton2.setEnabled(false);
        jButton3.setVisible(true);
        jTextField1.setEnabled(false);
        GetText();
      }

public void GetText()
{
   v =(jTextField1.getText());
}
 
private void jButton3MouseClicked(java.awt.event.MouseEvent evt)
  {
   jButton3.setEnabled(false);
   DispResults dspr = new DispResults();
   dspr.setVisible(true);
   this.dispose();
  }


(在"DispResults"框架中)


(in frame "DispResults")

FBS fbsobject = new FBS();
jTextArea2.append(fbsobject.v + "\n");



我认为它不起作用,因为jTextField1被设置为声明为私有,但是在将其设置为public之后,它仍然不起作用.



I thought it didn''t work because the jTextField1 was set declared as private, but after I set it to public it still didn''t work.

推荐答案

您到达那里的一个奇怪的GetText.它实际上并没有执行方法名称所隐含的含义,因为那样的话它将是return jTextField1.getText();

您的代码:
What a strange GetText you got there. It doesn''t actually does what the method name implies because then it would be return jTextField1.getText();

Your code:
FBS fbsobject = new FBS();
jTextArea2.append(fbsobject.v + "\n");



在该代码中,您将创建一个新的FSB,而这与您用jTextField1的文本填充v的另一个无关.您仅创建了另一个不用于输入任何文本的文本.一个类始终是一个类,但是您可以使用它创建尽可能多的对象.但是创建的那些对象通常没有任何共享数据.这实际上就是使用对象的力量.因此,即使属性v存在并且具有相同的名称,它实际上也属于您的FSB类的不同实例.

您需要引用输入文本的原始文本.就像这里的示例一样:



In that code you create a new FSB and that has nothing to do with the other where you filled v with the text of jTextField1. You simply created another one that wasn''t used to enter any text. A class is always one but you can create as many objects you want with it. But those objects created do not normally have any shared data. That''s actually the power of using objects. So even though property v exists and has the same name, it is actually belongs to a different instance of your FSB class.

You would need a reference to the original one where the text was entered. Like in the example here:

private void jButton3MouseClicked(java.awt.event.MouseEvent evt)
  {
   jButton3.setEnabled(false);
   DispResults dspr = new DispResults();
   dspr.fsbobject = this;
   dspr.setVisible(true);
   this.dispose();
  }



这不是最整洁的代码,但是对于您的问题,这是可行的.

最好在DispResults的属性中设置文本,而不是传递完整的框架,例如:



This isn''t the most neat and clean code, but for your question this works.

It would be better to set the text in a property of DispResults instead of passing the complete frame, like:

dspr.v_text = this.v;



祝你好运!



Good luck!


这篇关于将jTextField值传递给另一个类.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆