我是java新手。我创建一个项目,其中有一个文本字段和一个按钮。我为按钮创建了函数,在其中启动其他函数,就可以了。但我需要从文本字段中获取数值作为我的函数的参数...
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
int price;
int quantity = Integer.parseInt(tf2.getText());
int totalamount = price*quantity;
//need to insert this total amout into textfield tf4 //
tf4.getText(totalamount); //showing error ;
}
});
请帮我,提前谢谢
最佳答案
这很简单...
您可以从文本字段获取整数值,例如
int totalamount = Integer.parseInt(tf2.getText());
getText() 方法用于从文本字段获取值,如果该值是整数,则可以像 Integer.parseInt 一样解析它,如果该值是字符串,则可以使用 toString() 方法获取该值。
你可以像这样设置这个值
tf4.setText(String.valueOf(totalamount));
setText() 方法用于将文本设置到文本字段。
您可以在函数调用中使用该值作为参数来调用函数,例如
myFunction(totalAmount);// function declaration
并在函数定义中使用这个值,例如
public void myFunction(int totalamount)// Function Defination
您必须阅读Basic Java。 Here is link which help you
关于java - 如何从其他文本字段中获取文本字段中的 int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13909614/