J a v a编写的扫雷小程序Prepared on 21 November 2021整个程序使用了4个类:Game 用于启动程序, 界面的布置,各种行列的算法。
ExtendButton 继承了Jbutton 用于新建button的各种属性ShowNum 监听到某个button后,计算出此button周围有几个雷,不同的雷对应不通的图片,类得到button上应加载的图片地址ViewReSet 此类用于新建一个窗口,用户输入自己想要的行、列、雷数,然后用新的行列雷数new 一个新的Gamebiaose dise1 dise2 dise3dise4 dise5 dise6 dise7dise8 dise lei biaoji/****************************************************************运行类创建游戏主窗体*/package class Game extends JFrame implements ActionListener{private Container myContainer;private GridLayout myLayout;private JPanel myPanelMain;private JPanel myPanel;private JToolBar myTool;private JLabel time;private JLabel labelTime;private JLabel bombNum;private JLabel labelBombNum;private JMenuBar menuBar;private JMenu menuE, menuH;private JMenuItem menuItemCZ, menuItemSZ;private int numBomb, countTime, leaveBomb, numEnd;private int myRows, myColumns;private int xis, yis;private Timer timer;private ExtendButton[] myButton = null;etPostion(i);[i].setIcon((new ShowNum(10)).getImageIcon());[i].setButton_num(0);[i].SetVisited(false);[i].SetStatus(false);ddMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent evt) {if (() == ) {int n =((ExtendButton)()).getButton_num();if(n == 1) {if(!((ExtendButton)()).GetVisited()) {((ExtendButton)()).setButton_num(0);((ExtendButton)()).setIcon((new ShowNum(10)).getImageIcon());leaveBomb++;(leaveBomb));}} else {if(!((ExtendButton)()).GetVisited()) {((ExtendButton)()).setButton_num(1);((ExtendButton)()).setIcon((new ShowNum(11)).getImageIcon());leaveBomb--;(leaveBomb));}}}}});[i].addActionListener(this);}();;show();etVisited(false);[i].SetStatus(false);[i].setText(null);[i].setIcon((new ShowNum(10)).getImageIcon());}(numBomb);= 0;= numBomb;= * - leaveBomb;}etStatus()) {[tempint].SetStatus(true);counter++;}}}etStatus()) {[i].setIcon((new ShowNum(9)).getImageIcon());}}}etStatus() && !myButton[CircleNum[i]].GetVisited()) {temp++;}}}etImageIcon());temp = 0;} else if(temp == 0) {(true);((new ShowNum(0)).getImageIcon());for (int i = 0; i < 8; i++) {if(CircleNum[i] != -1&& !myButton[CircleNum[i]].GetVisited()) {CheckButton(myButton[CircleNum[i]]);}}};if == 0) {(this, "恭喜你!", "消息", ;ShowBomb();}}}etScreenSize();int x = (int) (() - 400) / 2);int y = (int) (() - 500) / 2);= 300;= 400;(x, y, , ;("扫雷");= 10;= 9;= 9;();();;}public Game(int row, int column, int numbomb) {Dimension dimension = ().getScreenSize();int x = (int) (() - 400) / 2);int y = (int) (() - 500) / 2);= 310 * column /9;= 300 * row / 9 + 100;(x, y, , ;("扫雷");= row;= column;= numbomb;();();;}class/images/");break;case 1:imageIcon = new ImageIcon("./class/images/");break;case 2:imageIcon = new ImageIcon("./class/images/");break;case 3:imageIcon = new ImageIcon("./class/images/");break;case 4:imageIcon = new ImageIcon("./class/images/");break;case 5:imageIcon = new ImageIcon("./class/images/");break;case 6:imageIcon = new ImageIcon("./class/images/");break;case 7:imageIcon = new ImageIcon("./class/images/");break;case 8:imageIcon = new ImageIcon("./class/images/");break;case 9:imageIcon = new ImageIcon("./class/images/");break;case 10:imageIcon = new ImageIcon("./class/images/");break;case 11:imageIcon = new ImageIcon("./class/images/");break;default:imageIcon = new ImageIcon("./class/images/");break;}return imageIcon;}}/********************************************************************创建一个新窗口,输入新的行、列、雷数,然后用新数据重新new一个Game窗口*/package class ViewReSet extends JDialog implements ActionListener{private JFrame frame = null;private JPanel myPanel = null;private JLabel myLabel = null;private JLabel myLabelRow = null;private JLabel myLabelColum = null;private JLabel myLabelBombNum = null;private JTextField myFieldRow = null;private JTextField myFieldColum = null;private JTextField myFieldBombNum = null;private JButton buttonOK = null;private JButton buttonReset = null;private int row;private int column;private int bombNum;private int [] myrcb = new int [3];private void createView(){Container container = ();= new JPanel(new GridBagLayout());;GridBagConstraints gbc = new GridBagConstraints();= 1;= 0;= new JLabel("窗口行列设置"); gbc);= 0;= 1;= new JLabel("行数:");gbc);= 1;= 1;= new JTextField(10);gbc);= 0;= 2;= new JLabel("列数:");gbc);= 1;= 2;= new JTextField(10);gbc);= 0;= 3;= new JLabel("雷数:");gbc);= 1;= 3;= new JTextField(10);gbc);JPanel btnPanel = new JPanel(); (false);= new JButton("确定");;= new JButton("重置");;= 1;= 4;gbc);("设置");;}public ViewReSet(int r, int c, int b) {Dimension dimension = ().getScreenSize();int xAxis = (int) (() - 300) / 2);int yAyis = (int) (() - 200) / 2);(xAxis, yAyis, 300, 200);[0] = r;[1] = c;[2] = b;();(true);}public void actionPerformed(ActionEvent e) {if() == {} else if() == { String r, c, b;r = if() > 0) {= (r);} else {= [0];}c = if() > 0) {= (c);} else {= [1];}b = if() > 0) {= } else {= [2];}new Game, , ;();}}}。