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

📄 hanopanel.java

📁 该游戏软件在Windows系统上为玩家提供汉偌塔金片的移动的基本功能
💻 JAVA
字号:
package hannota;

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.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class HanoPanel extends JPanel implements ActionListener {

	JPanel southPanel = new JPanel();

	JPanel northPanel = new JPanel();

	JPanel centerPanel = new JPanel();

	JButton exit = new JButton("返回");

	JButton reset = new JButton("重置");

	JButton config = new JButton("设置等级");

	JLabel westLabel = new JLabel("坚持到底永不放弃");

	JLabel eastLabel = new JLabel("坚持到底永不放弃");

	// 定义面板数组
	int n = 30;

	JPanel[] hanoPanel = new JPanel[n];

	// 定义提示标签
	JLabel prompt = new JLabel("祝您游戏愉快!");

	// 定义时间标签
	JLabel lastTime = new JLabel("游戏时间");

	// 定义一个变量用于接受
	int level = 3;

	// 创建塔中的面板
	JLabel[] dish = new JLabel[9];

	// 鼠标事件
	HanoMouseEventf hanomouseevent;

	CardLayout card;

	JPanel fatherPanel;

	HanoActionEvent hanoactionevent;

	// 得到时间
	JLabel timelabel;

	int bushu = 0;

	JLabel bushulabel;

	public HanoPanel(CardLayout card, JPanel fatherPanel) {
		this.card = card;
		this.fatherPanel = fatherPanel;
		start();
	}

	private void start() {
		this.setBackground(Color.magenta);
		this.setLayout(new BorderLayout());
		createNouthPanel();
		southPanel.setBackground(Color.CYAN);
		// 创建南方的面板
		createSouthPanel();
		// 中间面板
		createCenterPanel();
		// 创建鼠标事件
		createMouse();
		// 创建按钮事件
		createHanoEvent();
		// 创建Label标签
		createDishes();
		// 添加多少个盘子
		addDishes(3);
		eastLabel.setBackground(Color.LIGHT_GRAY);
		westLabel.setBackground(Color.LIGHT_GRAY);
		this.add(eastLabel, BorderLayout.EAST);
		this.add(westLabel, BorderLayout.WEST);
		this.add(northPanel, BorderLayout.NORTH);
		this.add(southPanel, BorderLayout.SOUTH);
		this.add(centerPanel, BorderLayout.CENTER);
	}

	// 创建最下方的按钮的事件
	private void createHanoEvent() {
		hanoactionevent = new HanoActionEvent(this);
		reset.addActionListener(hanoactionevent);
		config.addActionListener(hanoactionevent);
	}

	// 将鼠标事件添加到柱子上面
	private void createMouse() {
		hanomouseevent = new HanoMouseEventf(this);
		// 对三个柱子进行事件的兼听
		hanoPanel[0].addMouseListener(hanomouseevent);
		hanoPanel[10].addMouseListener(hanomouseevent);
		hanoPanel[20].addMouseListener(hanomouseevent);

	}

	// 添加盘子的个数
	protected void addDishes(int q) {
		int p = q - 1;
		for (int i = 9; i > 9 - q; i--) {
			hanoPanel[i].add(dish[p]);
			p--;
		}
	}

	// 盘子的数量
	private void createDishes() {

		for (int j = 0; j < 9; j++) {
			// 事例标签
			dish[j] = new JLabel();
			// 设置盘子的大小
			dish[j].setSize(j * 5 + 10, 10);
			dish[j].setPreferredSize(new Dimension(j * 10 + 10, 20));
			// 背景颜色
			dish[j].setBackground(Color.blue);
			// 绘制主键的所以相数
			dish[j].setOpaque(true);
		}
	}               

	private void createCenterPanel() {
		int index = 0;
		// 在面板上添加小面板,
		centerPanel.setLayout(new GridLayout(0, 3, 60, 0));
		for (int i = 0; i < 30; i++) {
			hanoPanel[i] = new JPanel();
			// 判断是否为放置最小面板的面板
			if (i % 10 == 0) {
				index = i;
				hanoPanel[index].setBackground(Color.cyan);
				centerPanel.add(hanoPanel[index]);
			} else {
				// 汉诺塔的塔柱,及边框颜色
				hanoPanel[i].setBorder(BorderFactory
						.createLineBorder(Color.red));
				// 设置塔柱的布局管理器,采用网格布局
				hanoPanel[index].setLayout(new GridLayout(9, 1, 0, 0));
				// 添加小面板
				hanoPanel[index].add(hanoPanel[i]);
			}
		}
	}

	// 北方面板用于添加标签
	private void createNouthPanel() {

		// 获取系统日期
		Calendar cal = new GregorianCalendar();

		int y = cal.get(Calendar.YEAR);
		int m = cal.get(Calendar.MARCH);
		int d = cal.get(Calendar.DATE);
		// 获取系统的时间
		int hour = cal.get(Calendar.HOUR);
		int second = cal.get(Calendar.SECOND);
		int minute = cal.get(Calendar.MINUTE);
                int mm=m+1;
		JLabel timelabel = new JLabel("" + y + "年" + mm + "月" + d + "日" + "");
		JLabel timeday = new JLabel("" + hour + ":" + second + ":" + minute
				+ ":" + "");
		// 计算步数的标签
//		bushulabel = new JLabel();

		FlowLayout mgr = new FlowLayout(FlowLayout.LEFT);
		northPanel.setLayout(mgr);
		prompt.setBackground(Color.magenta);
		northPanel.setBackground(Color.magenta);
		
		northPanel.add(prompt);
		northPanel.add(timelabel);
		northPanel.add(timeday);

	}

	// 南方面板用于添加按钮
	private void createSouthPanel() {
		southPanel.setLayout(new GridLayout(1, 3, 0, 0));
		exit.setSize(100, 30);
		// 退出的快捷方式
		exit.setMnemonic(KeyEvent.VK_F);
		exit.setActionCommand("exit");
		// 注册按钮的关闭事件
		exit.addActionListener(this);
		southPanel.add(config);
		southPanel.add(reset);
		southPanel.add(exit);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == exit) {
			card.show(fatherPanel, "navigatapanel");
		}
	}

}

⌨️ 快捷键说明

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