當我單擊button1時,button2將變爲紅色,並且當我第二次單擊button1時,button2將變爲黃色。當按鈕2呈藍色時,點擊按鈕1後,按鈕2將變回紅色。我的代碼不正確,但我希望編寫與此類似的代碼。單擊Button1和Button2將根據Java中的順序更改爲多種顏色
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String[] colorsArray = {"red","yellow","green","blue"};
for(int i = 0; i < 4; i++)
jButton2.setBackground(Color.colorsArray[i]);
}
代碼錯誤:
cannot find symbol
symbol: variable colorsArray
location: class Color
'顏色[] colorsArray = {Color.RED,Color.YELLOW,Color.GREEN, \t \t Color.BLUE};'然後僅'jButton2.setBackground(colorsArray [I]);' –
@ JohnJoe非常感謝你! – Jack