java 板


LINE

我的程式是一個可以計時的記憶翻牌遊戲 在程式一些方法中建立一個新的 timer 並呼叫相應的 timertask 我的 timertask 以 inner class 寫在相同的 class 裡面 奇怪的是那方法就只有 timer & timertask 沒有執行,其他都順利執行 我懷疑是我用了 2個 timertask 做不同事情而沒有另外新增執行緒 以下為部分程式碼,請各位大大看看哪邊出問題,若資訊太少會在補充 謝謝 private JFrame mainFrame; ... private List<JButton> Cards = new ArrayList<JButton>(); private List<Integer> numberlist = new ArrayList<Integer>(); private int firstOpenCard = -1; // 記錄第一次翻開的牌 private int openCards = -1; // 記錄翻開幾組 private Timer timer, timer2; // timer2 用來設定翻錯的牌多久蓋回,沒有順利執行 private int i1; public MemoryGame(String title) { playerTable = new JPanel(); playerArea = new JPanel(); playerInformation = new JPanel(); ... mainFrame = new JFrame(title); playerInformation.add(clicks); playerInformation.add(time); clickDetermine clickCards = new clickDetermine(); //替每個按鈕加上事件 startBtn = new JButton("start"); startBtn.addActionListener( new ActionListener () { public void actionPerformed(ActionEvent arg0) { if ( (openCards != CardNumber/2) && (openCards != -1) ) { timer.cancel(); int option = JOptionPane.showConfirmDialog(null, "Do you want to restart the game ?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION); if (option == 1 || option == 2) { timer = new Timer(); timer.schedule(new timeDisplay(), 0, 1000); return; } } ... timer = new Timer(); timer.schedule(new timeDisplay(), 0, 1000); } }); giveUpBtn = new JButton("give up"); giveUpBtn.addActionListener(...); ... private class clickDetermine implements ActionListener { public void actionPerformed(ActionEvent e) { useSteps++; for (int i = 0 ; i < CardNumber ; i++) { if (e.getSource() == Cards.get(i)) { // 翻的是第一張, 記錄位置 if (firstOpenCard == -1) { Cards.get(i).setText(numberlist.get(i) + ""); firstOpenCard = i; clicks.setText("目前翻動次數 : " + useSteps); } else { if (i == firstOpenCard) { // 翻同一張 useSteps--; return; } Cards.get(i).setText(String.valueOf(numberlist.get(i))); clicks.setText("目前翻動次數 : " + useSteps); // 翻對 if (numberlist.get(i).equals(numberlist.get(firstOpenCard))) { Cards.get(i).setEnabled(false); Cards.get(firstOpenCard).setEnabled(false); openCards++; if (openCards == CardNumber/2) { // 翻完,停止計時 timer.cancel(); } } else { // 翻錯 i1 = i; timer2 = new Timer(); timer2.schedule(new coverback(), 1500); timer2.cancel(); } firstOpenCard = -1; } break; } } } } ... private class timeDisplay extends TimerTask { // 設定時間顯示 public void run() { ... } } private class coverback extends TimerTask { // 設定將牌蓋回 public void run() { Cards.get(firstOpenCard).setText("*"); Cards.get(i1).setText("*"); } } ... -- 肝不好 肝若好 人生是黑白的 考卷是空白的 、 ﹐ ● ●b ▎ ●> ● ▌ ﹍﹍ 囧> 幹... ▲ ■┘ ▎ ■ ▋ ︶■ 〈﹀ ∥ ▁▁∥ ▎ ﹀〉▊ 〈\ ψcockroach727 --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.232.118
※ 文章網址: https://webptt.com/m.aspx?n=bbs/java/M.1449844111.A.A57.html
1F:推 AI3767: 翻錯 那邊, 一schedule完就cancel不會有問題嗎? 12/11 23:11
中間若要插入 sleep ,數值設定太小也不會執行 設大一些要凍結很久,而且也不會有翻開又蓋回的感覺 ※ 編輯: obelisk0114 (140.112.232.118), 12/12/2015 00:34:35
2F:→ AI3767: 不清楚你的timer2還有做什麼其它的, 如果不cancel可以嗎? 12/12 00:44
3F:→ AI3767: 另外是,這裡沒看到clickCards變數有被誰用 12/12 00:46
timer2 是設定蓋回去的時間,只有將2張翻開的牌蓋回去 我有試過將 timer2.cancel() 刪掉,執行會報錯誤 clickCards 是設定 16 張牌的 ActionPerformed, 相關程式碼附上如下 for (int i = 0 ; i < CardNumber ; i++) { // CardNumber 是總共有幾張牌 numberlist.add(new Integer(i/2 + 1)); Cards.add(new JButton("*")); playerArea.add(Cards.get(i)); Cards.get(i).addActionListener(clickCards); } ※ 編輯: obelisk0114 (140.112.232.118), 12/12/2015 01:56:24
4F:→ AI3767: 呃 是報什麼錯?? 12/12 16:37
原本覺得他的報錯很奇怪 後來 print 出來發現翻錯那邊 在跑到 timer2 = new Timer(); 會因為後面的 firstOpenCard = -1; 而將 firstOpenCard 設定成 -1,這樣 coverback 的 Cards.get(firstOpenCard).setText("*"); 就會因為抓到 Cards.get(-1) 而出錯 不知道為何 ? ※ 編輯: obelisk0114 (140.112.232.118), 12/12/2015 18:30:13
5F:→ jinn: timer2.cancel()刪掉 然後coverback那裡改成 12/12 21:10
6F:→ jinn: if(firstOpenCard!=-1) { 12/12 21:11
7F:→ jinn: Cards.get(firstOpenCard).setText("*"); } 12/12 21:12
8F:→ jinn: 不就好了@@? 12/12 21:13
Cards.get(firstOpenCard).setText("*"); 是翻錯時,把第一張翻開的牌蓋回去 若移到上面會變成翻錯時,先將第一張蓋回,1.5秒後蓋回第二張 感覺很奇怪 ※ 編輯: obelisk0114 (140.112.232.118), 12/12/2015 21:19:58
9F:推 AI3767: 這個報錯和timer無關, 是程式邏輯要修, 你的寫法,遇到連續 12/12 22:11
10F:→ AI3767: 開牌, 可能也會有問題 12/12 22:11
程式邏輯要修可以多提一下嗎 ? 資訊太少,不知道怎麼改 timer 和 timertask 的搭配好像是另一個執行緒 是因為主要程式執行完才會交給 timer2 和 coverback ? 雖然只要多定義一個變數,將 firstOpenCard 傳入,再由 coverback 接收就好 ※ 編輯: obelisk0114 (140.112.232.118), 12/13/2015 02:57:46
11F:→ AI3767: 上面這句, 這樣做是好的, 在new coverback時傳入誰要翻回 12/13 23:46
12F:→ AI3767: timer new一次就夠了. 然後連續翻牌問題, 可以有兩種方法 12/13 23:47
13F:→ AI3767: 1:最多翻兩張, 兩張有中,或是兩張都翻回後, 才能翻下一輪 12/13 23:48
14F:→ AI3767: 2:可連續翻牌,但自己定出方法和資料結構, 能記得哪兩張要 12/13 23:50
15F:→ AI3767: 互相配對比較 12/13 23:50
只用滑鼠點擊好像不會達成連續翻牌 ? 記得哪2張的方式像是下面嗎 ? 在 public class MemoryGame{ private int i1,i2; // 在 clickDetermine 才傳入第一張,第二張 } ※ 編輯: obelisk0114 (140.112.232.118), 12/14/2015 02:27:47
16F:→ AI3767: 也可以,原先的firstOpenCard也可以,只要能確保一次最多只 12/14 22:11
17F:→ AI3767: 會翻開兩張. 然後程式執行的部份再檢查看看吧 12/14 22:15







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP