package shudoGUI; 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.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Sudoku extends JFrame { final private TextField[][] txtGame; static String level="简单"; static int num=20;//空白格数量 static int guan=5;//关卡数量 static int add=5;//没关过后增加的空白格数量 public static void main(String[] args) { JButton j1=new JButton("简单"); JButton j2=new JButton("中等"); JButton j3=new JButton("较难"); j1.setSize(100,30); j2.setSize(100,30); j3.setSize(100,30); j1.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { Sudoku.level="简单"; Sudoku.num=20; Sudoku shudu = new Sudoku(); } }); j2.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { Sudoku.level="中等"; Sudoku.num=30; Sudoku shudu = new Sudoku(); } }); j3.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { Sudoku.level="较难"; Sudoku.num=60; Sudoku shudu = new Sudoku(); } }); //// JFrame j=new JFrame(); j.setLayout(new GridLayout(1, 3)); j.add(j1); j.add(j2); j.add(j3); j.setSize(200, 100); j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); j.setVisible(true); j.setTitle("选择难度"); } public Sudoku() {// 对JFrame进行布局初始以及监听设置 txtGame = new TextField[9][9];// 建立81个TextField对象 DoShudu shudu = new DoShudu(); int[][] cells = DoShudu.newshudu();// 获取数独数组 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(300, 300); this.setResizable(false); this.setTitle("数独 难度:"+Sudoku.level); 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, 20));// 设置字体大小 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为半透明 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); } }
程序实现界面如下:
程序分析:本次作业基于上一次的数独作业,改正了数独生成个数0<N<1000000,并且代码已上传至conding,地址https://git.coding.net/dhlg_201810775002/shudu.git。
程序实现的功能:能生成正确的GUI界面,不同颜色位于空白处,并且可以选择相应的难易程度,开始玩数独游戏,当所有空白处填写正确后,会出现提示“恭喜你已完成此关卡”。
学习心得:第一次做GUI界面,查了很多资料和从网上百度经验,花费的时间较多。并将上一次作业没有实现的功能进行了改进,对GUI知识点进行了具体学习,如控件的设置学习,方法的实现等。希望下一次的作业能掌握更多的知识点,加强自身的学习。
原文:https://www.cnblogs.com/luoyy/p/9807345.html