JAVA 扫雷 程序

文件列表

2.主程序入口

3.1部分源代码

package MineSweeper;

import java.awt.BorderLayout;
import java.awt.Font;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.UIManager;

public class SartFrame extends JFrame{

    private static final long serialVersionUID = -1585043387266273492L;
    private MineState mineState; // 记雷数
    private MineField mineField; // 布置labble和雷数
    private MineMenu mineMenu;
    private Timer timer;
    private Timers timers;
    /**
     * 游戏是否开始
     */
    private boolean isStart;
    JLabel jLabel_start = new JLabel(); // 开始图片

    public SartFrame() {
        // 改变系统默认字体
        Font font = new Font("Dialog", Font.PLAIN, 12);
        Enumeration<Object> keys = UIManager.getDefaults().keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof javax.swing.plaf.FontUIResource) {
                UIManager.put(key, font);
            }
        }
        this.setTitle("扫雷");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        setIconImage(Tools.iicon); // 利用tools来做的作法

        this.setResizable(false); // 这样让窗口不会可放大

        // ..................状态栏.....................
        mineState = new MineState(this);
        this.add(mineState, BorderLayout.NORTH);

        // ...................雷区......................
        mineField = new MineField(this);
        this.add(mineField, BorderLayout.CENTER);

        jLabel_start.setIcon(Tools.start);
        this.add(jLabel_start, BorderLayout.CENTER);

        // ....................菜单栏................
        mineMenu = new MineMenu(this);
        this.setJMenuBar(mineMenu);

        // .....................时间................
        Tools.time = 0;
        timers = new Timers(mineState);
        timer = new Timer(1000, timers);

        // ...................声音...................
        // AudioClip s1=loadSound("alarm1.wav"); //AudioClip类的对象s1通过方法 loadSound()装载声音
        // public AudioClip loadSound(String filename){ //返回一个AudioClip对象
        // URL url=null; //因为newAudioClip()的参数为URL型
        // try{
        // url=new URL("file:"+filename); //指定文件,“file:"不能少
        // }
        // catch(MalformedURLException e){ }
        // return Applet.newAudioClip(url); //通过newAudioClip(
        // )方法装载声音,此方法为JDK后添加的方法,太老的JDK里可能没有
        // }
        //
        //
        //
        //
        pack();
        this.setVisible(true);
    }

    // 重新布局
    public void restart() {

        this.remove(mineState);

        this.remove(mineField);

        this.remove(jLabel_start);

        // ..................状态栏.....................
        mineState = new MineState(this);
        this.add(mineState, BorderLayout.NORTH);

        // ...................雷区......................
        mineField = new MineField(this);
        this.add(mineField, BorderLayout.CENTER);

        // .....................时间................
        Tools.time = 0;
        Timers timers = new Timers(mineState);
        timer = new Timer(1000, timers);

        pack();
        validate();// 刷新窗体
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    public MineState getMineState() {
        return mineState;
    }

    public MineField getMineField() {
        return mineField;
    }

    public MineMenu getMineMenu() {
        return mineMenu;
    }

    public Timer getTimer() {
        return timer;
    }

    public Timers getTimers() {
        return timers;
    }

    public boolean isStart() {
        return isStart;
    }

    public void setStart(boolean isStart) {
        this.isStart = isStart;
    }

    public static void main(String[] args) {
        new SartFrame();
    }
}
SartFrame.java

 

About.java

package MineSweeper;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class About extends JDialog {
    private static final long serialVersionUID = 1L;
    private JTextField jTextField1,jTextField2,jTextField3;
    private JButton butyes,butno;
    SartFrame sartFrame;
    public About(SartFrame sartFrame) {
        this.sartFrame = sartFrame;
        this.setTitle("");
        this.setLocationRelativeTo(null);
        this.setModal(true);
        this.setSize(new Dimension(200,200));
        this.init();
        this.setVisible(true);
    }

    private void init() {
        JPanel jPanel = new JPanel();
         new JLabel("");
         jTextField1=new JTextField(12);
         jTextField2=new JTextField(12);
         jTextField3=new JTextField(12);
         
         JLabel jLabelTotalx=new JLabel("行数:");
         JLabel jLabelTotaly=new JLabel("列数:");
         JLabel jLabelTotalMine=new JLabel("雷数:");

        jPanel.add(jLabelTotalx);
        jPanel.add(jTextField1);
        
        jPanel.add(jLabelTotaly);
        jPanel.add(jTextField2);
        
        jPanel.add(jLabelTotalMine);
        jPanel.add(jTextField3);

        
        butyes = new JButton("确定");
        butno = new JButton("取消");
        jPanel.add(butyes);
        jPanel.add(butno);
        
        butyes.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
            
                try {
                    Tools.totalx=Integer.parseInt(jTextField1.getText());
                    Tools.totaly=Integer.parseInt(jTextField2.getText());
                    Tools.totalMine=Integer.parseInt(jTextField3.getText());
                    if(Tools.totalx>9&&Tools.totalx<30){
                        if(Tools.totaly>9&&Tools.totaly<30){
                            if(Tools.totalMine>=10&&Tools.totalMine<Tools.totalx*Tools.totaly*4/5){
                                sartFrame.restart();
                                About.this.dispose();
                            }else{
                                JOptionPane.showMessageDialog(null, "布雷有误请重试!!!\n布雷数应少于总格子数!!!", "提示消息", JOptionPane.NO_OPTION);    
                            }
                        }else {
                            JOptionPane.showMessageDialog(null, "您输入的列数有误\n请重试!!!", "提示消息", JOptionPane.NO_OPTION);        
                        }                
                    }else {
                        JOptionPane.showMessageDialog(null, "您输入的行数有误\n请重试!!!", "提示消息", JOptionPane.NO_OPTION);    

                    }        
                } catch (Exception e2) {
                    JOptionPane.showMessageDialog(null, "必须是数字类型", "提示消息", JOptionPane.NO_OPTION);
                    return;
                }
                
            }
        });
        
        butno.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                About.this.dispose();
            }
        });
        this.add(jPanel);
    }
}
About.java

 源代码下载  https://pan.baidu.com/s/1c1UlluC

posted @ 2017-09-30 16:37  落霞秋水  阅读(1645)  评论(0编辑  收藏  举报