Hangman游戏,请大家指点

package cn.hangman;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class HangMan extends JFrame {

	MyPanel mp = new MyPanel();

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HangMan hm = new HangMan();
	}

	public HangMan() {
		this.setTitle("HangMan");
		this.setSize(300, 380);
		this.add(mp);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setAlwaysOnTop(true);
		this.setResizable(false);
		this.setVisible(true);
	}
}

class MyPanel extends JPanel implements ActionListener {

	char[] world = "world".toCharArray();
	LetterPanel lp;
	WordPanel wp;
	int temp1 = 0;
	int temp2 = 0;

	public int getTemp2() {
		return temp2;
	}

	public void setTemp2(int temp2) {
		this.temp2 = temp2;
	}

	public MyPanel() {
		this.lp = new LetterPanel();
		this.wp = new WordPanel();
		this.setLayout(new BorderLayout());
		this.setSize(300, 380);
		this.add(lp, BorderLayout.NORTH);
		this.add(wp, BorderLayout.CENTER);
		for (int i = 0; i < 26; i++) {
			lp.jb[i].addActionListener(this);
		}
	}

	// 覆写paint方法
	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.black);
		g.fillRect(0, 172, 300, 170);
		g.setColor(Color.white);
		g.drawLine(40, 324, 110, 324);
		if (this.getTemp1() == 1) {
			this.paint1(g);
		} else if (this.getTemp1() == 2) {
			this.paint1(g);
			this.paint2(g);
		} else if (this.getTemp1() == 3) {
			this.paint1(g);
			this.paint2(g);
			this.paint3(g);
		} else if (this.getTemp1() == 4) {
			this.paint1(g);
			this.paint2(g);
			this.paint3(g);
			this.paint4(g);
		} else if (this.getTemp1() == 5) {
			this.paint1(g);
			this.paint2(g);
			this.paint3(g);
			this.paint4(g);
			this.paint5(g);
		} else if (this.getTemp1() == 6) {
			this.paint1(g);
			this.paint2(g);
			this.paint3(g);
			this.paint4(g);
			this.paint5(g);
			this.paint6(g);
		} else if (this.getTemp1() == 7) {
			this.paint1(g);
			this.paint2(g);
			this.paint3(g);
			this.paint4(g);
			this.paint5(g);
			this.paint6(g);
			this.paint7(g);
		} else if (this.getTemp1() == 8) {
			this.paint1(g);
			this.paint2(g);
			this.paint3(g);
			this.paint4(g);
			this.paint5(g);
			this.paint6(g);
			this.paint7(g);
			this.paint8(g);
		} else if (this.getTemp1() == 9) {
			this.paint1(g);
			this.paint2(g);
			this.paint3(g);
			this.paint4(g);
			this.paint5(g);
			this.paint6(g);
			this.paint7(g);
			this.paint8(g);
			this.paint9(g);
		} else if (this.getTemp1() == 10) {
			this.paint1(g);
			this.paint2(g);
			this.paint3(g);
			this.paint4(g);
			this.paint5(g);
			this.paint6(g);
			this.paint7(g);
			this.paint8(g);
			this.paint9(g);
			this.paint10(g);
		} else if (this.temp2 == 5) {
			this.paint11(g);
		}
	}

	public void paint1(Graphics g) {
		g.setColor(Color.white);
		g.drawLine(40, 324, 40, 190);
	}

	public void paint2(Graphics g) {
		g.setColor(Color.white);
		g.drawLine(40, 190, 110, 190);
	}

	public void paint3(Graphics g) {
		g.setColor(Color.white);
		g.drawLine(40, 210, 60, 190);
	}

	public void paint4(Graphics g) {
		g.setColor(Color.white);
		g.drawLine(110, 190, 110, 210);
	}

	public void paint5(Graphics g) {
		g.setColor(Color.white);
		g.drawOval(95, 210, 30, 30);
	}

	public void paint6(Graphics g) {
		g.setColor(Color.white);
		g.drawLine(110, 240, 110, 280);
	}

	public void paint7(Graphics g) {
		g.setColor(Color.white);
		g.drawLine(110, 260, 80, 275);
	}

	public void paint8(Graphics g) {
		g.setColor(Color.white);
		g.drawLine(110, 260, 140, 275);
	}

	public void paint9(Graphics g) {
		g.setColor(Color.white);
		g.drawLine(110, 280, 80, 295);
	}

	public void paint10(Graphics g) {
		g.setColor(Color.white);
		g.drawLine(110, 280, 140, 295);
		g.setFont(new Font("宋体", Font.BOLD, 15));
		g.drawString("努力,奋斗", 160, 230);
		g.drawString("你输了,请退出", 160, 260);
	}

	public void paint11(Graphics g) {
		g.setColor(Color.white);
		g.setFont(new Font("宋体", Font.BOLD, 15));
		g.drawString("恭喜你", 160, 230);
		g.drawString("你赢了,请退出", 160, 260);
	}

	public int getTemp1() {
		return temp1;
	}

	public void setTemp1(int temp1) {
		this.temp1 = temp1;
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand().equals(String.valueOf(this.world[0]))) {
			for (int i = 0; i < 26; i++) {
				if (e.getSource() == this.lp.jb[i]) {
					this.lp.remove(this.lp.jb[i]);
					this.setTemp2(this.getTemp2() + 1);
					break;
				}
			}
			this.repaint();
			this.wp.jl[0].setText(String.valueOf(this.world[0]) + " ");
		} else if (e.getActionCommand().equals(String.valueOf(this.world[1]))) {
			int i;
			a: for (i = 0; i < 26; i++) {
				if (e.getSource() == this.lp.jb[i]) {
					this.lp.remove(this.lp.jb[i]);
					this.setTemp2(this.getTemp2() + 1);
					break a;
				}
			}
			this.repaint();
			this.wp.jl[1].setText(String.valueOf(this.world[1]) + " ");
		} else if (e.getActionCommand().equals(String.valueOf(this.world[2]))) {
			for (int i = 0; i < 26; i++) {
				if (e.getSource() == this.lp.jb[i]) {
					this.lp.remove(this.lp.jb[i]);
					this.setTemp2(this.getTemp2() + 1);
					break;
				}
			}
			this.repaint();
			this.wp.jl[2].setText(String.valueOf(this.world[2]) + " ");
		} else if (e.getActionCommand().equals(String.valueOf(this.world[3]))) {
			for (int i = 0; i < 26; i++) {
				if (e.getSource() == this.lp.jb[i]) {
					this.lp.remove(this.lp.jb[i]);
					this.setTemp2(this.getTemp2() + 1);
					break;
				}
			}
			this.wp.jl[3].setText(String.valueOf(this.world[3]) + " ");
			this.repaint();
		} else if (e.getActionCommand().equals(String.valueOf(this.world[4]))) {
			for (int i = 0; i < 26; i++) {
				if (e.getSource() == this.lp.jb[i]) {
					this.lp.remove(this.lp.jb[i]);
					this.setTemp2(this.getTemp2() + 1);
					break;
				}
			}
			this.repaint();
			this.wp.jl[4].setText(String.valueOf(this.world[4]) + " ");
		} else {
			for (int k = 0; k < 26; k++) {
				if (e.getSource() == this.lp.jb[k])
					this.lp.remove(this.lp.jb[k]);
			}
			this.setTemp1(this.getTemp1() + 1);
			this.repaint();
		}
	}
}

class LetterPanel extends JPanel {

	JButton[] jb = new JButton[26];
	JLabel[] jl;

	public LetterPanel() {
		this.jl = new JLabel[5];
		this.setLayout(new GridLayout(5, 6));
		for (int i = 97; i <= 122; i++) {
			jb[i - 97] = new JButton(String.valueOf((char) i));
		}
		for (int i = 97; i <= 122; i++) {
			this.add(jb[i - 97]);
		}
	}
}

class WordPanel extends JPanel {
	JLabel[] jl = new JLabel[5];

	public WordPanel() {
		for (int i = 0; i < 5; i++) {
			this.jl[i] = new JLabel("_" + " ");
		}
		for (int j = 0; j < 5; j++) {
			this.add(this.jl[j]);
		}
	}
}
不明白如何在按了按钮后继续画图,并不是调用repaint()函数,还有为什么我点对了删除按钮,按钮会重新排版,而按错了就会重新排版
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值