📄 minesweepingmain.java
字号:
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
/**
* <p>Title: 扫雷</p>
*
* <p>Description: 仿Windows自带的小游戏-扫雷-Java 版</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: 中国矿业大学</p>
*
* <p>Email:chxzhou012@126.com</p>
*
* <p>QQ:395246039</p>
*
* @author zhou(周川祥)
* @version 1.0
*/
public class MineSweepingMain extends JFrame implements ActionListener
{
static MineSweeping mineSweeping ;
JMenuBar menuBar ;
JMenu menuSet , menuHelp ;
JMenuItem setItem , helpItem ,authorItem;
int gridx = 12 ;
int gridy = 12 ;
int nMine = 10 ;
SetEventListener setEvent;
public MineSweepingMain ( String title )
{
super ( title ) ;
menuBar = new JMenuBar () ;
menuSet = new JMenu ( "游戏" ) ;
menuHelp = new JMenu ( "帮助" ) ;
setItem = new JMenuItem ( "自定义大小" ) ;
helpItem = new JMenuItem ( "帮助" ) ;
authorItem = new JMenuItem ( "关于Java版扫雷" ) ;
menuBar.add ( menuSet ) ;
menuSet.add ( setItem ) ;
menuHelp.add ( helpItem ) ;
menuBar.add ( menuHelp ) ;
menuHelp.add(authorItem);
this.setJMenuBar ( menuBar ) ; //添加菜单栏
setEvent=new SetEventListener();
setItem.addActionListener ( setEvent ) ;
}
public static void main ( String[] args )
{
JFrame.setDefaultLookAndFeelDecorated ( true ) ; //设定标准接口
MineSweepingMain m = new MineSweepingMain ( "扫雷Java版" ) ;
m.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ) ; //预设窗口关闭方式
//m.setIconImage(Toolkit.getDefaultToolkit().createImage("picture/mineSweeping.gif"));
m.setLayout ( null ) ;
Container cont = new Container () ;
cont.setBounds ( 0 , 0 , ( m.gridx + 2 ) * 16 + 10 , ( m.gridy + 6 ) * 16 + 50 ) ;
m.add ( cont ) ;
m.setLocation ( 300 , 200 ) ;
m.setSize ( ( m.gridx + 2 ) * 16 + 10 , ( m.gridy + 6 ) * 16 + 70 ) ; //设定窗口大小
m.setVisible ( true ) ;
m.setResizable ( false ) ;
mineSweeping = new MineSweeping ( m.gridx , m.gridy , m.nMine , cont ) ; //建立SingleMine对象
cont.addMouseListener ( mineSweeping ) ; //加入鼠标监听事件
cont.addMouseMotionListener ( mineSweeping ) ; //加入鼠标监听事件
}
public void actionPerformed ( ActionEvent e )
{
if ( e.getSource () == setItem )
{
this.gridx=setEvent.gridx;
this.gridy=setEvent.gridy;
this.nMine=setEvent.mineCount;
}
}
private class SetEventListener extends JFrame implements ActionListener
{
JButton sureBtn , cancelBtn ;
//MineSweepingMain mineSet;
int gridx , gridy , mineCount ;
JTextField tefGridx , tefGridy , tefMineCount ;
public SetEventListener ()
{
tefGridx = new JTextField ( 10 ) ;
tefGridy = new JTextField ( 10 ) ;
tefMineCount = new JTextField ( 10 ) ;
JLabel setXLabel = new JLabel ( "宽度:" ) ;
JLabel setYLabel = new JLabel ( "高度:" ) ;
JLabel setMineCountLabel = new JLabel ( "雷数:" ) ;
sureBtn = new JButton ( "确定" ) ;
cancelBtn = new JButton ( "取消" ) ;
setLayout ( null ) ;
tefGridx.setBounds ( 80 , 40 , 70 , 20 ) ;
tefGridy.setBounds ( 80 , 80 , 70 , 20 ) ;
tefMineCount.setBounds ( 80 , 120 , 70 , 20 ) ;
setXLabel.setBounds ( 20 , 40 , 70 , 20 ) ;
setYLabel.setBounds ( 20 , 80 , 70 , 20 ) ;
setMineCountLabel.setBounds ( 20 , 120 , 70 , 20 ) ;
sureBtn.setBounds ( 190 , 50 , 70 , 28 ) ;
cancelBtn.setBounds ( 190 , 100 , 70 , 28 ) ;
sureBtn.addActionListener ( this ) ;
cancelBtn.addActionListener ( this ) ;
tefGridx.addActionListener(this);
tefGridy.addActionListener(this);
tefMineCount.addActionListener(this);
add ( setXLabel ) ;
add ( setYLabel ) ;
add ( setMineCountLabel ) ;
add ( tefGridx ) ;
add ( tefGridy ) ;
add ( tefMineCount ) ;
add ( sureBtn ) ;
add ( cancelBtn ) ;
setSize ( 300 , 200 ) ;
}
public void actionPerformed ( ActionEvent e )
{
this.setVisible ( true ) ;
if ( e.getSource () == sureBtn )
{
this.gridx = Integer.parseInt( tefGridx.getText () ) ;
this.gridy = Integer.parseInt ( tefGridy.getText () ) ;
this.mineCount = Integer.parseInt( tefMineCount.getText () ) ;
this.dispose () ;
}
if ( e.getSource () == cancelBtn )
{
this.dispose () ;
}
}
}
public void paint ( Graphics g )
{
super.paint ( g ) ;
if ( mineSweeping != null )
{
mineSweeping.draw.update () ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -