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

📄 hanopanel.java

📁 一个游戏。是用java基础写的一个游戏。供给java初学者下载
💻 JAVA
字号:
package com.Luoweixun;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class HanoPanel extends JPanel{	
	/**
	 * 游戏主界面
	 */
	private static final long serialVersionUID = 1L;
	JPanel northPanel=new JPanel();
	JPanel centerPanel=new JPanel();
	JPanel southPanel=new JPanel();		
	CardLayout cardLayout=null;
	JPanel fatherPanel=null;
	HanoMouseEvent hme;
	int Level=3;
	int sum=0;
	JButton reset=new JButton("重置游戏");
	JButton config=new JButton("设置等级");
	JButton exits=new JButton("退出游戏");
	JLabel text=new JLabel("步数",SwingConstants.CENTER);
	JLabel prompt=new JLabel("游戏开始!加油!",SwingConstants.LEFT);
	JLabel text1=null;
	JLabel dish[]=new JLabel[9];	
	JPanel hanoPane[]=new JPanel[30];
	HanoActionEvent Hanoactionevent;


	
//	创建构造方法
	  HanoPanel(JPanel fatherPanel, CardLayout cardLayout){
		  this.fatherPanel=fatherPanel;
		  this.cardLayout=cardLayout;
		  createInterface();
		  createCenterPanel();
		  createNorthPanel();
		  createSouthPanel();
		  createHanoPanel();
		  addHanoPane();
		  addToContainer();
		  createDishes();
		  addDishes(Level);
		  addMouseE();
		  removeMouseE();

	}
//	  创建游戏界面
	public void createInterface(){
		setLayout(new BorderLayout());
		Hanoactionevent=new HanoActionEvent(this,fatherPanel,cardLayout);
	}
//	创建北边面板
	public void createNorthPanel(){
		text1=new JLabel("等级:"+Level);
		northPanel.setLayout(new BorderLayout());
		northPanel.add(text,BorderLayout.CENTER);
		northPanel.add(prompt,BorderLayout.WEST);
		northPanel.add(text1,BorderLayout.EAST);
		northPanel.setBorder(BorderFactory.createLineBorder(Color.red));
		}
//	创建中间面板
	public void createCenterPanel(){		
		centerPanel.setBorder(BorderFactory.createLineBorder(Color.red));
		centerPanel.setLayout(new FlowLayout(FlowLayout.CENTER,70,15));	
	}
//	创建南边面板
	public  void createSouthPanel(){
		southPanel.add(reset);
		southPanel.add(config);		
		southPanel.add(exits);
		
		exits.addActionListener(Hanoactionevent);
		config.addActionListener(Hanoactionevent);
		reset.addActionListener(Hanoactionevent);
		
		southPanel.setBorder(BorderFactory.createLineBorder(Color.red));
		
	}
//	创建中间面板包含30个面板
	public void createHanoPanel(){
		int k=0;
		for(int i=0;i<hanoPane.length;i++){
			hanoPane[i]=new JPanel();	
			if(i%10==0){
				k=i;
				hanoPane[k].setLayout(new GridLayout(9,1));
				hanoPane[k].setPreferredSize(new Dimension(100,300));		
		
			}		
			else{
				hanoPane[k].add(hanoPane[i]);
				hanoPane[i].setLayout(new FlowLayout(FlowLayout.CENTER,20,9));
				hanoPane[i].setBorder(BorderFactory.createLineBorder(Color.blue));	
			}
		}	
	}
//	添加面板到中间面板中
	public void addHanoPane(){
		for(int i=0;i<hanoPane.length;i++){
			if(i%10==0){
			centerPanel.add(hanoPane[i]);	
			}
		}	
	}
//	添加北,中,南面板到边界布局的容器面板中
	public void addToContainer(){
		add(northPanel,BorderLayout.NORTH);
		add(centerPanel,BorderLayout.CENTER);
		add(southPanel,BorderLayout.SOUTH);
	}
//	创建9个盘子
	public void createDishes(){
		for(int i=0;i<9;i++){
			dish[i]=new JLabel();
			dish[i].setOpaque(true);
			dish[i].setBackground(Color.blue);
		}
	}
//	添加盘子到塔柱面板中,n代表等级
	public void addDishes(int n){
		int N=n; Level=n;
		for(int i=1;i<10;i++){
			hanoPane[i].updateUI();
			hanoPane[i].removeAll();
		}
		for(int i=9;i>9-n;i--){
			hanoPane[i].updateUI();
			hanoPane[i].add(dish[N-1],BorderLayout.CENTER);
			dish[N-1].setPreferredSize(new Dimension(9+(N*9),18));
			N--;
		}
	}
// 添加监听	
//	中间面板添加鼠标鼠标事件监听
	public void addMouseE(){
		hme=new HanoMouseEvent(this);
		for(int i=0;i<hanoPane.length;i++){
			if(i%10==0){
			hanoPane[i].addMouseListener(hme);		
			}
		}
	}
//	从中间面板中移走鼠标事件监听
	public void removeMouseE(){
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -