Java使用setText具有int值 [英] Java have a int value using setText
问题描述
我正在尝试使用jTextField和setText方法设置一个int值.但是当然setText需要一个String.我如何解决这个问题?我将向您提供一段代码:
I'm trying to set an int value using jTextField and the setText method. But of course setText wants a String. How do I get round this? I'll give you a snippet of the code:
private void setAllTextFields(FilmSystem e){
getFilmNameTF().setText(e.getFilmName());
lectureTF.setText(e.getLecture());
ageTF.setText(e.getAge());
priceTF.setText(e.getTicketCost());
seatsTF.setText(e.getNoOfSeats());
seatsTF是一个jTextField,而getNoOfSeats是另一个类中的一个返回int值的方法.
seatsTF is a jTextField and getNoOfSeats is a method in another class that returns a int value.
再次感谢您回答这个问题.现在,我将如何获取int的值来做某事?
Thanks again for answering this question. Now how would I go about getting the value of the int to do something to do?
public void buyTicket() {
String newFilmName = filmNameTF.getText();
String newLecture = lectureTF.getText();
String newAge = ageTF.getText();
String newPrice = priceTF.getText();
int newSeats = seatsTF.
正如您所看到的代码,使用getText可以很容易地获得String值.然后,我可以将它们打印出来或与它们一起打印.我如何在seats int上做到这一点?再次感谢.
As you can see the code, the String values I can get easy with getText. I can then print them out or whatever with them. How can I do this with the seats int? Thanks again.
推荐答案
String#valueOf
将您的int
转换为String
.
String.valueOf(e.getAge());
将返回int
参数的string
表示形式.
String.valueOf(e.getAge());
will return the string
representation of the int
argument.
seatsTF.setText(String.valueOf(e.Age()));
...
这篇关于Java使用setText具有int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!