A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李德国 黑马帝   /  2011-7-25 14:57  /  1828 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

数独游戏经过N天闭门造车的努力,今天终于初步成型了,经过初步测试,基本达到要求,但是肯定还有许多错误之处,
现在把代码贴上来,希望各位马友斧正!
1、产生数独数组的类:[code=java]import java.util.Random;

public class DoShudu {

       
        public static void main(String args[]){
                DoShudu shudu=new DoShudu();
                int[][] cells=shudu.getShudu();
                shudu.printShuDu(cells);
               
        }
       
       
        public  void  printShuDu(int [][] cells) {//打印出打乱顺序后的数独数组
       
                for(int k=0;k<9;k++){
                        for(int i=0;i<9;i++){
                        System.out.print(cells[k]);
                        }
                        System.out.println();
                }
        }
       
       
        public int[][] getShudu(){//获得打乱顺序后的数独数组的方法
               
                int[][]  cells=new int[][]{//建立有顺序的数组
                               
                                {1,2,3,4,5,6,7,8,9},
                                {4,5,6,7,8,9,1,2,3},
                                {7,8,9,1,2,3,4,5,6},
                                {2,3,1,5,6,4,8,9,7},
                                {5,6,4,8,9,7,2,3,1},
                                {8,9,7,2,3,1,5,6,4},
                                {3,1,2,6,4,5,9,7,8},
                                {6,4,5,9,7,8,3,1,2},
                                {9,7,8,3,1,2,6,4,5}
                               
                };
               
               
               
                int countH=new Random().nextInt(10);
                for(int k=0;k<countH;k++){//对数独数组行与列进行随机次数的交换
                        cells=lineTolie(cells);
               
                }
               
                int count=0;
                for(int k=0;k<12;k++){//对数独数组行进行随机次数的交换
                        count=new Random().nextInt(9);
                        cells=changeLine(cells,count);
               
                }
               
                int countH2=new Random().nextInt(10);
                for(int k=0;k<countH2;k++){//对数独数组行与列进行第二次随机次数的交换
                        cells=lineTolie(cells);
               
                }
                return cells;
        }
       
        public  int [][] changeLine(int[][] cells,int m){//对数组进行行与行交换
                int n=m;
                int [] temp=new int[9];
                n=((m+3)>=9)?(m+3-9):m+3;
                        for(int j=0;j<9;j++){
                                temp[j]=cells[m][j];
                                cells[m][j]=cells[n][j];
                                cells[n][j]=temp[j];
                       
                }
                return cells;
               
        }
       

        public  int[][] lineTolie(int[][] cells){//对数组进行行与列交换
               
                int temp=0;
                for(int j=0;j<9;j++){
                                for(int k=j+1;k<9;k++){
                                        temp=cells[k][j];
                                        cells[k][j]=cells[j][k];
                                        cells[j][k]=temp;

                                }
                }
                return cells;
               
               
        }

}[/code]2、运行类[code=java]import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

import shudu.DoShudu;

public class Sudoku extends JFrame {

        final private TextField[][] txtGame;

        public static void main(String[] args) {
                Sudoku shudu = new Sudoku();

        }

        public Sudoku() {//对JFrame进行布局初始以及监听设置
                txtGame = new TextField[9][9];//建立81个TextField对象
                DoShudu shudu = new DoShudu();
                int[][] cells = shudu.getShudu();//获取数独数组
                final JPanel jpl = new JPanel();//建立JPanel对象
                final int spaceNum = 50;//spaceNum表示需要设置空白TextField的数量
                jpl.setLayout(new GridLayout(9, 9));//JPanel布局
                final int[][] cellAn = new int[9][9];//数独数组的答案
                System.arraycopy(cells, 0, cellAn, 0, cells.length);//答案从建立的数独数组中Copy
                for (int i = 0; i < 9; i++) {//把答案从Console打印出来

                        for (int j = 0; j < 9; j++) {
                                System.out.print(cellAn[j]);
                        }
                        System.out.println();
                }                                                //打印结束
                this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
                this.setSize(600, 600);
                this.setResizable(false);
                this.setTitle("黑马-李德国-数独游戏");
               

                for (int i = 0; i < 9; i++) {

                        for (int j = 0; j < 9; j++) {

                                txtGame[j] = new TextField();
                               
                                //设置TextField背景颜色

                                if ((i < 3 && j < 3) || (i < 6 && i >= 3 && j >= 3 && j < 6)
                                                || (i < 9 && i >= 6 && j >= 6 && j < 9)) {
                                        txtGame[j].setBackground(Color.ORANGE);
                                }
                                if ((i < 6 && i >= 3 && j < 3) || (i < 3 && j >= 6 && j < 9)
                                                || (i < 9 && i >= 6 && j >= 3 && j < 6)) {
                                        txtGame[j].setBackground(Color.GREEN);
                                }

                                if ((i < 9 && i >= 6 && j < 3) || (i < 3 && j >= 3 && j < 6)
                                                || (i < 6 && i >= 3 && j < 9 && j >= 6)) {
                                        txtGame[j].setBackground(Color.PINK);
                                }

                                txtGame[j].setFont(new Font("Dialog", Font.CENTER_BASELINE,
                                                60));// 设置字体大小
                                txtGame[j].setText(Integer.toString(cells[j]));
                                txtGame[j].setEnabled(false);
                                txtGame[j].setVisible(true);
                                jpl.add(txtGame[j]);
                                jpl.setVisible(true);
                        }

                }

                final int[][] tempArray = new int[spaceNum][2];
                for (int i = 0; i < spaceNum; i++) {//依据需要空白的TextField数量,随机对TextField设置为空

                        final int ranD1 = new Random().nextInt(9);
                        final int ranD2 = new Random().nextInt(9);
                        tempArray[0] = ranD1;
                        tempArray[1] = ranD2;
                        txtGame[ranD1][ranD2].setText("");
                        txtGame[ranD1][ranD2].setBackground(Color.WHITE);

                        txtGame[ranD1][ranD2].addTextListener(new TextListener() {//对空白的TextField添加监听,数值发生变化后进行答案对比,如果全部答对在Console打印“good”
                                @Override
                                public void textValueChanged(TextEvent e) {
                                        TextField tmp = (TextField) e.getSource();
                                        int count = 0;
                                        for (int u = 0; u < spaceNum; u++) {
                                                if ((txtGame[tempArray[0]][tempArray[1]]
                                                                .getText())
                                                                .equals(Integer
                                                                                .toString(cellAn[tempArray[0]][tempArray[1]]))) {
                                                        count++;
                                                }
                                        }
                                        if (count == spaceNum) {

                                                System.out.println("good");
                                        }
                                }
                        });
                        txtGame[ranD1][ranD2].setEnabled(true);
                }
                this.add(jpl);
                this.setVisible(true);
        }
}[/code]

2 个回复

正序浏览
黑马网友  发表于 2011-7-29 10:47:59
藤椅
多谢马友提出的宝贵意见,我重新梳理了程序,经过修改,完善了多处Bug。不过数独产生类随机性还不够好,留给马友们修改吧[code]package shudu;

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CopyOfSudoku extends JFrame {

        private TextField[][] txtGame = new TextField[9][9];;

        static int num = 40;// 空白格数量
        static int guan = 3;// 关卡数量,暂时没有用上
        static int add = 10;// 没关过后增加的空白格数量
        JPanel jpl = new JPanel(new GridLayout(9, 9));// 建立JPanel对象
        final int[][] cellAn = new int[9][9];
        static JFrame jfm = new JFrame("选择数字");;
        final static JPanel jpnl = new JPanel();
        static DoShudu shudu = new DoShudu();
        static int[][] cells = shudu.getShudu();// 获取数独数组
        static TextField tempTextField = new TextField();

        public static void main(String[] args) {
                CopyOfSudoku shudu = new CopyOfSudoku();
        }

        public void getAnswer() {//获取答案方法

                System.arraycopy(cells, 0, cellAn, 0, cells.length);// 答案从建立的数独数组中Copy
                for (int i = 0; i < 9; i++) {// 把答案从Console打印出来

                        for (int j = 0; j < 9; j++) {
                                System.out.print(cellAn[i][j]);
                        }
                        System.out.println();
                } // 打印结束
        }

        public void addListener() {// 设置空白TextField 并添加监听
                final int[][] tempArray = new int[num][2];
                for (int i = 0; i < num; i++) {// 依据需要空白的TextField数量,随机对TextField设置为空

                        final int ranD1 = new Random().nextInt(9);
                        final int ranD2 = new Random().nextInt(9);
                        tempArray[i][0] = ranD1;
                        tempArray[i][1] = ranD2;
                        txtGame[ranD1][ranD2].setText("");
                        txtGame[ranD1][ranD2].setForeground(Color.BLUE);
                        if ((ranD1 < 3 && ranD2 < 3)
                                        || (ranD1 < 6 && ranD1 >= 3 && ranD2 >= 3 && ranD2 < 6)
                                        || (ranD1 < 9 && ranD1  >= 6 && ranD2 >= 6 && ranD2 < 9)) {
                                txtGame[ranD1][ranD2].setBackground(Color.ORANGE);

                        }
                        if ((ranD1 < 6 && ranD1 >= 3 && ranD2 < 3)
                                        || (ranD1 < 3 && ranD2 >= 6 && ranD2 < 9)
                                        || (ranD1 < 9 && ranD1 >= 6 && ranD2 >= 3 && ranD2 < 6)) {
                                txtGame[ranD1][ranD2].setBackground(Color.GREEN);
                        }

                        if ((ranD1 < 9 && ranD1 >= 6 && ranD2 < 3)
                                        || (ranD1 < 3 && ranD2 >= 3 && ranD2 < 6)
                                        || (ranD1 < 6 && ranD1 >= 3 && ranD2 < 9 && ranD2 >= 6)) {
                                txtGame[ranD1][ranD2].setBackground(Color.PINK);
                        }

                        txtGame[ranD1][ranD2].addMouseListener(new MouseAdapter() {

                                public void mouseClicked(MouseEvent e) {

                                        tempTextField = (TextField) e.getSource();
                       
                                        tempTextField.setText("");
                       
                                }
                        });
                       
                        txtGame[ranD1][ranD2].addFocusListener(new FocusListener(){//TextField得到焦点背景变白,失去焦点变回原背景

                                @Override
                                public void focusGained(FocusEvent e) {
                                        // TODO Auto-generated method stub
                                        ((TextField) e.getSource()).setBackground(Color.WHITE);
                                }

                                @Override
                                public void focusLost(FocusEvent e) {
                                        // TODO Auto-generated method stub
                                        if ((ranD1 < 3 && ranD2 < 3)
                                                        || (ranD1 < 6 && ranD1 >= 3 && ranD2 >= 3 && ranD2 < 6)
                                                        || (ranD1 < 9 && ranD1  >= 6 && ranD2 >= 6 && ranD2 < 9)) {
                                                txtGame[ranD1][ranD2].setBackground(Color.ORANGE);

                                        }
                                        if ((ranD1 < 6 && ranD1 >= 3 && ranD2 < 3)
                                                        || (ranD1 < 3 && ranD2 >= 6 && ranD2 < 9)
                                                        || (ranD1 < 9 && ranD1 >= 6 && ranD2 >= 3 && ranD2 < 6)) {
                                                txtGame[ranD1][ranD2].setBackground(Color.GREEN);
                                        }

                                        if ((ranD1 < 9 && ranD1 >= 6 && ranD2 < 3)
                                                        || (ranD1 < 3 && ranD2 >= 3 && ranD2 < 6)
                                                        || (ranD1 < 6 && ranD1 >= 3 && ranD2 < 9 && ranD2 >= 6)) {
                                                txtGame[ranD1][ranD2].setBackground(Color.PINK);
                                        }

                                }
                               
                               
                               
                               
                        });
                       
                       

                        txtGame[ranD1][ranD2].addTextListener(new TextListener() {// 对空白的TextField添加监听,数值发生变化后进行答案对比,如果全部答对在Console打印“good”
                                                @Override
                                                public void textValueChanged(TextEvent e) {
                                                       
                                                       
                                               
                                                        int count = 0;
                                                        for (int u = 0; u < num; u++) {
                                                                if ((txtGame[tempArray[u][0]][tempArray[u][1]]
                                                                                .getText())
                                                                                .equals(Integer
                                                                                                .toString(cellAn[tempArray[u][0]][tempArray[u][1]]))) {
                                                                        count++;
                                                                }
                                                        }
                                                        if (count == num) {

                                                                jpl.removeAll();
                                                                FlowLayout blt = new FlowLayout();
                                                                jpl.setLayout(blt);

                                                                if (num <= 50) {

                                                                        jpl.add(new JLabel("恭喜你过关"));
                                                                        Button btn = new Button("进入下一关");

                                                                        btn.addMouseListener(new MouseAdapter() {

                                                                                @Override
                                                                                public void mouseClicked(MouseEvent e) {
                                                                                        // TODO Auto-generated method stub
                                                                                        num = num + add;
                                                                                        CopyOfSudoku.this.setLayout(null);
                                                                                        jpl.removeAll();
                                                                                        jpl.setLayout(new GridLayout(9, 9));
                                                                                        jpnl.removeAll();
                                                                                        printFrame();
                                                                                        addListener();
                                                                                }
                                                                        });

                                                                        jpl.add(btn);
                                                                } else {
                                                                        jpl.add(new JLabel("恭喜 你已经完成所有关卡!"));
                                                                        jpnl.removeAll();
                                                                }
                                                                jpl.updateUI();
                                                                jpnl.updateUI();
                                                                System.out.println("ok  mission complete!");
                                                        }
                                                }
                                        });
                        txtGame[ranD1][ranD2].setEnabled(true);

                }

        }

        public void printFrame() {//初始化JFrame

                getAnswer();
                jpl.setBounds(0, 0, 600, 500);
                for (int i = 0; i < 9; i++) {

                        for (int j = 0; j < 9; j++) {

                                txtGame[i][j] = new TextField();

                                // 设置TextField背景颜色

                                if ((i < 3 && j < 3) || (i < 6 && i >= 3 && j >= 3 && j < 6)
                                                || (i < 9 && i >= 6 && j >= 6 && j < 9)) {
                                        txtGame[i][j].setBackground(Color.ORANGE);

                                }
                                if ((i < 6 && i >= 3 && j < 3) || (i < 3 && j >= 6 && j < 9)
                                                || (i < 9 && i >= 6 && j >= 3 && j < 6)) {
                                        txtGame[i][j].setBackground(Color.GREEN);
                                }

                                if ((i < 9 && i >= 6 && j < 3) || (i < 3 && j >= 3 && j < 6)
                                                || (i < 6 && i >= 3 && j < 9 && j >= 6)) {
                                        txtGame[i][j].setBackground(Color.PINK);
                                }

                                txtGame[i][j].setFont(new Font("Dialog", Font.CENTER_BASELINE,60));// 设置字体大小
                                txtGame[i][j].setText(Integer.toString(cells[i][j]));
                                txtGame[i][j].setEnabled(false);
                                txtGame[i][j].setVisible(true);
                                jpl.add(txtGame[i][j]);
                       
                        }

                }
                this.add(jpl);
                jpnl.setBounds(0, 500, 600, 100);
                for (int f = 0; f < 9; f++) {

                        final Button btn = new Button((f + 1) + "");
                        btn.setForeground(Color.WHITE);
                        btn.setBackground(Color.RED);
                       
                        btn.setFont(new Font("Dialog", Font.CENTER_BASELINE, 30));

                        btn.addMouseListener(new MouseAdapter() {

                                @Override
                                public void mouseClicked(MouseEvent e) {
                                        // TODO Auto-generated method stub
                                        tempTextField.setText(btn.getLabel()+ tempTextField.getText());

                                }
                        });

                        jpnl.add(btn);
                }
                Button btnDel = new Button(" 清 空 ");
                btnDel.setForeground(Color.WHITE);
                btnDel.setBackground(Color.RED);
                btnDel.setFont(new Font("Dialog", Font.CENTER_BASELINE, 30));

                btnDel.addMouseListener(new MouseAdapter() {

                        @Override
                        public void mouseClicked(MouseEvent e) {
                                // TODO Auto-generated method stub
                                tempTextField.setText("");

                        }
                });

                jpnl.add(btnDel);
                this.add(jpnl);
                this.setVisible(true);
        }

        public CopyOfSudoku() {// 对JFrame进行布局初始以及监听设置

                getAnswer();
                this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
                this.setSize(600, 600);
                this.setLayout(null);
                this.setResizable(false);
                this.setTitle("黑马-李德国-数独游戏 ");
                printFrame();
                addListener();
                this.setVisible(true);
        }
}[/code]
回复 使用道具 举报
黑马网友  发表于 2011-7-27 01:36:31
沙发
运行类改动了一下,游戏共9关,同学们帮忙测试一下,找BUG,会加分的[code]import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.sun.awt.AWTUtilities;

public class Sudoku extends JFrame {

        final private TextField[][] txtGame;

        static int num=20;//空白格数量
        static int guan=5;//关卡数量
        static int add=5;//没关过后增加的空白格数量
        public static void main(String[] args) {
                Sudoku shudu = new Sudoku();

        }

        public Sudoku() {// 对JFrame进行布局初始以及监听设置
                txtGame = new TextField[9][9];// 建立81个TextField对象
                DoShudu shudu = new DoShudu();
                int[][] cells = shudu.getShudu();// 获取数独数组
                final JPanel jpl = new JPanel();// 建立JPanel对象
                final int spaceNum = num;// spaceNum表示需要设置空白TextField的数量
                jpl.setLayout(new GridLayout(9, 9));// JPanel布局
                final int[][] cellAn = new int[9][9];// 数独数组的答案
                System.arraycopy(cells, 0, cellAn, 0, cells.length);// 答案从建立的数独数组中Copy
                for (int i = 0; i < 9; i++) {// 把答案从Console打印出来

                        for (int j = 0; j < 9; j++) {
                                System.out.print(cellAn[i][j]);
                        }
                        System.out.println();
                } // 打印结束
                this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
                this.setSize(600, 600);
                this.setResizable(false);
                this.setTitle("黑马-李德国-数独游戏  9关");

                for (int i = 0; i < 9; i++) {

                        for (int j = 0; j < 9; j++) {

                                txtGame[i][j] = new TextField();

                                // 设置TextField背景颜色

                                if ((i < 3 && j < 3) || (i < 6 && i >= 3 && j >= 3 && j < 6)
                                                || (i < 9 && i >= 6 && j >= 6 && j < 9)) {
                                        txtGame[i][j].setBackground(Color.ORANGE);

                                }
                                if ((i < 6 && i >= 3 && j < 3) || (i < 3 && j >= 6 && j < 9)
                                                || (i < 9 && i >= 6 && j >= 3 && j < 6)) {
                                        txtGame[i][j].setBackground(Color.GREEN);
                                }

                                if ((i < 9 && i >= 6 && j < 3) || (i < 3 && j >= 3 && j < 6)
                                                || (i < 6 && i >= 3 && j < 9 && j >= 6)) {
                                        txtGame[i][j].setBackground(Color.PINK);
                                }

                                txtGame[i][j].setFont(new Font("Dialog", Font.CENTER_BASELINE,
                                                60));// 设置字体大小
                                txtGame[i][j].setText(Integer.toString(cells[i][j]));
                                txtGame[i][j].setEnabled(false);
                                txtGame[i][j].setVisible(true);
                                jpl.add(txtGame[i][j]);
                                jpl.setVisible(true);
                        }

                }

                final int[][] tempArray = new int[spaceNum][2];

                final JFrame jfm = new JFrame("选择数字");
                // 取消JFrame title
                jfm.setUndecorated(true);

                // 增加JFrame拖拽功能
                final Point origin = new Point();
                jfm.addMouseListener(new MouseAdapter() {
                        public void mousePressed(MouseEvent e) {
                                origin.x = e.getX();
                                origin.y = e.getY();
                        }
                });
                jfm.addMouseMotionListener(new MouseMotionAdapter() {
                        public void mouseDragged(MouseEvent e) {
                                Point p = jfm.getLocation();
                                jfm.setLocation(p.x + e.getX() - origin.x, p.y + e.getY()
                                                - origin.y);
                        }
                });

                // 设置JFrame为半透明
                AWTUtilities.setWindowOpacity(jfm, 0.7f);

                final JPanel jpnl = new JPanel(new GridLayout(3, 3));

                jfm.setLayout(null);
                jfm.setSize(190, 200);
                jfm.setResizable(false);
                jpnl.setBounds(0, 0, 190, 120);

                jfm.setResizable(false);

                for (int i = 0; i < spaceNum; i++) {// 依据需要空白的TextField数量,随机对TextField设置为空

                        final int ranD1 = new Random().nextInt(9);
                        final int ranD2 = new Random().nextInt(9);
                        tempArray[i][0] = ranD1;
                        tempArray[i][1] = ranD2;
                        txtGame[ranD1][ranD2].setText("");

                        if ((ranD1 < 3 && ranD2 < 3)
                                        || (ranD1 < 6 && ranD1 >= 3 && ranD2 >= 3 && ranD2 < 6)
                                        || (ranD1 < 9 && i >= 6 && ranD2 >= 6 && ranD2 < 9)) {
                                txtGame[ranD1][ranD2].setBackground(Color.ORANGE);

                        }
                        if ((ranD1 < 6 && ranD1 >= 3 && ranD2 < 3)
                                        || (ranD1 < 3 && ranD2 >= 6 && ranD2 < 9)
                                        || (ranD1 < 9 && ranD1 >= 6 && ranD2 >= 3 && ranD2 < 6)) {
                                txtGame[ranD1][ranD2].setBackground(Color.GREEN);
                        }

                        if ((ranD1 < 9 && ranD1 >= 6 && ranD2 < 3)
                                        || (ranD1 < 3 && ranD2 >= 3 && ranD2 < 6)
                                        || (ranD1 < 6 && ranD1 >= 3 && ranD2 < 9 && ranD2 >= 6)) {
                                txtGame[ranD1][ranD2].setBackground(Color.PINK);
                        }

                        txtGame[ranD1][ranD2].addMouseListener(new MouseAdapter() {

                                public void mouseClicked(MouseEvent mouseevent) {

                                        jfm.getContentPane().removeAll();// 移出了所有的组件
                                        jpnl.removeAll();

                                        for (int f = 0; f < 9; f++) {

                                                final Button btn = new Button((f + 1) + "");
                                                btn.setForeground(Color.RED);
                                                btn.setBackground(Color.WHITE);
                                                btn
                                                                .setFont(new Font("Dialog",
                                                                                Font.CENTER_BASELINE, 30));
                                                btn.addMouseListener(new MouseAdapter() {

                                                        @Override
                                                        public void mouseClicked(MouseEvent e) {
                                                                // TODO Auto-generated method stub
                                                                txtGame[ranD1][ranD2].setText(btn.getLabel()
                                                                                + txtGame[ranD1][ranD2].getText());

                                                        }
                                                });

                                                jpnl.add(btn);
                                        }
                                        Button btnDel = new Button(" 清 空 ");
                                        btnDel.setForeground(Color.WHITE);
                                        btnDel.setBackground(Color.RED);
                                        btnDel
                                                        .setFont(new Font("Dialog", Font.CENTER_BASELINE,
                                                                        30));
                                        btnDel.setBounds(0, 120, 190, 50);
                                        btnDel.addMouseListener(new MouseAdapter() {

                                                @Override
                                                public void mouseClicked(MouseEvent e) {
                                                        // TODO Auto-generated method stub
                                                        txtGame[ranD1][ranD2].setText("");

                                                }
                                        });

                                        jfm.add(jpnl);
                                        jfm.add(btnDel);
                                        jfm.setVisible(true);
                                }
                        });
                        txtGame[ranD1][ranD2].addTextListener(new TextListener() {// 对空白的TextField添加监听,数值发生变化后进行答案对比,如果全部答对在Console打印“good”
                                                @Override
                                                public void textValueChanged(TextEvent e) {
                                                        TextField tmp = (TextField) e.getSource();
                                                        int count = 0;
                                                        for (int u = 0; u < spaceNum; u++) {
                                                                if ((txtGame[tempArray[u][0]][tempArray[u][1]]
                                                                                .getText())
                                                                                .equals(Integer
                                                                                                .toString(cellAn[tempArray[u][0]][tempArray[u][1]]))) {
                                                                        count++;
                                                                }
                                                        }
                                                        if (count == spaceNum) {

                                                                jpl.removeAll();
                                                                FlowLayout blt = new FlowLayout();
                                                                jpl.setLayout(blt);
                                                               
                                                                if(num<=3){
                                                               
                                                                jpl.add(new JLabel("恭喜你过关"));
                                                                Button btn = new Button("进入下一关");

                                                                btn.addMouseListener(new MouseAdapter() {

                                                                        @Override
                                                                        public void mouseClicked(MouseEvent e) {
                                                                                // TODO Auto-generated method stub
                                                                                Sudoku.this.dispose();
                                                                                jfm.dispose();
                                                                                num=num+add;
                                                                                new Sudoku();
                                                                        }
                                                                });
                                                               
                                                                jpl.add(btn);
                                                                }
                                                                else{
                                                                        jpl.add(new JLabel("恭喜 你已经完成所有关卡!"));
                                                                }
                                                                jpl.updateUI();
                                                               
                                                               
                                                               
                                                               
                                                                System.out.println("good");
                                                               
                                                                       
                                                        }
                                                       
                                                       
                                                       
                                                }
                                        });
                        txtGame[ranD1][ranD2].setEnabled(true);
                }
                this.add(jpl);
                this.setVisible(true);
        }
}[/code]

评分

参与人数 1技术分 +4 收起 理由
admin + 4 虽然没去运行,但是分还是给你加的!

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马