简体   繁体   English

如何从文本字段中获取整数值并将值检索到Java中的另一个类

[英]how to get integer value from textfield and retrieve the value to another class in java

How to get integer value from textfield and retrieve the value to another class in java ?? 如何从文本字段中获取整数值并将值检索到Java中的另一个类?

I know how to get but if i used in another class he get me 0 value,,, 我知道如何获得,但是如果我在另一个班上使用他会给我0值,

"this is in the GUI" x1,x2,y1,y2 predefined 预定义了“这在GUI中” x1,x2,y1,y2

private void X2_accesActionPerformed(java.awt.event.ActionEvent evt){              
     x2=(int)(Double.parseDouble(X2_acces.getText())); 
}

I want to draw line the user inter 2 point (x1,y1) and (x2,y2) after that move the value to class draw line 我想在用户将2点(x1,y1)和(x2,y2)插入后将值移动到类绘制线上

"this is in class drawLine" “这是在drawLine类中”

Next_frame nf=new Next_frame();
Point2D.Double p2=new Point2D.Double(nf.x2,nf.y2);

The following statement is quite ambigous. 以下陈述含糊不清。

x2=(int)(Double.parseDouble(X2_acces.getText())); 

You are parsing the value as Double and casting it down to int . 您正在将值解析为Double并将其强制转换为int This is enough: 这就够了:

x2 = Integer.parseInt(x2_acces.getTest());

Now, when passing this value to another class, you have to make sure, that the method to which you are passing to accept the data type you are about to send. 现在,当将此值传递给另一个类时,必须确保要传递给的方法接受要发送的数据类型。

For example: 例如:

For a class like this: 对于这样的课程:

class DummyClass {
   public DummyClass(int X) {
      //Here note that 'x' is asking for a integer so you have to provide it with integer
   }
}

Then you have use them in the following order: 然后按以下顺序使用它们:

x2 = Integer.parseInt(x2_acces.getTest());
DummyClass c = new DummyClass(x2);

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

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