- 最後登錄
- 2024-4-24
- 在線時間
- 319 小時
- 註冊時間
- 2007-12-16
- 閱讀權限
- 70
- 精華
- 0
- UID
- 3034780
- 帖子
- 4539
- 積分
- 17830 點
- 潛水值
- 172133 米
| 若對尊貴或贊助會員有任何疑問,歡迎向我們查詢。我們的即時通或MSN: admin@eyny.com 本帖最後由 missu0524 於 2014-5-25 05:01 PM 編輯
新手練習「圈圈叉叉」使用工具:Eclipse
- import java.awt.Button;
- import java.awt.Frame;
- import java.awt.Label;
- import java.awt.PopupMenu;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- public class Game1 implements WindowListener, ActionListener{
- Frame fr;
- Button[] btn;
- String[] cm = {"0","1","2","3","4","5","6","7","8"};
- boolean b = true;
- int count = 0;
- Game1() {
- loadGUI();
- }
-
- public void loadGUI() {
- fr = new Frame("圈圈叉叉");
- btn = new Button[9];
- fr.setBounds(200,200,340,340);
- for (int i=0; i<btn.length; i++)
- btn[i] = new Button();
- btn[0].setBounds(20,30,100,100);
- btn[1].setBounds(120,30,100,100);
- btn[2].setBounds(220,30,100,100);
- btn[3].setBounds(20,130,100,100);
- btn[4].setBounds(120,130,100,100);
- btn[5].setBounds(220,130,100,100);
- btn[6].setBounds(20,230,100,100);
- btn[7].setBounds(120,230,100,100);
- btn[8].setBounds(220,230,100,100);
- fr.setLayout(null);
- for (int i=0; i<btn.length; i++) {
- btn[i].setActionCommand(cm[i]);
- btn[i].addActionListener(this);
- fr.add(btn[i]);
- }
- fr.setResizable(false);
- fr.addWindowListener(this);
- fr.setVisible(true);
- }
-
- public void reset() {
- for (int i=0; i<btn.length; i++) {
- btn[i].setLabel("");
- count = 0;
- b = true;
- }
- }
-
- //012
- //345
- //678
- public void checkWin(int i, String s) {
- int j = 0,h = 0;
- count += 1;
- for (int k=0; k<3; k++) {
- if (btn[j].getLabel() == btn[j+1].getLabel() && btn[j+1].getLabel() == btn[j+2].getLabel() && btn[j].getLabel() != "") {
- fr.setTitle(btn[j].getLabel() +" 獲勝");
- reset();
- }
- j += 3;
- }
- for (int k=0; k<3; k++) {
- if (btn[h].getLabel() == btn[h+3].getLabel() && btn[h+3].getLabel() == btn[h+6].getLabel() && btn[h].getLabel() != "") {
- fr.setTitle(btn[h].getLabel() +" 獲勝");
- reset();
- }
- h += 1;
- }
- if (btn[0].getLabel() == btn[4].getLabel() && btn[4].getLabel() == btn[8].getLabel() && btn[0].getLabel() != "") {
- fr.setTitle(btn[0].getLabel() +" 獲勝");
- reset();
- } else if (btn[2].getLabel() == btn[4].getLabel() && btn[4].getLabel() == btn[6].getLabel() && btn[2].getLabel() != "") {
- fr.setTitle(btn[2].getLabel() +" 獲勝");
- reset();
- }
- if (count == 9){
- fr.setTitle("平手");
- reset();
- }
- }
-
- /**
- * 圈圈叉叉
- */
- public static void main(String[] args) {
- new Game1();
- }
- public void windowActivated(WindowEvent e) {
- }
- public void windowClosed(WindowEvent e) {
- }
- public void windowClosing(WindowEvent e) {
- fr.dispose();
- }
- public void windowDeactivated(WindowEvent e) {
- }
- public void windowDeiconified(WindowEvent e) {
- }
- public void windowIconified(WindowEvent e) {
- }
- public void windowOpened(WindowEvent e) {
- }
- public void actionPerformed(ActionEvent e) {
- for (int i=0; i<btn.length; i++) {
- if (e.getActionCommand() == cm[i] && btn[i].getLabel() == "") {
- if (b == true) {
- btn[i].setLabel("O");
- b = false;
- checkWin(i,"O");
- } else {
- btn[i].setLabel("X");
- b = true;
- checkWin(i,"X");
- }
- }
- }
- }
- }
複製代碼
... |
附件: 你需要登錄才可以下載或查看附件。沒有帳號?註冊 |