?? 貪吃蛇界面.java.bak
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class 貪吃蛇界面 extends JFrame
{
JPanel panel1,panel11,panel12 ,panelset,helppanel;
JButton btStart,btParse,btExit,btHelp;
JLabel picture1,picture2,picture3;
JLabel label1,label2,label3,label4,label5;
JTextField textField1,textField2,textField3;
JRadioButton RadioButton1,RadioButton2,RadioButton3,RadioButton4,RadioButton5,RadioButton6,RadioButton7;
ButtonGroup radioGroup;
JComboBox backBox;
String speedLevelNumber[] = {"1","2","3","4","5"};
String colorNames[] = { "藍色","黑色", "青色", "深灰色", "灰色",
"綠色", "淺灰色", "紫紅色",
"橙色", "粉紅色", "紅色", "白色", "黃色" };
Color colors[] = { Color.blue, Color.black,
Color.cyan, Color.darkGray, Color.gray, Color.green,
Color.lightGray, Color.magenta, Color.orange, Color.pink,
Color.red, Color.white, Color.yellow };
//風格
String strings[] = { "Metal", "Motif", "Windows" };
UIManager.LookAndFeelInfo looks[];
JRadioButtonMenuItem radio[];
//判斷暫停鍵是否第一次按下
boolean parse = true;
//設置按鈕
String up,DOWN,LEFT,RIGTH;
貪吃蛇界面 ()
{
final 貪吃蛇游戲界面 panel2=new 貪吃蛇游戲界面(this);
//字體
Font font1=new Font("",Font.ITALIC+Font.BOLD,15);
Font font2=new Font("",Font.ITALIC+Font.BOLD,20);
Font font3=new Font("",Font.ITALIC+Font.BOLD,25);
Font font4=new Font("",Font.ITALIC+Font.BOLD,15);
//菜單欄
JMenuBar menubar=new JMenuBar();
setJMenuBar(menubar);
//主菜單
JMenu fileMenu=new JMenu("選項");
fileMenu.setFont(font2);fileMenu.setForeground(Color.blue);
//按鍵設置菜單
fileMenu.add(new AbstractAction("按鍵設置"){
public void actionPerformed(ActionEvent event){
panelset.setVisible(true);
}
}
); //結束監聽菜單
fileMenu.addSeparator();
//網格設置
JMenu netmenu = new JMenu("網格設置");
netmenu.add(new AbstractAction("顯示網格"){
public void actionPerformed(ActionEvent event){
try{panel2.setBackground(colors[backBox.getSelectedIndex()+1]); }
catch(ArrayIndexOutOfBoundsException e)
{panel2.setBackground(Color.blue);}
}
}
); //結束監聽菜單
netmenu.add(new AbstractAction("隱藏網格"){
public void actionPerformed(ActionEvent event){
panel2.setBackground(colors[backBox.getSelectedIndex()]);
}
}
); //結束監聽菜單
fileMenu.add(netmenu);
fileMenu.addSeparator();
//風格選項
JMenu menu = new JMenu("風格");
radio = new JRadioButtonMenuItem[ strings.length ];
ButtonGroup group = new ButtonGroup();
ItemHandler handler = new ItemHandler();
for ( int count = 0; count < radio.length; count++ ) {
radio[ count ] = new JRadioButtonMenuItem( strings[ count ] );
radio[ count ].addItemListener( handler );
group.add( radio[ count ] );
menu.add( radio[ count ] );
}
looks = UIManager.getInstalledLookAndFeels();
radio[ 2 ].setSelected( true );
fileMenu.add(menu);
menubar.add(fileMenu);
//設置容器
Container container = getContentPane();
container.setLayout( new BorderLayout(5,0));
//panel1——按鈕之類部分
panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel11 = new JPanel();
panel11.setLayout(new GridLayout(2,8));
panel12 = new JPanel();
panel12.setLayout(new FlowLayout());
label1 = new JLabel("等級",SwingConstants.CENTER);
label1.setFont(font1);
panel11.add(label1);
RadioButton1 = new JRadioButton("菜鳥",false);
RadioButton1.setBackground(Color.cyan);
RadioButton1.setFont(font1);
RadioButton2 = new JRadioButton("入門",true);
RadioButton2.setBackground(Color.cyan);
RadioButton2.setFont(font1);
RadioButton3 = new JRadioButton("高手",false);
RadioButton3.setBackground(Color.cyan);
RadioButton3.setFont(font1);
RadioButton4 = new JRadioButton("專家",false);
RadioButton4.setBackground(Color.cyan);
RadioButton4.setFont(font1);
RadioButton5 = new JRadioButton("精英",false);
RadioButton5.setBackground(Color.cyan);
RadioButton5.setFont(font1);
RadioButton6 = new JRadioButton("大師",false);
RadioButton6.setBackground(Color.cyan);
RadioButton6.setFont(font1);
RadioButton7 = new JRadioButton("超人",false);
RadioButton7.setBackground(Color.cyan);
RadioButton7.setFont(font1);
radioGroup = new ButtonGroup();
radioGroup.add(RadioButton1);
radioGroup.add(RadioButton2);
radioGroup.add(RadioButton3);
radioGroup.add(RadioButton4);
radioGroup.add(RadioButton5);
radioGroup.add(RadioButton6);
radioGroup.add(RadioButton7);
RadioButton1.addActionListener(new listener(400));
RadioButton2.addActionListener(new listener(300));
RadioButton3.addActionListener(new listener(200));
RadioButton4.addActionListener(new listener(100));
RadioButton5.addActionListener(new listener(75));
RadioButton6.addActionListener(new listener(50));
RadioButton7.addActionListener(new listener(25));
panel11.add(RadioButton1);
panel11.add(RadioButton2);
panel11.add(RadioButton3);
panel11.add(RadioButton4);
panel11.add(RadioButton5);
panel11.add(RadioButton6);
panel11.add(RadioButton7);
label2 = new JLabel("得分",SwingConstants.CENTER);
label2.setFont(font1);
panel11.add(label2);
textField1 = new JTextField("0",4);
textField1.setHorizontalAlignment(textField2.CENTER);
textField1.setFont(font1);
textField1.setBackground(Color.white);
textField1.setEditable(false);
panel11.add(textField1);
label3 = new JLabel("蛇身",SwingConstants.CENTER);
label3.setFont(font1);
panel11.add(label3);
textField2 = new JTextField("0",4);
textField2.setHorizontalAlignment(textField2.CENTER);
textField2.setFont(font1);
textField2.setBackground(Color.white);
textField2.setEditable(false);
panel11.add(textField2);
label4 = new JLabel("食物",SwingConstants.CENTER);
label4.setFont(font1);
panel11.add(label4);
textField3 = new JTextField("0",4);
textField3.setHorizontalAlignment(textField2.CENTER);
textField3.setFont(font1);
textField3.setBackground(Color.white);
textField3.setEditable(false);
panel11.add(textField3);
//下拉框換背景色
label5 = new JLabel("背景",SwingConstants.CENTER);
label5.setFont(font1);
panel11.add(label5);
backBox = new JComboBox( colorNames);
backBox.setFont(font2) ;
backBox.setBackground(Color.white);
backBox.setMaximumRowCount( 5 );
//開始監聽
backBox.addItemListener(
new ItemListener() {
public void itemStateChanged( ItemEvent event )
{
if ( event.getStateChange() == ItemEvent.SELECTED )
panel2.setBackground(colors[backBox.getSelectedIndex()]);
panel2.othersColor=colors[backBox.getSelectedIndex()];
for (int i = 0 ;i <panel2.row ;i++ )
{
for (int j = 0;j<panel2.col ;j++ )
{panel2.grids[i][j].setBackground(colors[backBox.getSelectedIndex()]);
}
}
}
}
); // 結束監聽
panel11.add(backBox);
//button組件
//開始按鈕
Icon btStartPicture = new ImageIcon( "開始.gif" );
btStart = new JButton("開始",btStartPicture);
btStart.setToolTipText("開始");
btStart.setFont(font3) ;
btStart.setHorizontalTextPosition( SwingConstants.CENTER );
btStart.setVerticalTextPosition( SwingConstants.BOTTOM );
btStart.setBackground(Color.green);
btStart.setForeground(Color.red);
//監聽開始按鈕
btStart.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent e)
{
panel2.newGame(speedtime);
}
}
);
//暫停按鈕
Icon btParsePicture = new ImageIcon( "暫停.gif" );
btParse = new JButton("暫停",btParsePicture);
btParse.setToolTipText("暫停");
btParse.setFont(font3) ;
btParse.setHorizontalTextPosition( SwingConstants.CENTER );
btParse.setVerticalTextPosition( SwingConstants.BOTTOM );
btParse.setBackground(Color.green);
btParse.setForeground(Color.red)
//監聽暫停按鈕
btParse.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent e)
{
if (parse == true )
{panel2.stopGame();btParse.setText("繼續");}
if (parse == false)
{panel2.resumeGame();btParse.setText("暫停");}
parse = !parse;
}
}
);
//結束監聽
//退出按鈕
Icon btExitPicture = new ImageIcon( "討厭.gif" );
btExit = new JButton("退出",btExitPicture);
btExit.setToolTipText("退出");
btExit.setFont(font3) ;
btExit.setHorizontalTextPosition( SwingConstants.CENTER );
btExit.setVerticalTextPosition( SwingConstants.BOTTOM );
btExit.setBackground(Color.green);
btExit.setForeground(Color.red);
//開始監聽退出按鈕
btExit.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
);//結束監聽退出按鈕
//幫助按鈕
Icon btHelpPicture = new ImageIcon( "關于.gif" );
btHelp = new JButton("幫助",btHelpPicture);
btHelp.setToolTipText("幫助");
btHelp.setHorizontalTextPosition( SwingConstants.CENTER );
btHelp.setVerticalTextPosition( SwingConstants.BOTTOM );
btHelp.setFont(font3) ;
btHelp.setBackground(Color.green);
btHelp.setForeground(Color.red);
//監聽關于按鈕
btHelp.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
helppanel.setVisible(true);
}
}
);//結束監聽關于按鈕
panel12.add(btStart);
panel12.add(btParse);
panel12.add(btExit);
panel12.add(btHelp);
panel11.setBackground(Color.cyan);
panel12.setBackground(Color.cyan);
Icon snakePicture = new ImageIcon( "002.jpg" );
picture1 = new JLabel(snakePicture);
picture2= new JLabel(snakePicture);
//panelset
panelset = new JPanel();
panelset.setLayout(new GridLayout(2,2));
panelset.setBackground(Color.blue);
JPanel panelset1 = new JPanel(new GridLayout(1,4));
JPanel panelset2 = new JPanel(new GridLayout(1,4));
JLabel labelset1 = new JLabel("設置上 : ");
JLabel labelset2 = new JLabel("設置下 : ");
JLabel labelset3 = new JLabel("設置左 : ");
JLabel labelset4 = new JLabel("設置右 : ");
final JTextField textFieldset1 = new JTextField();
//監聽
textFieldset1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
up = textFieldset1.getText();
}
}
);
JTextField textFieldset2 = new JTextField();//監聽
JTextField textFieldset3 = new JTextField();//監聽
JTextField textFieldset4 = new JTextField();//監聽
JButton buttonset1 = new RoundButton("重新設置");//監聽
JButton buttonset2 = new RoundButton("確定設置");
//監聽
buttonset2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panelset.setVisible(false);
//未完
}
});
//監聽結束
//加入容器
panelset1.add(labelset1);panelset1.add(textFieldset1);
panelset1.add(labelset2);panelset1.add(textFieldset2);
panelset2.add(labelset3);panelset2.add(textFieldset3);
panelset2.add(labelset4);panelset2.add(textFieldset4);
panelset.add(panelset1);panelset.add(panelset2);
panelset.add(buttonset1);panelset.add(buttonset2);
panelset1.setBackground(Color.green);panelset2.setBackground(Color.green);
buttonset1.setBackground(Color.green);buttonset2.setBackground(Color.green);
buttonset1.setForeground(Color.blue);buttonset2.setForeground(Color.blue);
labelset1.setFont(font4);labelset3.setFont(font4);
labelset3.setFont(font4);labelset4.setFont(font4);
buttonset1.setFont(font4);buttonset2.setFont(font4);
panelset.setVisible(false);
panel1.add(panel11,BorderLayout.NORTH);
panel1.add(panel12,BorderLayout.CENTER);
panel1.add(picture1,BorderLayout.WEST);
panel1.add(picture2,BorderLayout.EAST);
panel1.add(panelset,BorderLayout.SOUTH);
//幫助按鈕要實現的地方
helppanel = new JPanel( new BorderLayout(20,0));
helppanel.setBackground(Color.green);
String instruction = "游戲說明:\n1 :方向鍵控制蛇移動的方向!"+
"\n2 :按開始鍵開始游戲!"+
"\n3 :按暫停鍵可以暫停游戲,再按暫停鍵能繼續玩游戲!";
JTextArea helpArea = new JTextArea(instruction,5,30);
helpArea.setFont(font4);helpArea.setBackground(Color.cyan);helpArea.setForeground(Color.blue);
helpArea.setEditable(false);
helppanel.add( new JScrollPane( helpArea ),BorderLayout.CENTER);
Icon btOKPicture = new ImageIcon( "比卡丘.gif" );
JButton btOK = new JButton("懂了!快讓我玩!",btOKPicture);
btOK.setHorizontalTextPosition( SwingConstants.CENTER );
btOK.setVerticalTextPosition( SwingConstants.BOTTOM );
btOK.setFont(font2);btOK.setBackground(Color.cyan);btOK.setForeground(Color.red);
//監聽確定鍵
btOK.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
helppanel.setVisible(false);
}
}
);//結束監聽
helppanel.setVisible(false);
helppanel.add( btOK,BorderLayout.EAST);
//panel2——游戲界面
panel2.setBackground(Color.blue);
//加入容器
container.add(panel1,BorderLayout.SOUTH);
container.add(panel2,BorderLayout.CENTER);
container.add(helppanel,BorderLayout.NORTH);
}
//風格
private void changeTheLookAndFeel( int value )
{
try {
UIManager.setLookAndFeel(
looks[ value ].getClassName() );
SwingUtilities.updateComponentTreeUI( this );
}
catch ( Exception exception ) {
exception.printStackTrace();
}
}
private class ItemHandler implements ItemListener {
public void itemStateChanged( ItemEvent event )
{
for ( int count = 0; count < radio.length; count++ )
if ( radio[ count ].isSelected() ) {
changeTheLookAndFeel( count );
}
}
}
//速度
private long speedtime = 200;
private class listener implements ActionListener{
private long pause;
listener(long pause){
if ( speedtime > 25 )
{ this.pause = pause; }
else{ pause = 25;}
}
public void actionPerformed(ActionEvent e){
speedtime=pause;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -