62,615
社区成员
发帖
与我相关
我的任务
分享
class gameCtrlPanel extends JPanel
{
JButton musicButton=new JButton();//控制音乐开关按钮
JButton helpButton=new JButton();//帮助
JButton toMenuButton=new JButton();//返回主菜单按钮
JButton exitGameButton=new JButton();//游戏退出按钮
JButton playAgainButton=new JButton();//重玩按钮
JButton poseGameButton=new JButton();//暂停游戏按钮
JButton upButton=new JButton();//上一关
JButton downButton=new JButton();//下一关
JPanel previewPanel=new JPanel();//方块预览框
JLabel showStation=new JLabel();//显示等级和分数
GridBagLayout gameCtrlGbl=new GridBagLayout();
GridBagConstraints gbc2=new GridBagConstraints();
gameCtrlPanel(){
setLayout(gameCtrlGbl);
gbc2.insets=new Insets(10,10,10,10);
gbc2.fill=GridBagConstraints.BOTH;
/*.......构造musicButton...........*/
gbc2.weightx=1;
gbc2.weighty=1;
gbc2.gridwidth=1;
gbc2.gridheight=1;
// musicButton.setBorder(null);
// musicButton.setOpaque(false);
// musicButton.setContentAreaFilled(false);
addComponent(musicButton,0,0);
/*....*/
JLabel lblA=new JLabel();
gbc2.weightx=1.5;
addComponent(lblA,1,0);
/*构造helpButton*/
gbc2.weightx=1;
gbc2.gridwidth=GridBagConstraints.REMAINDER;
helpButton.setBorder(null);
helpButton.setOpaque(false);
helpButton.setContentAreaFilled(false);
addComponent(helpButton,2,0);
// /*....*/
// JLabel lblB=new JLabel();
// gbc2.weightx=1;
// gbc2.weighty=4;
// addComponent(lblB,0,1);
/*构造previewPanel*/
gbc2.weightx=1;
gbc2.weighty=4;
gbc2.gridwidth=GridBagConstraints.REMAINDER;
previewPanel.setOpaque(false);
addComponent(previewPanel,0,1);
// /*...*/
// JLabel lblC=new JLabel();
//
// addComponent(lblC,2,1);
//
/*构造showStation*/
gbc2.weighty=3.5;
addComponent(showStation,0,2);
/*构造upButton、poseGameButton、downButton*/
gbc2.weightx=1;
gbc2.weighty=1.5;
gbc2.gridwidth=1;
upButton.setBorder(null);
upButton.setOpaque(false);
upButton.setContentAreaFilled(false);
addComponent(upButton,0,3);
poseGameButton.setBorder(null);
poseGameButton.setOpaque(false);
poseGameButton.setContentAreaFilled(false);
addComponent(poseGameButton,1,3);
downButton.setBorder(null);
downButton.setOpaque(false);
downButton.setContentAreaFilled(false);
addComponent(downButton,2,3);
JLabel lbl_1=new JLabel();
gbc2.gridwidth=GridBagConstraints.REMAINDER;
addComponent(lbl_1,3,3);
/*构造palyAgainButton、toMenuButton、exitGameButton*/
gbc2.weightx=1;
gbc2.weighty=1.5;
gbc2.gridwidth=1;
gbc2.gridheight=1;
playAgainButton.setBorder(null);
playAgainButton.setOpaque(false);
playAgainButton.setContentAreaFilled(false);
addComponent(playAgainButton,0,4);
toMenuButton.setBorder(null);
toMenuButton.setOpaque(false);
toMenuButton.setContentAreaFilled(false);
addComponent(toMenuButton,1,4);
exitGameButton.setBorder(null);
exitGameButton.setOpaque(false);
exitGameButton.setContentAreaFilled(false);
addComponent(exitGameButton,2,4);
JLabel lbl_2=new JLabel();
gbc2.gridwidth=GridBagConstraints.REMAINDER;
addComponent(lbl_2,3,4);
}
public void addComponent(Component c,int col,int row ){
gbc2.gridx=col;//组件定位
gbc2.gridy=row;
gameCtrlGbl.setConstraints(c,gbc2);
add(c);
}
}
//游戏界面
class panelTwo extends JPanel implements KeyListener {
elspanel gameArea = new elspanel();// 游戏区
gameCtrlPanel gameCtrl = new gameCtrlPanel();// 游戏控制区
GridBagLayout panelTwoLayout = new GridBagLayout();
GridBagConstraints gbc1 = new GridBagConstraints();
panelTwo() {
addKeyListener(this);
setLayout(panelTwoLayout);
gbc1.insets=new Insets(1,10,1,5);
gbc1.fill=GridBagConstraints.BOTH;
JLabel lbl1=new JLabel();
gbc1.weightx=1;
gbc1.weighty=1;
gbc1.gridwidth=GridBagConstraints.REMAINDER;
gbc1.gridheight=1;
addComponent(lbl1,0,0);
JLabel lbl4=new JLabel();
gbc1.weightx=1;
gbc1.weighty=7.5;
gbc1.gridwidth=1;
gbc1.gridheight=2;
addComponent(lbl4,0,1);
/*............构造gameArea游戏区面板.........*/
gbc1.weightx=10.5;
gbc1.weighty=7.5;
gbc1.gridwidth=1;
gbc1.gridheight=1;
addComponent(gameArea,1,1);
Thread thread=new Thread(gameArea);
thread.start();
// gameArea.setOpaque(false);
//gameArea.setBackground(Color.red);
gbc1.weightx=1.5;
// gbc1.gridx=1;
gbc1.gridheight=1;
JLabel lbl3=new JLabel();
addComponent(lbl3,2,1);
/*...........构造gameCtrl游戏区面板..........*/
gbc1.weightx=5.2;
gbc1.gridheight=1;
addComponent(gameCtrl,3,1);
// gameCtrl.setOpaque(false);
// gameCtrl.setBackground(Color.green);//
JLabel lbl5=new JLabel();
gbc1.weightx=5.6;
gbc1.gridwidth=GridBagConstraints.REMAINDER;
gbc1.gridheight=1;
addComponent(lbl5,4,1);
JLabel lbl2=new JLabel();
gbc1.weighty=0.6;
gbc1.gridwidth=GridBagConstraints.REMAINDER;
gbc1.gridheight=1;
addComponent(lbl2,0,2);
}
public void addComponent(Component c, int col, int row) {
gbc1.gridx = col;// 组件定位
gbc1.gridy = row;
panelTwoLayout.setConstraints(c, gbc1);
add(c);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_RIGHT:
if (gameArea.currentShape.canMoveRight())
gameArea.currentShape.MoveRight();
gameArea.repaint();
break;
case KeyEvent.VK_LEFT:
if (gameArea.currentShape.canMoveLeft())
gameArea.currentShape.MoveLeft();
gameArea.repaint();
break;
case KeyEvent.VK_DOWN:
gameArea.time = 50;
gameArea.repaint();
break;
case KeyEvent.VK_UP:
if (gameArea.currentShape.canTurn())
gameArea.currentShape.turn();
gameArea.repaint();
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
int key = e.getKeyCode();
if (key == KeyEvent.VK_DOWN)
gameArea.time = 500;
}
}
public class els extends JFrame implements ActionListener {
music mymusic = null;
panelTwo game=null;
public els() {
// panelOne control=new panelOne(){
// protected void paintComponent(Graphics g) {
// ImageIcon icon = new ImageIcon(
// "D:\\projects\\MyEls\\src\\panelOne.jpg");
// Image img = icon.getImage();
// g.drawImage(img, 0, 0, this.getSize().width,
// this.getSize().height, this);
// };
// };
panelTwo game=new panelTwo(){
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon(
"D:\\projects\\MyEls\\src\\panelTwo.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, this.getSize().width,
this.getSize().height, this);
};
};
mymusic = new music();
Container container = this.getContentPane();
mymusic.start();
container.add(game);
addKeyListener(game);
game.gameCtrl.musicButton.addActionListener(this);
}
public static void main(String[] args) throws Exception {
els f = new els();
f.setSize(850, 637);
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==game.gameCtrl.musicButton)
{
try {
mymusic.sleep(10000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
}
}
}
}
这只是代码的一部分。。