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

📄 test.java

📁 用JAVA实现的一个挖雷的小游戏 不过只是为了实现这个小游戏 所以没有增加级别等级
💻 JAVA
字号:
package com.xinxi.test;

import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.VolatileImage;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

import com.xinxi.mine.*;

public class Test extends JFrame implements ActionListener{

	static JButton but[][];
	JTextField tf[][];
	Image image;
	static Icon icon;     
	static Icon icon2;  
	            
	public static void main(String[] args) {
		Test t = new Test();
		t.start();
	}
	
	public void start(){
		but = new JButton[5][5];
		tf = new JTextField[5][5];
		//image = createVolatileImage(3,3);
		icon = new ImageIcon("image/white.gif");
		icon2 = new ImageIcon("image/black.gif");
		//this.setLayout(new GridLayout(10, 10));
		this.setLayout(null);
		for(int i=0; i<but.length; i++){
			for(int j=0; j<but[0].length; j++){
				but[i][j] = new JButton();
				//but[i][j].setIcon(icon);
				tf[i][j] = new JTextField();
				but[i][j].setBounds(new Rectangle(i*25, j*25,25, 25)); 
				tf [i][j].setBounds(new Rectangle(i*25, j*25,25, 25));//用这种方法实现三维布局 
				this.getContentPane().add(but[i][j]);
				this.getContentPane().add(tf[i][j]);
				but[i][j].setVisible(true);
				tf[i][j].setVisible(false);
				tf[i][j].setEditable(false);
				//tf[i][j].setVisible(true);
				//but[i][j].addActionListener(this);
				but[i][j].addMouseListener(new MyListener());
			}
		}		
		this.setSize(300,300);
		this.setLocation(300,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}

	public void actionPerformed(ActionEvent e) {
		JButton b = (JButton)e.getSource();
		for(int i=0; i<but.length; i++){
			for(int j=0; j<but[0].length; j++){
				b.setIcon(icon);
			}
		}
	}

}

class MyListener implements MouseListener{

	static boolean flag = false;
	public void mouseClicked(MouseEvent e) {
		JButton b = (JButton)e.getSource();
		if(flag) {
			b.setIcon(Test.icon);
			flag=false;
		}
		else if(!flag){
			b.setIcon(Test.icon2);
			flag=true;
		}
	}

	public void mouseEntered(MouseEvent arg0) {
		
	}

	public void mouseExited(MouseEvent arg0) {
		
	}

	public void mousePressed(MouseEvent arg0) {
		
	}

	public void mouseReleased(MouseEvent arg0) {
		
	}
	
}





⌨️ 快捷键说明

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