当按下具有相同字母/符号的键时,我试图更改按钮的背景颜色,但问题是此代码不适用于符号和空格,我做错了什么,如何解决?
这是代码:
private String names[]={"`"," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 "," 0 "," - "," + "," Backspace "
," Tab "," Q "," W "," E "," R "," T "," Y "," U "," I "," O "," P "," [ "," ] "," \\ "
," Caps Lock "," A "," S "," D "," F "," G "," H "," J "," K "," L "," ; "," ' "," Enter "
," Shift "," Z "," X "," C "," V "," B "," N "," M "," , "," . "," / "," ^ "
," "," < "," ! "," > "};
public void keyPressed(KeyEvent event)
{
int code= event.getKeyCode();
String x;
for(int i=0;i<names.length;i++)
{
s=names[i];
x=s.trim();
if(x.contentEquals(event.getKeyText(code)) )
{
backColor=but[i].getBackground();
but[i].setBackground(Color.RED);
break;
}
}
}
public void keyReleased(KeyEvent event)
{
String x;
int code= event.getKeyCode();
for(int i=0;i<names.length;i++)
{
s=names[i];
x=s.trim();
if(x.contentEquals(event.getKeyText(code)))
{System.out.println("outside");
but[i].setBackground(backColor);
break;
}
}
}
问候