下面是关于俄罗斯方块游戏的控制面板和消息接收框界面的实现,有助于我们
1, 掌握组件基本创建方法;
2, 掌握窗口的实现机制;
3, 建立俄罗斯方块游戏控制面板类;
4, 建立俄罗斯方块的消息接收面板类;
5, 实现对这两个类的测试。
具体代码如下:
1)
/**
*作 者: Helloway
*类 名:CGlobal
*类说明:游戏的全局信息
*/
public class CGlobal {
public static final int Block_Rows=4; //方块的行数
public static final int Block_Cols=4; //方块的列数
public static final int DEFAULT_LEVEL=5; //默认的游戏等级
public static final int MAXLEVEL=10;//最高的游戏等级
public static final int Block_KIND_NUM=7;//方块的7种种类
public static final int Block_STATES_NUM=4;//方块的4种状态
public static final Border border = new EtchedBorder(
EtchedBorder.RAISED, Color.WHITE, new Color(100,150,255));//边的样式
public static final void showCenter(Frame frame) {//设置窗口居中
int x= (Toolkit.getDefaultToolkit().getScreenSize().width-frame.getSize().width)/2;
int y= (Toolkit.getDefaultToolkit().getScreenSize().height-frame.getSize().height)/2;
frame.setLocation(x,y);
}
}
2)
/**
*作 者: Helloway
*类 名:CControlPanel
*类说明:
*信息控制主面板
*/
public class CControlPanel extends JPanel{
private InfoPanel infoPanel;
private ConButtonPanel conButtonPanel;
private TipPanel tipPanel;
public CControlPanel(){
tipPanel=new TipPanel();
infoPanel=new InfoPanel();
conButtonPanel=new ConButtonPanel();
this.setLayout(new GridLayout(3,1,0,4));
this.add(tipPanel);
this.add(infoPanel);
this.add(conButtonPanel);
this.setBorder(CGlobal.border);
}
}
/**
*作 者: Helloway
*类 名:TipPanel
*类说明:
*方块提示面板
*/
class TipPanel extends JPanel{
private JLabel tipLabel;
private TipBlock tipBlock;
public TipPanel() {
tipLabel=new JLabel("下一个方块");
tipBlock=new TipBlock();
this.add(tipLabel);
this.add(tipBlock);
this.setBorder(CGlobal.border);
}
private class TipBlock extends JPanel{
public TipBlock() {
super();
}
}
}
/**
*作 者: Helloway
*类 名:InfoPanel
*类说明:
*信息显示面板
*/
class InfoPanel extends JPanel{
private JLabel lDiff,lScore;
private JTextField tfScore,tfLevel;
public InfoPanel() {
lDiff=new JLabel("难度等级");
lScore=new JLabel("得分");
tfLevel=new JTextField("5");
tfLevel.setEditable(false);
tfScore=new JTextField("0");
tfScore.setEditable(false);
this.setLayout(new GridLayout(4,1));
this.add(lDiff);this.add(tfLevel);
this.add(lScore);this.add(tfScore);
this.setBorder(CGlobal.border);
}
}
/**
*作 者: Helloway
*类 名:ConButtonPanel
*类说明:
*按钮控制面板
*/
class ConButtonPanel extends JPanel{
private JButton btPlay,btPause,btStop;
public ConButtonPanel(){
btPlay=new JButton("开始");
btPause=new JButton("暂停");
btStop=new JButton("结束");
this.setLayout(new GridLayout(3,1));
this.add(btPlay);
this.add(btPause);
this.add(btStop);
this.setBorder(CGlobal.border);
}
}
3)
/**
*作 者: Helloway
*类 名:CMessagePanel
*类说明:消息面板
*/
public class CMessagePanel extends JPanel {
private JTextArea infoWinArea;
public CMessagePanel() {
infoWinArea=new JTextArea(3,30);
infoWinArea.setEditable(false);
this.add(infoWinArea);
this.setBorder(CGlobal.border);
}
}
4)
/**
*作 者:Helloway
*类 名:TextMyGame
*类说明:用户创建窗口开始游戏及测试其他的类的正确性
*/
public class TestMyGame {
public static void main(String[] args) {
JFrame myFrame=new JFrame("我的俄罗斯方块");
CControlPanel myControlPanel = new CControlPanel();
CMessagePanel myMsgPanel= new CMessagePanel();
Container con=myFrame.getContentPane();
con.add(myControlPanel,BorderLayout.EAST);
con.add(myMsgPanel,BorderLayout.SOUTH);
myFrame.setSize(400, 500);//设置窗口的大小
CGlobal.showCenter(myFrame);//设置窗口居中
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
最终的界面效果展示如下: