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

📄 huarongdao.java

📁 窗口的退出方法.rar
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class HuaRongDao extends JFrame implements ActionListener
{
	static int i=0;
	JButton [][] btn=new JButton [4][4];
	JLabel lb=new JLabel ("祝您游戏愉快!",JLabel.CENTER);
	JPanel pn=new JPanel (new GridLayout (4,4));
	
	public HuaRongDao (String str)
	{
		super (str);
		i=0;
		int ct=15;
		Container ctn=this.getContentPane();
		for (int j=3;j>=0;j--)
		{
			for (int k=3;k>=0;k--)
			{	
			    if (j==0 && k==0)
		     	   {break;}
			    btn[j][k]=new JButton (""+ct+"");
			    pn.add(btn[j][k]);
		     	btn[j][k].addActionListener(this);
		     	ct--;
		     	
		    }	
		}
		btn[0][0]=new JButton ("");
		pn.add(btn[0][0]);
		btn[0][0].addActionListener(this);
		
		ctn.setLayout(new BorderLayout ());
		ctn.add(pn,BorderLayout.CENTER);
		ctn.add(lb,BorderLayout.SOUTH);
		
		this.setSize(300,260);
		this.show();
		
		this.addWindowListener(new WindowAdapter ()
		{
			public void windowClosing (WindowEvent e)
			{
				JOptionPane jp=new JOptionPane ();
				int value=jp.showConfirmDialog(null,"您确实想推出游戏吗?");
	        	if (value==jp.YES_OPTION)
	        	{
	        		YES ();
	        	}
	        	else if (value==jp.NO_OPTION)
	        	{
	        		NO ();	
	        	}
	        	else if (value==jp.CANCEL_OPTION)
	        	{
	        		CANCEL ();
	        	}
			}
		});
		
	}
	public void YES ()
	{
		System.exit(0);
	}
	public void NO ()
	{
	    new JOptionPane().showMessageDialog(null,"重新开始游戏!");
	    this.dispose();
	    new HuaRongDao ("Game");
	}
	public void CANCEL ()
	{
		new JOptionPane().showMessageDialog(null,"重新开始游戏!");
		this.dispose();
	    new HuaRongDao ("Game");
	}
	
	public void actionPerformed (ActionEvent e)
	{
		if (e.getActionCommand()=="")
		{
			lb.setText("您共移动了:"+i+"次!");
		}
		else if (e.getActionCommand()!="")
		{
			lb.setText("您共移动了:"+i+"次!");
			
			for (int r=0;r<4;r++)
			{
				int gg=0;
			for (int t=0;t<4;t++)
			{
				if (btn[r][t].getLabel()==e.getActionCommand())
				{
					if (r+1<4 && btn[r+1][t].getLabel()=="")
					{
						btn[r+1][t].setLabel(e.getActionCommand());
						btn[r][t].setLabel("");
						gg=1;
						i++;
		            	lb.setText("您共移动了:"+i+"次!");
						break;
					}
					if (r-1>=0 && btn[r-1][t].getLabel()=="")
					{
						btn[r-1][t].setLabel(e.getActionCommand());
						btn[r][t].setLabel("");
						gg=1;
						i++;
		            	lb.setText("您共移动了:"+i+"次!");
						break;
					}
					if (t+1<4 && btn[r][t+1].getLabel()=="")
					{
						btn[r][t+1].setLabel(e.getActionCommand());
						btn[r][t].setLabel("");
						gg=1;
						i++;
		            	lb.setText("您共移动了:"+i+"次!");
						break;
					}
					if (t-1>=0 && btn[r][t-1].getLabel()=="")
					{
						btn[r][t-1].setLabel(e.getActionCommand());
						btn[r][t].setLabel("");
						gg=1;
						i++;
		            	lb.setText("您共移动了:"+i+"次!");
						break;
					}
				}
			}
			if (gg==1)
			break;
		}
		}
	}
	
	public static void main (String [] args)
	{
		new HuaRongDao ("Game");
	}
}

⌨️ 快捷键说明

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