import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main
{
public static void main(String[] argus)
{
MainGui begin = new MainGui();
}
}
//
//**界面类*/
class MainGui extends JFrame
{
public static int length_ = 5; //格子列数
public static int width = 5; //格子行数
public static int stepNumber = 0; //步数
public static MyTimer t = new MyTimer(); //计时器
CardsButton [][] cards = new CardsButton[22][22]; //游戏区按钮数组
/*监听者*/
CardsListener cardslistener = new CardsListener(this);
MenuListener menuListener = new MenuListener(this);
DetermineListener determineListener = new DetermineListener(this);
/*文本框,按钮*/
JTextField stepText = new JTextField(4);
JTextField lengthText = new JTextField(4);
JTextField widthText = new JTextField(4);
JButton quitButton = new JButton ("退出");
JButton restartButton = new JButton("重新开始");
JButton startButton = new JButton("开始");
JButton determineButton = new JButton("确定");
public Insets getInsets()
{//覆盖Insets方法,将组件与周围边界隔开
Insets squeeze = new Insets(40,20,10,20);
return squeeze;
}
public MainGui()
{
/*主窗口属性*/
super("翻牌,小老头");
int windowLength = (length_ * 60 > 440) ? length_ * 60 : 440 ;
int windowWidth = (width * 60 > 350) ? width * 60 : 350 ;
setBounds(300,200,windowLength,windowLength);//设置窗口大小,随格子数多少而变化,但至少为440*350
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
/*cardsPanel*/
JPanel cardsPanel = new JPanel();
cardsPanel.setLayout(new GridLayout(length_,width));
for(int i=0; i<22; ++i){
for(int j=0; j<22;++j){
cards[i][j] = new CardsButton(i,j);
}
}
for(int i=1; i<length_+1; ++i){
for(int j=1; j<width+1;++j){
cardsPanel.add(cards[i][j]);
cards[i][j].addActionListener(cardslistener);
cards[i][j].setEnabled(false);
}
}
/*rightPanel*/
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.Y_AXIS));
/*步数,时间区*/
JPanel stepInRightPanel = new JPanel();
stepInRightPanel.setLayout(new GridLayout(2,2));
JLabel stepLabel = new JLabel("步数");
stepText.setEditable(false);
stepText.setText(stepNumber+"步");
stepInRightPanel.add(stepLabel);
stepInRightPanel.add(stepText);
stepInRightPanel.add(t.usedTimeLabel);
stepInRightPanel.add(t.timeText);
rightPanel.add(stepInRightPanel);
/*文本区*/
JTextArea ruleTextArea = new JTextArea();
ruleTextArea.setEditable(false);
ruleTextArea.setText( "\n规则:\n"
+" 点击“开始”\n"
+"进行游戏。\n"
+"点击一个方块,\n"
+"此方块及其周围\n"
+"的方块将变色,\n"
+"所有的方块都改\n"
+"变为绿色时胜利\n"
+" 。\n "
+" ———小老头");
ruleTextArea.setFont(new Font(null,Font.BOLD,16));
rightPanel.add(ruleTextArea);
/*menuPanel*/
JPanel menuPanel = new JPanel();
quitButton.addActionListener(menuListener);
restartButton.addActionListener(menuListener);
startButton.addActionListener(menuListener);
menuPanel.add(startButton);
menuPanel.add(restartButton);
menuPanel.add(quitButton);
/*lowerPanel*/
JPanel lowerPanel = new JPanel();
JLabel newSettingLabel = new JLabel("长宽格子数(输入20内的整数)设置:");
determineButton.addActionListener(determineListener);
lowerPanel.add(newSettingLabel);
lowerPanel.add(lengthText);
lowerPanel.add(widthText);
lowerPanel.add(determineButton);
/*面板容器加到窗口里*/
add(cardsPanel,BorderLayout.CENTER);
add(rightPanel,BorderLayout.EAST);
add(menuPanel,BorderLayout.NORTH);
add(lowerPanel,BorderLayout.SOUTH);
setVisible(true);
}
}
//
/**游戏区监听者*/
class CardsListener implements ActionListener
{
MainGui gui;
public CardsListener(MainGui inGui)
{
gui = inGui;
}
public void actionPerformed(ActionEvent e)
{
CardsButton receive = (CardsButton)e.getSource();
/*见此按钮及其周围4个改变颜色*/
changeColor(receive);
changeColor(gui.cards[receive.x][receive.y+1]);
changeColor(gui.cards[receive.x][receive.y-1]);
changeColor(gui.cards[receive.x+1][receive.y]);
changeColor(gui.cards[receive.x-1][receive.y]);
gui.stepText.setText(++gui.stepNumber+"步"); //更新步数
/*判断是否所有的按钮改变颜色了*/
boolean flag = true;
judge :
for(int i=1; i<gui.length_+1; ++i)
{
for(int j=1; j<gui.width+1;++j)
{
if(gui.cards[i][j].color == 1)
{
flag = false;
break judge;
}
}
}
if(flag)
{//颜色都改变了则过关~~~
gui.t.timer.stop();
int yourChoice = JOptionPane.showConfirmDialog(null,"你成功了!牛X!\n"+
"按“是”再在一局,“否”或“取消”退出");
if(yourChoice == JOptionPane.OK_OPTION)
{
(new NewGame(gui)).restartGame();
}
else
{
System.exit(0);
}
}
}
public void changeColor(CardsButton in)
{
in.color = (in.color == 1 ? 2 : 1);
in.setBackground(in.color==1 ? Color.RED : Color.GREEN);
}
}
//
/**菜单按钮监听者*/
class MenuListener implements ActionListener
{
MainGui gui;
public MenuListener(Main
- 1
- 2
前往页