用java做个九宫格
316方格排序游戏设计。编写一个程序:如图所示,要求在界面上设计4×4的按钮,即16个按钮排列成4×4的网格形状。其中有且只有15个按钮上有从1到15的数字,而且这些数字...
3 16方格排序游戏设计。编写一个程序:如图所示,要求在界面上设计4×4的按钮,即16个按钮排列成4×4的网格形状。其中有且只有15个按钮上有从1到15的数字,而且这些数字在按钮上不重复出现。另外有一个按钮上没有数字。当程序刚启动时这15个数字是随机排列的。当用鼠标左键单击某个按钮,如果该按钮上有数字而且该按钮在没有数字的按钮的边上,则将该按钮上的数字给没有数字的按钮,同时该按钮就变成了没有数字的按钮。当15个数字在4×4的网格中呈顺序或逆序排列,则显示消息框表明排列成功,并重新随机排列这15个数字在按钮网格上的位置。(提示: Math.random( )可以产生随机数)
汗。给个思想也行。。。 展开
汗。给个思想也行。。。 展开
展开全部
定义了一个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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridLayout(4, 4));
pack();
}// </editor-fold>
/**
* @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
}
把下面的代码粘贴了,编译运行就可以了
不用谢我了!
/*
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.GridLayout(4, 4));
pack();
}// </editor-fold>
/**
* @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
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询