java 如何从其他文本字段中获取文本字段中的 int 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13909614/
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
How to take int value in text field from other textfield
提问by Ank-It
I'm new to java. I create project where I have one textField and one button. I make function for button, where I start my other function and it's ok. But I need to take number value from textField as parameter for my function...
我是 Java 新手。我创建了一个项目,其中有一个 textField 和一个按钮。我为按钮创建功能,在那里我开始我的其他功能,没关系。但是我需要将 textField 中的数值作为我的函数的参数...
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 ;
}
});
help me in please ,thank you in advance
请帮助我,提前谢谢
采纳答案by Sandip Armal Patil
This is simple...
You can get integer value from textfield like
这很简单......
您可以从文本字段中获取整数值,例如
int totalamount = Integer.parseInt(tf2.getText());
int totalamount = Integer.parseInt(tf2.getText());
getText() method are use to get value from textfield and If this value is integer the you can parse it like Integer.parseInt, If this value is string then you can get this value using toString() method.
getText() 方法用于从文本字段中获取值,如果该值是整数,则可以像 Integer.parseInt 一样解析它,如果该值是字符串,则可以使用 toString() 方法获取该值。
and you can set this value like
你可以像这样设置这个值
tf4.setText(String.valueOf(totalamount));
setText() method are use to set text to Textfield.
setText() 方法用于将文本设置为 Textfield。
You can use this value in function call as a parameter to call function like
您可以在函数调用中使用此值作为参数来调用函数,例如
myFunction(totalAmount);// function declaration
And use this value in Function Definition like
并在函数定义中使用此值,例如
public void myFunction(int totalamount)// Function Defination
You have to read Basic Java. Here is link which help you
您必须阅读 Basic Java。这是可以帮助您的链接
回答by Freak
just replace your this line
只需更换您的这一行
tf4.getText(totalamount);
by this one
通过这个
tf4.setText(Integer.toString(totalamount));
ORtf4.setText(totalamount);
because TextField
have overloded methods for setting text for Strings
and int
.
或者tf4.setText(totalamount);
因为TextField
已经覆盖了为Strings
和设置文本的方法int
。
Please keep in mind that You never pass a value(by convention of Java) from a getter method.Only parameters can be passed from setter methods (if we are considering them in Beans sense or any other as well).Please follow some basics of Java hereand here
请记住,你永远不会从 getter 方法传递值(按照 Java 的约定)。只有参数可以从 setter 方法传递(如果我们在 Beans 或任何其他意义上考虑它们)。请遵循一些基础知识Java在这里和这里
回答by Xiaoerge
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//put the try catch here so if user didnt put an integer we can do something else
try
{
int price;
int quantity = Integer.parseInt(tf2.getText());
int totalamount = price*quantity;
//need to insert this total amout into textfield tf4 //
tf4.setText(totalamount);
}
catch(NumberFormatException ex)
{
//do something like telling the user the input is not a number
}
}});
回答by Michael 'Maik' Ardan
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.setText(totalamount); //showing error ;
}
});