您的位置:首页 > 编程语言 > Java开发

简单的Java 16方格排序游戏

2014-11-04 14:38 106 查看
用16个按钮简单写了一个排序的小游戏。最后显示成功的功能还未实现。

以下是代码。

package u8;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;

public class T10 extends JFrame implements ActionListener{
int[] x = MyRandom();
JButton  [] btn=new JButton[16];

public T10(){
Container c = getContentPane();
for(int i = 0; i < 15; i++){
btn[i] = new JButton("" + x[i]);
c.add(btn[i]);
}
btn[15] = new JButton("");
c.add(btn[15]);
c.setLayout(new GridLayout(4,8));

for(int i = 0; i < 15; i++){
btn[i].addActionListener(this);
}

}

public int[] MyRandom(){
int[] x = new int[15];
int j = 0;
int n = 15;
Random rand = new Random();
boolean[]  bool = new boolean
;
int randInt = 0;
for(int i = 0; i < 15 ; i++) {
do {
randInt  = rand.nextInt(n);
}while(bool[randInt]);
bool[randInt] = true;
x[j]=randInt+1;
j++;
}
return x;
}

public static void main(String[] args){
T10 mp = new T10();
mp.setSize(400, 400);
mp.setLocation(400, 200);
mp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mp.setVisible(true);
}

//    public void win(){
//        Container d = getContentPane();
//        JButton  btnwin =new JButton("You win!");
//        d.add(btnwin);
////        g.setColor(Color.RED);
////        Font f =new Font("",Font.BOLD,20);
////        g.setFont(f);
////        g.drawString("You win!", 50, 20);
//    }

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton btntip = new JButton("");
int[] y = new int[16];
boolean b = false;

//        while(b==false){
//        //判断是否已经排好序
//        for(int i=0;i<16;i++){
//            y[i]=Integer.parseInt(btn[i].getText());
//        }
//        for(int i=0;i<15;i++){
//            if(y[i+1]==y[i]+1){
//                b=true;
//                break;
//            }else if(y[i]==y[i+1]+1){
//                b=true;
//                break;
//            }
//        }

//左上角移动
if(((JButton)e.getSource())==btn[0]){
if(btn[1].getText()==""){
String j = btn[0].getText();
btn[0].setText("");
btn[1].setText(j);
}
else if(btn[4].getText()==""){
String j = btn[0].getText();
btn[0].setText("");
btn[4].setText(j);
}
}

//右上角移动
if(((JButton)e.getSource())==btn[3]){
if(btn[2].getText()==""){
String j = btn[3].getText();
btn[3].setText("");
btn[2].setText(j);
}
else if(btn[7].getText()==""){
String j = btn[3].getText();
btn[3].setText("");
btn[7].setText(j);
}
}

//左下角移动
if(((JButton)e.getSource())==btn[12]){
if(btn[8].getText()==""){
String j = btn[12].getText();
btn[12].setText("");
btn[8].setText(j);
}
else if(btn[13].getText()==""){
String j = btn[12].getText();
btn[12].setText("");
btn[13].setText(j);
}
}

//右下角移动
if(((JButton)e.getSource())==btn[15]){
if(btn[11].getText()==""){
String j = btn[15].getText();
btn[15].setText("");
btn[11].setText(j);
}
else if(btn[14].getText()==""){
String j = btn[15].getText();
btn[15].setText("");
btn[14].setText(j);
}
}

//上中移动
if(((JButton)e.getSource())==btn[1]||((JButton)e.getSource())==btn[2]){
int tip = 0;
btntip = (JButton)e.getSource();
for(int i=0;i<16;i++){
if(btn[i]==btntip){
tip = i;
}
}
if(btn[tip-1].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip-1].setText(j);
}
else if(btn[tip+1].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip+1].setText(j);
}
else if(btn[tip+4].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip+4].setText(j);
}
}

//下中移动
if(((JButton)e.getSource())==btn[14]||((JButton)e.getSource())==btn[13]){
int tip = 0;
btntip = (JButton)e.getSource();
for(int i=0;i<16;i++){
if(btn[i]==btntip){
tip = i;
}
}
if(btn[tip-1].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip-1].setText(j);
}
else if(btn[tip+1].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip+1].setText(j);
}
else if(btn[tip-4].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip-4].setText(j);
}
}

//左中移动
if(((JButton)e.getSource())==btn[4]||((JButton)e.getSource())==btn[8]){
int tip = 0;
btntip = (JButton)e.getSource();
for(int i=0;i<16;i++){
if(btn[i]==btntip){
tip = i;
}
}
if(btn[tip-4].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip-4].setText(j);
}
else if(btn[tip+1].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip+1].setText(j);
}
else if(btn[tip+4].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip+4].setText(j);
}
}

//右中移动
if(((JButton)e.getSource())==btn[7]||((JButton)e.getSource())==btn[11]){
int tip = 0;
btntip = (JButton)e.getSource();
for(int i=0;i<16;i++){
if(btn[i]==btntip){
tip = i;
}
}
if(btn[tip-1].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip-1].setText(j);
}
else if(btn[tip+4].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip+4].setText(j);
}
else if(btn[tip-4].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip-4].setText(j);
}
}

//中间四块移动
if(((JButton)e.getSource())==btn[5]||((JButton)e.getSource())==btn[6]||((JButton)e.getSource())==btn[9]||((JButton)e.getSource())==btn[10]){
int tip = 0;
btntip = (JButton)e.getSource();
for(int i=0;i<16;i++){
if(btn[i]==btntip){
tip = i;
}
}
if(btn[tip-1].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip-1].setText(j);
}
else if(btn[tip+1].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip+1].setText(j);
}
else if(btn[tip-4].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip-4].setText(j);
}
else if(btn[tip+4].getText()==""){
String j = btn[tip].getText();
btn[tip].setText("");
btn[tip+4].setText(j);
}
}

}

//        if(b==true){
//            Container d = getContentPane();
//            JButton  btnwin =new JButton("You win!");
//            d.add(btnwin);
//            d.setSize(100, 50);
//            d.setLocation(450, 250);
//            d.setVisible(true);
//        }
}

//}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: