字符串到int然后更改背景

时间:2013-03-21 12:56:09

标签: java string

我想帮助我的colorchooser。好吧,我要做的就是在3个文本字段中键入rgb color lite 255,255,0。然后我需要将它们转换为int并最后更改背景颜色。现在问题是我点击确定按钮时出现了一些错误。

public void actionPerformed(ActionEvent e) {
    if(e.getSource()==ok){
        int rgb = Integer.parseInt (text1.getText());
        int rgb1 = Integer.parseInt (text2.getText());
        int rgb2 = Integer.parseInt (text3.getText());
        v.setBackground(new Color(rgb, rgb1, rgb2));
}

这会产生:线程中的异常“AWT-EventQueue-0”java.lang.NumberFormatException:对于输入字符串:“”at java.lang.NumberFormatException.forInputString(Unknown Source)

2 个答案:

答案 0 :(得分:3)

您的logcat明确表示您正在尝试将空字符串转换为Integer。

AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

即你正在传递

int color=Integer.parseInt("");  // this cause exception.

确保您的文本字段中包含数字。,为其提供验证。

答案 1 :(得分:0)

尝试调试text1 / text2 / text3文本变量并查看它们保存运行时的内容。如果这没有帮助尝试设置变量,并看到SetBackground按预期工作。