Re: [問題] 關於Windowing

看板java作者 (null)時間16年前 (2010/02/20 16:28), 編輯推噓2(200)
留言2則, 1人參與, 最新討論串3/5 (看更多)
※ 引述《BlackMatrix (BlackMatrix)》之銘言: : 最近教授有教怎麼做出簡單&上面有按鈕的程序 : 那...教授叫我們做出一個程序 : 有八個按鈕, 然後一開始程序就會設定每種按鈕的顏色, 然後點擊按鈕的時候會變換 : 那個被點擊的按鈕的框框會自動改變隨機的顏色. : /////////想像//////// START : 以下是想像的程序碼 : int x = 0; : if x = 1 那就是我第一個按鈕 : 就是設定一個Counter, 像我的程序碼一樣, 然後每一個數字代表一個JButton : 就是如果counter ++, 那麼counter在2的時候就是會自動的等於我的JButton : 再利用X.setBackground(XXXX顏色), 可是因為我知道教授給我的數目是八個 : 所以我就利用JButton one, two, three four到eight : 萬一教授沒有指定框框數目可是又要求我們把每一個框框隨機不同顏色,點擊不同 : 的顏色怎麼辦? : 簡單來說, 要怎麼設定N個框框, 隨機顏色很簡單, 可是還有另一個重點就是 : ActiveListener的GetSource要怎麼把他設定成只要是我剛剛設定的N個框框要是被點擊 : 就會自動改變顏色? : /////////想像//////// END : 可是問題是我不會讓X = 1 等於一個JButton : 所以我就寫出以下的程序, 可以完成老師要求的事, 可是我不知道有什麼更簡單的方法 : 可以做出相同的事情 : Frame: : http://nopaste.csie.org/211d0 : Main: : http://nopaste.csie.org/50a20 ====================================================================== if (e.getSource() == oneOneButton) { Random randomGeneratorThree = new Random(); int randomInt = randomGeneratorThree.nextInt(13); if (randomInt == 0) { oneOneButton.setBackground(Color.black); } else if (randomInt == 1) { ======================================================================== 何必在乎 e.getSource() 是那一個 reference 呢? 重點應該是確保它是一個 JButton, 而只要是 JButton 就會有 .setBackground() 方法可以用。 Swing 並不會你按的是 oneOneButton 卻把 oneTwoButton 作為 event source 傳遞給你。所以應該改成: if(e.getSource() instanceof JButton){ JButton btn = (JButton) e.getSource(); btn.setBackground(..........................); } ========================================================================= if (randomInt == 0) { oneOneButton.setBackground(Color.black); } else if (randomInt == 1) { oneOneButton.setBackground(Color.blue); } else if (randomInt == 2) { oneOneButton.setBackground(Color.cyan); } else if (randomInt == 3) { oneOneButton.setBackground(Color.darkGray); } else if (randomInt == 4) { oneOneButton.setBackground(Color.gray); } else if (randomInt == 5) { oneOneButton.setBackground(Color.green); } else if (randomInt == 6) { oneOneButton.setBackground(Color.lightGray); } else if (randomInt == 7) { oneOneButton.setBackground(Color.magenta); } else if (randomInt == 8) { oneOneButton.setBackground(Color.orange); } else if (randomInt == 9) { oneOneButton.setBackground(Color.pink); } else if (randomInt == 10) { oneOneButton.setBackground(Color.red); } else if (randomInt == 11) { oneOneButton.setBackground(Color.white); } else if (randomInt == 12) { oneOneButton.setBackground(Color.yellow); } } =========================================================================== 這很明顯是簡單地數字與 Color 物件的對應關係, 12 => yellow 11 => white 10 => red ....... 剛好能簡單化為陣列: Color[] COLORS = new Color[] { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW }; 那麼一切就變得很容易了: btn.setBackground(COLORS[randomGenerator.nextInt(13)]); PS. 不要執著於 reference,應針對 reference 代表的物件/介面去操作。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.168.161 ※ 編輯: qrtt1 來自: 140.112.168.161 (02/20 16:32) ※ 編輯: qrtt1 來自: 140.112.168.161 (02/20 17:07)

02/20 17:17, , 1F
謝謝qrtt1的提示, 我正在努力修改我的Code
02/20 17:17, 1F

02/20 17:52, , 2F
謝謝qrtt1的提示, 我已經把Code做完了, 感謝
02/20 17:52, 2F
文章代碼(AID): #1BVvr1za (java)
文章代碼(AID): #1BVvr1za (java)