1 packagesodu;2 import java.io.*;3 import java.util.*;4 import java.math.*;5 importjava.security.PrivateKey;6 import java.awt.*;7 importjava.awt.event.ActionEvent;8 importjava.awt.event.ActionListener;9
10 importjavax.security.auth.login.CredentialExpiredException;11 importjavax.security.cert.CertificateNotYetValidException;12 import javax.swing.*;13 importjavax.swing.text.AbstractDocument.LeafElement;14
15 importorg.omg.PortableServer.ServantLocator;16 public class gra extendsJFrame {17 Scanner cin = newScanner(System.in);18 Random ran = newRandom();19 JMenuBar mbar;20 JMenuItem ito,itt,its;21 JMenu menu;22 JTextField [][]tx = new JTextField [9][9];23 GridLayout gri;24 JPanel chess= newJPanel();;25 int[][] ans = new int[9][9];26 int []a = new int [11];27 int [][]num = new int[9][9];28 int [][]fat ={29 {8,7,1,9,3,2,6,4,5},30 {4,9,5,8,6,1,2,3,7},31 {6,3,2,7,5,4,8,1,9},32 {5,2,8,4,7,3,1,9,6},33 {9,1,3,6,2,5,7,8,4},34 {7,6,4,1,9,8,3,5,2},35 {2,8,7,3,4,9,5,6,1},36 {1,4,6,5,8,7,9,2,3},37 {3,5,9,2,1,6,4,7,8},38 };39 gra(){40 }41 gra(String name,int a,int b,int c,intd){42 ints(name);43 setLocation(a,b);44 setSize(c,d);45 setVisible(true);46 set();47 setDefaultCloseOperation(EXIT_ON_CLOSE);48 }49 voidShow(){50 chess.updateUI();51 chess.removeAll();52 gri = new GridLayout(9,9);53 for(int i = 0 ; i < 9 ; i++)54 for(int j = 0 ; j <9 ; j++){55 String s =String.valueOf(num[i][j]);56 int n = ran.nextInt(13);57 if(n%4==0){58 ans [i][j] =num [i][j];59 JLabel text = newJLabel(s,JLabel.CENTER);60 text.setForeground(Color.white);61 chess.add(text);62 }else{63 tx[i][j] = newJTextField();64 tx[i][j].setHorizontalAlignment(SwingConstants.CENTER);65 chess.add(tx[i][j]);66 }67 }68 chess.setLayout(gri);69 chess.setBackground(Color.DARK_GRAY);70 add(chess);71 }72 voidset(){73 ito.addActionListener(newActionListener(){74 public voidactionPerformed(ActionEvent e) {75 setVisible(false);76 calc();77 Show();78 setVisible(true);79 }80 });81 itt.addActionListener(newActionListener(){82 public voidactionPerformed(ActionEvent e) {83 judge();84 }85 });86 its.addActionListener(newActionListener() {87 public voidactionPerformed(ActionEvent e) {88 setVisible(false);89 }90 });91
92 }93 voidints(String s){94 setTitle(s);95 mbar = newJMenuBar();96 menu = new JMenu("菜单");97 ito = new JMenuItem("开始");98 itt = new JMenuItem("提交");99 its = new JMenuItem("退出");100 menu.add(ito);101 menu.add(itt);102 menu.add(its);103 mbar.add(menu);104 setJMenuBar(mbar);105 }106 voidcalc(){107 boolean []b = new boolean[11];108 int i = 0;109 b[0] = true;110 while(i<9){111 int n = ran.nextInt(10);112 if(!b[n]) {113 a[i++] =n;114 b[ n ] = true;115 }116 }117 for(int k = 0 ; k < 9 ; k++)118 for(int m = 0 ; m < 9 ; m++)119 for(int n = 0 ; n < 9 ; n++){120 if(fat[m][n]==a[ k ] && k != 8){121 num[m][n] = a[k+1];122 }else if(fat[m][n]==a[k]&&k == 8){123 num[m][n] = a[0];124 }125 }126 voidjudge(){127 int falg = 0;128 for(int i = 0 ; i < 9 ; i++)129 for(int j = 0 ; j < 9 ; j++){130 if(ans[i][j] == 0) {131 int tmp =Integer.valueOf(tx[i][j].getText());132 ans[i][j] =tmp;133 }134 }135 for(int i = 0 ; i < 9 ; i++){136 boolean[]h = new boolean[10];137 boolean[]s = new boolean[10];138 for(int j = 0 ; j < 9 ; j++){139 if(!h[ans[i][j]]&&!s[ans[i][j]]){140 h[ans[i][j]] = true;141 s[ans[i][j]] = true;142 }else{143 falg = 1;144 break;145 }146 }147 if(falg == 1) break;148 }149 if(falg == 1) {150 JFrame a = new JFrame("错误");151 JTextField text = new JTextField("你的答案错了");152 text.setHorizontalAlignment(SwingConstants.CENTER);153 a.add(text);154 a.setSize(200,200);155 a.setLocation(500,500);156 a.setVisible(true);157 }else{158 JFrame a = new JFrame("成功");159 JTextField text = new JTextField("你的答案正确");160 text.setHorizontalAlignment(SwingConstants.CENTER);161 text.setForeground(Color.red);162 a.add(text);163 a.setSize(200,200);164 a.setLocation(500,500);165 a.setVisible(true);166 }167 }168 }169