您的位置:首页 > 编程语言 > Java开发

java吃豆豆游戏-1控制按钮编写

2013-05-25 21:28 483 查看
个人习惯,先写按钮

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;

public class GameController extends JPanel implements ActionListener {

private JButton exitButton = new JButton("Exit");
private JButton settingsButton = new JButton ("Settings");
private JButton aboutButton = new JButton ("About");
private JButton keysButton = new JButton ("Keys");
private JLabel label = new JLabel ("     0     ");

private Background bg;

public GameController (Background bg){
super(new FlowLayout(FlowLayout.CENTER));
this.bg = bg;
setOpaque(false);//设置组件透明

exitButton.addActionListener(this);
settingsButton.addActionListener(this);
aboutButton.addActionListener(this);
keysButton.addActionListener(this);

//setBorder() 是使用Border 边框 ,Border 是特殊的Swing组件,为Swing组件提供不同的边框修饰
label.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));

add(label);
add(exitButton);
add(aboutButton);
add(settingsButton);
add(keysButton);

}

public JLabel getLabel(){
return label;
}

public void actionPerformed (ActionEvent e){

if (e.getSource()==exitButton){
System.exit(0);

}

else if (e.getSource()==settingsButton){

}

else if (e.getSource()==aboutButton){
JOptionPane.showMessageDialog(getParent(),  "Eater Version 1.1" +
"\nProgrammed by K.I.K");

}

else if (e.getSource()==keysButton){
JOptionPane.showMessageDialog(getParent(),  "Use arrow keys to move the eater");
}

else return;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: