⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 贪吃蛇界面.java.bak

📁 贪吃蛇的游戏源码。挺不错的。
💻 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 + -