码迷,mamicode.com
首页 > 编程语言 > 详细

简单的Java 16方格排序游戏

时间:2014-11-04 16:41:08      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   java   for   sp   

用16个按钮简单写了一个排序的小游戏。最后显示成功的功能还未实现。

以下是代码。

  1 package u8;
  2 
  3 import java.awt.*;
  4 import java.awt.event.ActionEvent;
  5 import java.awt.event.ActionListener;
  6 import java.util.Random;
  7 
  8 import javax.swing.*;
  9 
 10 public class T10 extends JFrame implements ActionListener{
 11     int[] x = MyRandom();
 12     JButton  [] btn=new JButton[16];
 13     
 14     public T10(){
 15         Container c = getContentPane();
 16         for(int i = 0; i < 15; i++){
 17             btn[i] = new JButton("" + x[i]);
 18             c.add(btn[i]);
 19         }
 20         btn[15] = new JButton("");
 21         c.add(btn[15]);
 22         c.setLayout(new GridLayout(4,8));
 23         
 24         for(int i = 0; i < 15; i++){
 25             btn[i].addActionListener(this);
 26         }
 27         
 28     }
 29     
 30     public int[] MyRandom(){
 31         int[] x = new int[15];
 32         int j = 0;
 33         int n = 15;
 34         Random rand = new Random();
 35         boolean[]  bool = new boolean[n];
 36         int randInt = 0;
 37         for(int i = 0; i < 15 ; i++) {
 38              do {
 39                  randInt  = rand.nextInt(n);
 40              }while(bool[randInt]);
 41         bool[randInt] = true;
 42         x[j]=randInt+1;
 43         j++;
 44         }
 45         return x;
 46     }
 47     
 48     public static void main(String[] args){
 49         T10 mp = new T10();
 50         mp.setSize(400, 400);
 51         mp.setLocation(400, 200);
 52         mp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 53         mp.setVisible(true);
 54     }
 55     
 56 //    public void win(){
 57 //        Container d = getContentPane();
 58 //        JButton  btnwin =new JButton("You win!");
 59 //        d.add(btnwin);
 60 ////        g.setColor(Color.RED);
 61 ////        Font f =new Font("",Font.BOLD,20);
 62 ////        g.setFont(f);
 63 ////        g.drawString("You win!", 50, 20);
 64 //    }
 65     
 66     @Override
 67     public void actionPerformed(ActionEvent e) {
 68         // TODO Auto-generated method stub
 69         JButton btntip = new JButton("");
 70         int[] y = new int[16];
 71         boolean b = false;
 72         
 73         
 74 //        while(b==false){
 75 //        //判断是否已经排好序
 76 //        for(int i=0;i<16;i++){
 77 //            y[i]=Integer.parseInt(btn[i].getText());
 78 //        }
 79 //        for(int i=0;i<15;i++){
 80 //            if(y[i+1]==y[i]+1){
 81 //                b=true;
 82 //                break;
 83 //            }else if(y[i]==y[i+1]+1){
 84 //                b=true;
 85 //                break;
 86 //            }
 87 //        }
 88         
 89         
 90         //左上角移动
 91         if(((JButton)e.getSource())==btn[0]){
 92             if(btn[1].getText()==""){
 93                 String j = btn[0].getText();
 94                 btn[0].setText("");
 95                 btn[1].setText(j);
 96             }
 97             else if(btn[4].getText()==""){
 98                 String j = btn[0].getText();
 99                 btn[0].setText("");
100                 btn[4].setText(j);
101             }
102         }
103         
104         //右上角移动
105         if(((JButton)e.getSource())==btn[3]){
106             if(btn[2].getText()==""){
107                 String j = btn[3].getText();
108                 btn[3].setText("");
109                 btn[2].setText(j);
110             }
111             else if(btn[7].getText()==""){
112                 String j = btn[3].getText();
113                 btn[3].setText("");
114                 btn[7].setText(j);
115             }
116         }
117         
118         //左下角移动
119         if(((JButton)e.getSource())==btn[12]){
120             if(btn[8].getText()==""){
121                 String j = btn[12].getText();
122                 btn[12].setText("");
123                 btn[8].setText(j);
124             }
125             else if(btn[13].getText()==""){
126                 String j = btn[12].getText();
127                 btn[12].setText("");
128                 btn[13].setText(j);
129             }
130         }
131         
132         //右下角移动
133         if(((JButton)e.getSource())==btn[15]){
134             if(btn[11].getText()==""){
135                 String j = btn[15].getText();
136                 btn[15].setText("");
137                 btn[11].setText(j);
138             }
139             else if(btn[14].getText()==""){
140                 String j = btn[15].getText();
141                 btn[15].setText("");
142                 btn[14].setText(j);
143             }
144         }
145         
146         //上中移动
147         if(((JButton)e.getSource())==btn[1]||((JButton)e.getSource())==btn[2]){
148             int tip = 0;
149             btntip = (JButton)e.getSource();
150             for(int i=0;i<16;i++){
151                 if(btn[i]==btntip){
152                     tip = i;
153                 }
154             }
155             if(btn[tip-1].getText()==""){
156                 String j = btn[tip].getText();
157                 btn[tip].setText("");
158                 btn[tip-1].setText(j);
159             }
160             else if(btn[tip+1].getText()==""){
161                 String j = btn[tip].getText();
162                 btn[tip].setText("");
163                 btn[tip+1].setText(j);
164             }
165             else if(btn[tip+4].getText()==""){
166                 String j = btn[tip].getText();
167                 btn[tip].setText("");
168                 btn[tip+4].setText(j);
169             }
170         }
171         
172         //下中移动
173         if(((JButton)e.getSource())==btn[14]||((JButton)e.getSource())==btn[13]){
174             int tip = 0;
175             btntip = (JButton)e.getSource();
176             for(int i=0;i<16;i++){
177                 if(btn[i]==btntip){
178                     tip = i;
179                 }
180             }
181             if(btn[tip-1].getText()==""){
182                 String j = btn[tip].getText();
183                 btn[tip].setText("");
184                 btn[tip-1].setText(j);
185             }
186             else if(btn[tip+1].getText()==""){
187                 String j = btn[tip].getText();
188                 btn[tip].setText("");
189                 btn[tip+1].setText(j);
190             }
191             else if(btn[tip-4].getText()==""){
192                 String j = btn[tip].getText();
193                 btn[tip].setText("");
194                 btn[tip-4].setText(j);
195             }
196         }
197         
198         //左中移动
199         if(((JButton)e.getSource())==btn[4]||((JButton)e.getSource())==btn[8]){
200             int tip = 0;
201             btntip = (JButton)e.getSource();
202             for(int i=0;i<16;i++){
203                 if(btn[i]==btntip){
204                     tip = i;
205                 }
206             }
207             if(btn[tip-4].getText()==""){
208                 String j = btn[tip].getText();
209                 btn[tip].setText("");
210                 btn[tip-4].setText(j);
211             }
212             else if(btn[tip+1].getText()==""){
213                 String j = btn[tip].getText();
214                 btn[tip].setText("");
215                 btn[tip+1].setText(j);
216             }
217             else if(btn[tip+4].getText()==""){
218                 String j = btn[tip].getText();
219                 btn[tip].setText("");
220                 btn[tip+4].setText(j);
221             }
222         }
223         
224         //右中移动
225         if(((JButton)e.getSource())==btn[7]||((JButton)e.getSource())==btn[11]){
226             int tip = 0;
227             btntip = (JButton)e.getSource();
228             for(int i=0;i<16;i++){
229                 if(btn[i]==btntip){
230                     tip = i;
231                 }
232             }
233             if(btn[tip-1].getText()==""){
234                 String j = btn[tip].getText();
235                 btn[tip].setText("");
236                 btn[tip-1].setText(j);
237             }
238             else if(btn[tip+4].getText()==""){
239                 String j = btn[tip].getText();
240                 btn[tip].setText("");
241                 btn[tip+4].setText(j);
242             }
243             else if(btn[tip-4].getText()==""){
244                 String j = btn[tip].getText();
245                 btn[tip].setText("");
246                 btn[tip-4].setText(j);
247             }
248         }
249         
250         //中间四块移动
251         if(((JButton)e.getSource())==btn[5]||((JButton)e.getSource())==btn[6]||((JButton)e.getSource())==btn[9]||((JButton)e.getSource())==btn[10]){
252             int tip = 0;
253             btntip = (JButton)e.getSource();
254             for(int i=0;i<16;i++){
255                 if(btn[i]==btntip){
256                     tip = i;
257                 }
258             }
259             if(btn[tip-1].getText()==""){
260                 String j = btn[tip].getText();
261                 btn[tip].setText("");
262                 btn[tip-1].setText(j);
263             }
264             else if(btn[tip+1].getText()==""){
265                 String j = btn[tip].getText();
266                 btn[tip].setText("");
267                 btn[tip+1].setText(j);
268             }
269             else if(btn[tip-4].getText()==""){
270                 String j = btn[tip].getText();
271                 btn[tip].setText("");
272                 btn[tip-4].setText(j);
273             }
274             else if(btn[tip+4].getText()==""){
275                 String j = btn[tip].getText();
276                 btn[tip].setText("");
277                 btn[tip+4].setText(j);
278             }
279         }
280         
281     }
282         
283 //        if(b==true){            
284 //            Container d = getContentPane();
285 //            JButton  btnwin =new JButton("You win!");
286 //            d.add(btnwin);
287 //            d.setSize(100, 50);
288 //            d.setLocation(450, 250);
289 //            d.setVisible(true);
290 //        }
291   }
292     
293 //}

 

简单的Java 16方格排序游戏

标签:style   blog   io   color   ar   os   java   for   sp   

原文地址:http://www.cnblogs.com/mamayi/p/4073661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!