java奇数九宫格代码_用java做个九宫格

这篇博客展示了如何在Java中创建一个奇数九宫格。作者定义了一个名为NineGrid的类,该类继承自JFrame并实现了ActionListener接口。九宫格中的数字是随机排列的,用户可以通过点击按钮交换位置,目标是使每个数字对应到其在网格中的正确位置。当九宫格排列正确时,程序会显示胜利消息。
摘要由CSDN通过智能技术生成

定义了一个package名叫aloha

把下面的代码粘贴了,编译运行就可以了

不用谢我了!

/*

* NineGrid.java

* @author libai8723@qq.com

* Created on 2011-12-20, 13:21:36

*/

package aloha;

import java.awt.Dimension;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JOptionPane;

/**

*

* @author libai

*/

public class NineGrid extends javax.swing.JFrame implements ActionListener{

/** Creates new form NineGrid */

public NineGrid() {

initComponents();

Toolkit tk = Toolkit.getDefaultToolkit();

Dimension d = tk.getScreenSize();

this.setSize(400, 400);

this.setTitle("Nine Grid");

this.setLocation((int)(d.getWidth() - 400)/2, (int)(d.getHeight() - 400)/2);

for(int i = 0;i < 15;i++)

{

this.arr[i] = i+1;

}

this.arr[15] = -1;

this.arr[11] = -1;

this.arr[15] = 12;

for(int i = 0;i < 15;i++)

{

int idx =(int) (Math.random() * 15);

int tmp = this.arr[7];

this.arr[7] = this.arr[idx];

this.arr[idx] = tmp;

}

for(int i = 0;i < 4;i++)

{

for(int j = 0;j < 4;j++)

{

if(this.arr[i * 4 + j] != -1)

{

this.Buttons[i][j] = new JButton("" + this.arr[i * 4 + j]);

this.Buttons[i][j].addActionListener(this);

this.getContentPane().add(this.Buttons[i][j]);

}

else

{

this.Buttons[i][j] = new JButton("");

this.Buttons[i][j].addActionListener(this);

this.getContentPane().add(this.Buttons[i][j]);

this.Buttons[i][j].setEnabled(false);

}

}

}

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

//

private void initComponents() {

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

getContentPane().setLayout(new java.awt.GridLayout(4, 4));

pack();

}//

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new NineGrid().setVisible(true);

}

});

}

private JButton[][] Buttons = new JButton[4][4];

private int[] arr = new int[16];

private boolean isSucceed()

{

boolean flag = true;

for(int i = 0;i < 4;i++)

{

for(int j = 0;j < 4;j++)

{

if(!this.Buttons[i][j].getText().equals(""))

if(!this.Buttons[i][j].getText().equals(""+(i * 4 + j + 1)))

{

return false;

}

}

}

return true;

}

public void actionPerformed(ActionEvent e)

{

int i = 0,j = 0;

boolean in = false;

for(i = 0;i < 4;i++)

{

for(j = 0;j < 4;j++)

{

if(e.getSource() == this.Buttons[i][j])

{

in = true;

break;

}

}

if(in)

break;

}

if((i >= 0 && (j - 1) >= 0)&&(!this.Buttons[i][j - 1].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i][j - 1].getText());

this.Buttons[i][j - 1].setText(tmp);

this.Buttons[i][j - 1].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

if((i >= 0 && (j + 1) < 4)&&(!this.Buttons[i][j + 1].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i][j + 1].getText());

this.Buttons[i][j + 1].setText(tmp);

this.Buttons[i][j + 1].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

if((i - 1 >= 0 && j >= 0)&&(!this.Buttons[i - 1][j].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i - 1][j].getText());

this.Buttons[i - 1][j].setText(tmp);

this.Buttons[i - 1][j].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

if((i + 1 < 4 && j >= 0)&&(!this.Buttons[i + 1][j].isEnabled()))

{

String tmp = this.Buttons[i][j].getText();

this.Buttons[i][j].setText(this.Buttons[i + 1][j].getText());

this.Buttons[i + 1][j].setText(tmp);

this.Buttons[i + 1][j].setEnabled(true);

this.Buttons[i][j].setEnabled(false);

if(this.isSucceed())

JOptionPane.showConfirmDialog(this, "You Win!!!!");

return;

}

}

// Variables declaration - do not modify

// End of variables declaration

}

本回答被提问者和网友采纳

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值