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

📄 caixianshou.java

📁 JAVA 写的五子棋
💻 JAVA
字号:
import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

public class CaiXianShou extends JFrame implements ActionListener {
	public JButton b1, b2, b3, b4;

	JFrame f;

	static int i;

	ImageIcon image1 = new ImageIcon("11.gif");

	ImageIcon image2 = new ImageIcon("12.gif");

	ImageIcon image3 = new ImageIcon("13.gif");

	ImageIcon image4 = new ImageIcon("14.gif");

	Timer timer;

	int n = 0;

	JLabel j2;
	JLabel j3;
	JLabel j4;
	JLabel j5;
	 MouseTracker[] mt=new MouseTracker[1];
	public CaiXianShou() {
		

		super("猜先手");
		
		//ChessBoard.getJF().enable(false);
		this.toFront();
		//ChessBoard.getJF().toBack();
		
		Container c = this.getContentPane();
		c.setLayout(null);
		i = (int) (Math.random() * 100);
		b1 = new JButton("单数(d)");
		b1.setLocation(50, 210);
		b1.setSize(120, 30);
		b1.addActionListener(this);
		b1.setMnemonic('d');
		b1.setEnabled(false);
		c.add(b1);

		b2 = new JButton("双数(s)");
		b2.setLocation(200, 210);
		b2.setSize(120, 30);
		b2.addActionListener(this);
		b2.setMnemonic('s');
		b2.setEnabled(false);
		c.add(b2);

		JLabel j1 = new JLabel("请随机抓取棋子哦!!!");
		j1.setSize(260, 40);
		j1.setLocation(50, 10);
		c.add(j1);

		j2 = new JLabel();
		j2.setIcon(image1);

		j2.setSize(110, 110);
		j2.setLocation(200, 20);
		// j2.setBorder(new Border());
		c.add(j2);

		ActionListener listener = new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				ImageIcon im[] = { image1, image2, image3, image4 };

				j2.setIcon(im[n]);

				n++;

				if (n == 4) {
					n = 0;
				}
			}
		};
		timer = new Timer(100, listener);

		j3 = new JLabel("现在棋盘上有若干个棋子");
		j3.setFont(new Font("楷体", Font.BOLD, 20));

		j3.setLocation(70, 140);
		j3.setSize(260, 40);
		c.add(j3);
		j3.setVisible(false);

		j4 = new JLabel("请选择是单是双~~");
		j4.setLocation(70, 170);
		j4.setSize(260, 40);
		c.add(j4);
		j4.setVisible(false);
		
		b3 = new JButton("开始抓棋(b)");
		b3.setLocation(50, 50);
		b3.setSize(120, 28);
		b3.addActionListener(this);
		b3.setMnemonic('b');
		c.add(b3);

		b4 = new JButton("抓取完成(s)");
		b4.setLocation(50, 90);
		b4.setSize(120, 28);
		b4.addActionListener(this);
		b4.setMnemonic('s');
		c.add(b4);

		//c.setBackground(Color.YELLOW);
		ImageIcon backg=new ImageIcon("cai.jpg");
		j5=new JLabel(backg);
		j5.setSize(400,300);
		j5.setLocation(0,0);
		j5.setVisible(true);
		
		c.add(j5);
		
		this.setSize(400, 300);
		this.setVisible(true);
		this.setResizable(false);
		//mt[0] = new MouseTracker(ChessBoard.getDL());

	}

	public void actionPerformed(ActionEvent e) {

		MessageDialog messagedialog = new MessageDialog();

		if (e.getSource() == b1) {
			if (i % 2 != 0) {
				messagedialog.showmessageofcaidui();
			} else {
				messagedialog.showmessageofcaicuo();
			}
			this.dispose();
			mt = ButtonPanel.mt;
			ChessBoard.a.addMouseListener(mt[0]);
			//ChessBoard.getJF().enable(true);
			//ChessBoard.getJF().toFront();
		}
		if (e.getSource() == b2) {
			if (i % 2 == 0) {
				messagedialog.showmessageofcaidui();
				this.dispose();
			} else {
				messagedialog.showmessageofcaicuo();
				this.dispose();
			}
			this.dispose();
			mt = ButtonPanel.mt;
			ChessBoard.a.addMouseListener(mt[0]);
			//ChessBoard.getJF().enable(true);
			//ChessBoard.getJF().toFront();
		}
		if (e.getSource() == b3) {
			j3.setVisible(false);
			j3.setVisible(false);
			b3.setEnabled(false);
			timer.start();

		}
		if (e.getSource() == b4) {
			timer.stop();
			j3.setVisible(true);
			j4.setVisible(true);
			b1.setEnabled(true);
			b2.setEnabled(true);
			b3.setEnabled(false);
			b4.setEnabled(false);
		}
	}

}

⌨️ 快捷键说明

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