?? cleanmine.java
字號:
package day02.cleanMine;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 掃雷游戲主界面
* @author tony.tang
*
*/
public class CleanMine extends JFrame implements ActionListener{
private JLabel text1,text2;
public static JLabel remainMine;//剩余地雷數
private JLabel time;//消耗時間
private JButton reset;//重新開始
private JPanel center;
private int row,col,mine;
public CleanMine(){
text1=new JLabel("剩余地雷:");
text2=new JLabel("消耗時間:");
remainMine=new JLabel("10");
time=new JLabel("0");
reset=new JButton("重新開始");
reset.addActionListener(this);
JMenuBar bar=new JMenuBar();
JMenu game=new JMenu("游戲");
JMenu help=new JMenu("幫助");
JMenuItem item;
game.add(item=new JMenuItem("開局"));item.addActionListener(this);
game.addSeparator();
ButtonGroup bg=new ButtonGroup();
game.add(item=new JCheckBoxMenuItem("初級",true));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("中級"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("高級"));bg.add(item);item.addActionListener(this);
game.add(item=new JCheckBoxMenuItem("自定義..."));bg.add(item);item.addActionListener(this);
game.addSeparator();
game.add(item=new JMenuItem("退出"));item.addActionListener(this);
help.add(item=new JMenuItem("查看幫助"));item.addActionListener(this);
help.add(item=new JMenuItem("關于掃雷..."));item.addActionListener(this);
bar.add(game);
bar.add(help);
this.setJMenuBar(bar);
init();
}
private void init(){
JPanel north=new JPanel();
north.add(text1);
north.add(remainMine);
north.add(reset);
north.add(text2);
north.add(time);
this.add(north,BorderLayout.NORTH);
this.row=9;
this.col=9;
this.mine=10;
restart();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Thread(){
public void run(){
while(Integer.parseInt(remainMine.getText())>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
time.setText((Integer.parseInt(time.getText())+1)+"");
}
}
}.start();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("初級")){
this.row=9;
this.col=9;
this.mine=10;
restart();
return;
}
if(e.getActionCommand().equals("中級")){
this.row=16;
this.col=16;
this.mine=40;
restart();
return;
}
if(e.getActionCommand().equals("高級")){
this.row=16;
this.col=30;
this.mine=99;
restart();
return;
}
if(e.getActionCommand().equals("重新開始")){
restart();
return;
}
}
private void restart(){
if(center!=null){
this.remove(center);
}
center=new AllButtonPanel(row,col,mine);
this.add(center,BorderLayout.CENTER);
this.remainMine.setText(mine+"");
this.time.setText("0");
this.setSize(col*30,row*30+10);
this.setResizable(false);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new CleanMine();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -