📄 mainpanel.java
字号:
import javax.swing.*;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.border.Border;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.Dimension;
class MainPanel extends JFrame implements ActionListener{
private JMenu game=new JMenu("Game");
private JMenuItem ng=new JMenuItem("New Game");
private JMenuItem exit=new JMenuItem("Exit");
private JMenuBar mb=new JMenuBar();
private JMenu help=new JMenu("help");
private JMenuItem about=new JMenuItem("about Mine..");
private JLabel l1=new JLabel("Mines"),t=new JLabel("Timming");
private JRadioButtonMenuItem primary=new JRadioButtonMenuItem("Primary");
private JRadioButtonMenuItem medium=new JRadioButtonMenuItem("Medium",true);
private JRadioButtonMenuItem advanced=new JRadioButtonMenuItem("Advaced");
private ButtonGroup level=new ButtonGroup();
private final static int MAXRAW=30,MAXCOL=30;
static ImageIcon image1=new ImageIcon("face\\1.gif");
static ImageIcon image2=new ImageIcon("face\\2.gif");
static ImageIcon image0=new ImageIcon("face\\0.gif");
static ImageIcon image3=new ImageIcon("face\\3.gif");
static JButton b1=new JButton(image1);
static int raw=16,col=16;
static UnitPanel[][] au=new UnitPanel[MAXRAW][MAXCOL];
static {
for(int i=0;i<MAXRAW;i++)
for(int j=0;j<MAXCOL;j++)
au[i][j]=new UnitPanel(i,j);
}
//lq的w,h
int lw;
int lh;
//地雷面板
private JPanel lq=new JPanel();
Border compound,
raisedbevel = BorderFactory.createRaisedBevelBorder(),
loweredbevel = BorderFactory.createLoweredBevelBorder();
//提示面板
private Box tip=Box.createHorizontalBox();
Container cp=getContentPane();
//雷数primary=10,medium=40,advanced=99
static int mines=40;
//是否布雷
static boolean Layed;
//0=fail 1=playing 2=win
static int activation=1;
static JTextField rm=new JTextField(Integer.toString(mines)),
time=new JTextField("0");
static Timer tc=new TimeCounter();
class LevelChanger implements ActionListener {
public void actionPerformed(ActionEvent e) {
switch(currentLevel()) {
case 0:
setState(9,9,10);
restart();
lq.removeAll();
setLQ();
pack();
break;
case 1:
setState(16,16,40);
restart();
lq.removeAll();
setLQ();
pack();
break;
case 2:
setState(16,30,99);
restart();
lq.removeAll();
setLQ();
pack();
}
}
}
int currentLevel() {
return primary.isSelected()==true?0:medium.isSelected()==true?1:2;
}
void reset() {
activation=1;
tc.stop();
rm.setText(Integer.toString(mines));
Layed=false;
UnitPanel.openedPanes=0;
}
void restart() {
reset();
for(int i=0;i<raw;i++)
for(int j=0;j<col;j++)
au[i][j].reset();
}
public void actionPerformed (ActionEvent e) {
Object source=e.getSource();
if(source==ng||source==b1) {
b1.setIcon(image1);
restart();
}
else {
if(source==exit) {
System.exit(0);
}
else {
JOptionPane.showMessageDialog(this,"Version 1.0"+'\n'+
"整个软件的设计十分ugly我会慢慢改进的"+'\n'
+" "+"by Uniguy",
"关于制作游戏的一点心得",JOptionPane.INFORMATION_MESSAGE);
}
}
}
//设置雷区
void setLQ() {
lq.setLayout(new GridLayout(raw,col));
for(int i=0;i<raw;i++) {
for(int j=0;j<col;j++) {
lq.add(au[i][j]);
}
}
lq.setBorder(compound);
lq.setPreferredSize(new Dimension(19*col+8,19*raw+8));
}
//雷区状态
void setState(int r,int c,int mn) {
raw=r;
col=c;
mines=mn;
}
public MainPanel() {
compound=BorderFactory.createCompoundBorder(
raisedbevel,loweredbevel);
ng.addActionListener(this);
exit.addActionListener(this);
about.addActionListener(this);
b1.addActionListener(this);
primary.addActionListener(new LevelChanger());
medium.addActionListener(new LevelChanger());
advanced.addActionListener(new LevelChanger());
tip.add(l1);
tip.add(rm);
level.add(primary);
level.add(medium);
level.add(advanced);
help.add(about);
game.add(ng);
game.add(new JSeparator());
game.add(primary);
game.add(medium);
game.add(advanced);
game.add(new JSeparator());
game.add(exit);
mb.add(game);
mb.add(help);
setJMenuBar(mb);
tip.add(Box.createHorizontalGlue());
tip.add(b1);
tip.add(Box.createHorizontalGlue());
tip.add(t);
tip.add(time);
time.setEditable(false);
rm.setEditable(false);
setTitle("踩地雷");
setResizable(false);
cp.add(BorderLayout.NORTH,tip);
setLQ();
cp.add(BorderLayout.CENTER,lq);
pack();
Painter.initialize();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -