java程序

13年前

 

//创建一个button

package com.dhl.saolei;

import javax.swing.JButton;

 

public class ExtendButton extends JButton {

    private int Button_pos;

    private boolean Button_Status;

    private boolean Button_Visited;

   

    public int SetPostion(int pos) {

       this.Button_pos = (pos >= 0 && pos <= 81) ? pos : 0;

       return this.Button_pos;

    }  

    public int GetPostion() {

       return this.Button_pos;

    }

   

    public boolean SetStatus(boolean sta) {

       this.Button_Status = sta;

       return this.Button_Status;

    }

    public boolean GetStatus() {

       return this.Button_Status;

    }

   

    public boolean SetVisited(boolean vis) {

       this.Button_Visited = vis;

       return this.Button_Visited;

    }

    public boolean GetVisited() {

       return this.Button_Visited;

    }

   

}

 

 

 

//窗体设计类

package com.dhl.saolei;

 

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.GridBagLayout;

import java.awt.GridLayout;

import java.awt.Insets;

import java.awt.Panel;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.InputEvent;

import java.awt.event.KeyEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JToolBar;

import javax.swing.KeyStroke;

 

public class Game extends JFrame implements ActionListener{

      

    private Container myContainer;

    private GridLayout myLayout;

    private Panel myPanelMain;

    private Panel myPanel;

   

    private JMenuBar menuBar;

    private JMenu menuE, menuH;

    private JMenuItem menuItemCZ, menuItemSZ;

   

    private JToolBar toolBar;

    private JButton buttonCz, buttonSz;

   

    private int numBomb;

   

   

    ExtendButton[] myButton = new ExtendButton[81];

//构造方法

    public Game() {

       this.setTitle("Game");

       this.setBounds(400, 100, 300, 400);      

       this.InitButton();

       this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

    }

   

    private void InitButton() {

       numBomb = 10;

       myContainer = this.getContentPane();

       myLayout = new GridLayout(9, 9, 1, 1);

       myPanelMain = new Panel(new BorderLayout());

       myPanel = new Panel();

       myPanel.setVisible(true);

       myPanel.setLayout(myLayout);

       myPanelMain.add(myPanel, BorderLayout.CENTER);

       myContainer.add(myPanelMain);

      

//菜单初始化

       menuBar = new JMenuBar();

       menuE = new JMenu("菜单(E)");

       menuH = new JMenu("帮助(H)");

      

       menuItemCZ = new JMenuItem("重置");

       menuItemCZ.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));

       menuE.add(menuItemCZ);

      

       menuItemSZ = new JMenuItem("设置");   

       menuE.add(menuItemSZ);

      

       menuBar.add(menuE);

       menuBar.add(menuH);

       setJMenuBar(menuBar);

      

//工具条初始化

       toolBar = new JToolBar("工具");

       buttonCz = new JButton("重置");

       buttonCz.addActionListener(this);

       toolBar.add(buttonCz);

       buttonSz = new JButton("设置");

       buttonSz.addActionListener(this);

       toolBar.add(buttonSz);

       myPanelMain.add(toolBar, BorderLayout.NORTH);

      

      

      

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

      

           myButton[i] = new ExtendButton();

           myButton[i].SetPostion(i);

           myButton[i].SetVisited(false);

           myButton[i].SetStatus(false);

           myButton[i].setMargin((Insets) new Insets(1,1,1,1).clone());

//设置右键监听标记雷

           myButton[i].addMouseListener(new MouseAdapter() {

              public void mouseClicked(MouseEvent evt) {

                  if ((evt.getModifiers() == InputEvent.BUTTON3_MASK)) {

                    

                     String str = ((ExtendButton)evt.getSource()).getText();

                                      

                     if(str == "o") {

                         ((ExtendButton)evt.getSource()).setText(" ");

                     } else {

                         ((ExtendButton)evt.getSource()).setText("o");

                     }

                  }

 

              }

           });

           myPanel.add(myButton[i]);

           myButton[i].addActionListener(this);

       }

      

      

      

       System.gc();

       this.SetBomb(numBomb);

       show();

      

      

    }

   

 

//重置方法 

    private void SetExtendButton() {

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

           myButton[i].SetVisited(false);

           myButton[i].SetStatus(false);

           myButton[i].setText(null); 

           myButton[i].setBackground(null);         

       }

       this.SetBomb(numBomb);

    }

   

//布雷方法

    private void SetBomb(int count) {

       int counter = 0;

       int tempint;

       while(counter != count) {

           tempint = (int) (Math.random()*81);

          

           if(!myButton[tempint].GetStatus()) {

              myButton[tempint].SetStatus(true);

              counter++;

           }         

       }            

    }

   

//显示雷得方法

    private void ShowBomb() {

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

           if(myButton[i].GetStatus()) {

              myButton[i].setBackground(new Color(0, 0, 0));

           }

       }

    }

   

//监听点击扫雷方法

    private void CheckButton(ExtendButton TempButton) {

       if(TempButton.GetStatus()) {

           JOptionPane.showMessageDialog(null, "You Failed!", "Game",JOptionPane.INFORMATION_MESSAGE);

           this.ShowBomb();

          

           return;

       }

                 

       int[] CircleNum = new int[8];

       int temp = 0;

       if(!TempButton.GetVisited()) {

           CircleNum[0] = TempButton.GetPostion() - 10;

           CircleNum[0] = (CircleNum[0] < 0 || (CircleNum[0] + 1) % 9 == 0) ? -1 : CircleNum[0];

          

           CircleNum[1] = TempButton.GetPostion() - 9;

           CircleNum[1] = (CircleNum[1] < 0) ? -1 : CircleNum[1];

          

           CircleNum[2] = TempButton.GetPostion() - 8;

           CircleNum[2] = (CircleNum[2] < 0 ||CircleNum[2] % 9 == 0) ? -1 : CircleNum[2];

          

           CircleNum[3] = TempButton.GetPostion() - 1;

           CircleNum[3] = ((CircleNum[3] + 1) % 9 == 0) ? -1 : CircleNum[3];

          

           CircleNum[4] = TempButton.GetPostion() + 1;

           CircleNum[4] = (CircleNum[4] % 9 == 0) ? -1 : CircleNum[4];

          

           CircleNum[5] = TempButton.GetPostion() + 8;

           CircleNum[5] = (CircleNum[5] > 80 || (CircleNum[5] + 1) % 9 == 0) ? -1 : CircleNum[5];

                    

           CircleNum[6] = TempButton.GetPostion() + 9;

           CircleNum[6] = (CircleNum[6] > 80) ? -1 : CircleNum[6];

          

           CircleNum[7] = TempButton.GetPostion() + 10;

           CircleNum[7] = (CircleNum[7] > 80 || CircleNum[7] % 9 == 0) ? -1 : CircleNum[7];           

          

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

              if(CircleNum[i] != -1) {              

                 if(myButton[CircleNum[i]].GetStatus() && !myButton[CircleNum[i]].GetVisited()) {

                     temp++;

                  }

              }

           }

//显示过程

           if(temp > 0) {

              TempButton.SetVisited(true);             

              TempButton.setText(String.valueOf(temp));       

              temp = 0;

           } else if(temp == 0) {

              TempButton.SetVisited(true);

              TempButton.setBackground(new Color(125, 152, 0));

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

                  if(CircleNum[i] != -1 && !myButton[CircleNum[i]].GetVisited()) {

                     CheckButton(myButton[CircleNum[i]]);

                  }

              }

           }         

          

       }

          

    }

   

   

    public void actionPerformed(ActionEvent e) {

       //81button的监听进行处理

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

           if(e.getSource() == myButton[i]) {

              CheckButton((ExtendButton) e.getSource());

           }

       }

       //重置按钮监听

       if(e.getSource() == buttonCz) {

           SetExtendButton();

       //设置按钮进行监听

       } else if(e.getSource() == buttonSz) {

           String num = JOptionPane.showInputDialog(null, "请输入要布置的雷得个数!", "消息", JOptionPane.INFORMATION_MESSAGE);

           numBomb = Integer.parseInt(num);        //得到要布置的雷得数目

           SetExtendButton();  

       }

      

      

    }

   

}

 

 

 

 

 

//运行类

package com.dhl.saolei;

 

public class RunGame {

 

    public static void main(String[] args) {

       Game game = new Game();

 

    }

 

}