0

我正在尝试使用 jTextField 和 setText 方法设置一个 int 值。但当然 setText 想要一个字符串。我该如何解决这个问题?我给你一段代码:

    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());

seatTF 是一个 jTextField,getNoOfSeats 是另一个类中的一个方法,它返回一个 int 值。

再次感谢您回答这个问题。现在我将如何获取 int 的值来做某事?

        public void buyTicket() {
    String newFilmName = filmNameTF.getText();
    String newLecture = lectureTF.getText();
    String newAge = ageTF.getText();
    String newPrice = priceTF.getText(); 
    int newSeats = seatsTF.

正如您所看到的代码,我可以使用 getText 轻松获取 String 值。然后我可以将它们打印出来或与它们一起打印。我怎么能用座位 int 做到这一点?再次感谢。

4

7 回答 7

6

String#valueOf将您的转换intString.

String.valueOf(e.getAge());将返回参数的string表示int

seatsTF.setText(String.valueOf(e.Age()));
...
于 2013-03-20T17:18:49.580 回答
1

利用

seatsTF.setText(""+e.getNoOfSeats());

或者

seatsTF.setText(String.valueOf(e.getNoOfSeats()));
于 2013-03-20T17:19:17.733 回答
0

假设年龄字段是 int 类型,您可以尝试以下操作:

        ageTF.setText( Integer.toString(e.getAge()) );
于 2013-03-20T17:25:37.387 回答
0

int将其设置String为没什么大不了的。显示值是个问题。要注意如何在文本字段中正确显示值,您可以使用 aDecimalFormat来格式化数值。但可能是数字是locale特定的那么你需要NumberFormat实例

NumberFormat nf = NumberFormat.getInstance(locale);
nf.setMaximumIntegerDigits(12);
nf.setMaximumFractionDigits(0);
nf.setMinimumFractionDigits(0);
String s = nf.format(e.getNoOfSeats());
seatsTF.setText(s);

您可能还需要阅读有关如何使用DecimalFormat.

于 2013-03-20T17:40:59.027 回答
0

正常的方法是

seatsTF.setText(Integer.toString(e.getNoOfSeats()));

或者

seatsTF.setText(String.valueOf(e.getNoOfSeats()));

但是,这可以通过这样的连接来实现:

seatsTF.setText("" + e.getNoOfSeats());
于 2013-03-20T17:21:14.270 回答
0

To convert Integer Value to String you should

MedicineTM medicine=tblmedicine.getSelectionModel().getSelectedItem();

    txtmedicine.setText(medicine.getMID());
    txtDescription.setText(medicine.getDescription());
    txtQty.setText(String.valueOf(medicine.getQty()));  // this is what i did
    cmbApproval.setValue(medicine.getApproval());
于 2018-06-27T05:36:50.157 回答
-1

我认为您应该将代码编写为

座位TF.setText(e.getNoOfSeats().toString());

于 2013-03-20T17:22:00.257 回答