简体   繁体   English

Java-将JLabel传递给Integer

[英]Java - Pass JLabel to Integer

I want to pass the JLabel to Integer, The following code does not work even with Integer.valueOf() and Integer.parse() 我想将JLabel传递给Integer,即使Integer.valueOf()Integer.parse()也无法使用以下代码

This are the following code I've Tried: 这是我尝试过的以下代码:

Test 1: 测试1:

JLabel life = new JLabel("204");
int x = Integer.valueOf(life).intValue();

Test 2: 测试2:

JLabel life = new JLabel("204");
int x = Integer.parseInt(life);

No. You can't magically convert a Label to Integer . 不能。您不能神奇地将Label转换为Integer

However you can get the string of that label and then convert. 但是,您可以获取该标签的字符串,然后进行转换。

JLabel life = new JLabel("204");
int x = Integer.parseInt(life.getText());

Note that, you'll be succeed when there is proper text. 请注意,如果文字正确,您将会成功。 For ex 对于前

"203", "34343" works but not "A2342" "203", "34343"有效,但"A2342"

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

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