Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

cần mọi nguười giúp e với bài java này ạ

19:11 19-07-2017 770 lượt xem 3 bình luận 19:30 19-07-2017

import java.awt.Button;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
import org.jfree.data.xy.Vector;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author 8470p
 */
public class demoo extends javax.swing.JFrame {

    
  
    JButton bt[]=new JButton[16];
    JButton btt[][]=new JButton[4][4];
    

    /**
     * Creates new form demoo
     */

    public static ArrayList generate() {

//Khai bao thanh phan trung gian
        ArrayList tmp = new ArrayList();
        int count = 0;
        int value;
        boolean flag;
        while (count < 15) {
            flag = false;
            value = (int) (Math.random() * 15 + 1);//random từ 1 tơi 15
            for (int i = 0; i < tmp.size(); i++) {
                if (((Integer) tmp.get(i)).intValue() == value) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                tmp.add(value);
                count++;
            }
        }
        return tmp;

    }

    public demoo() {
        test2 te=new test2();
        initComponents();
        ArrayList list=te.generate();
       
        bt[0]=jButton1;
        bt[1]=jButton2;
        bt[2]=jButton3;
        bt[3]=jButton4;
        bt[4]=jButton5;
        bt[5]=jButton6;
        bt[6]=jButton7;
        bt[7]=jButton8;
        bt[8]=jButton9;
        bt[9]=jButton10;
        bt[10]=jButton11;
        bt[11]=jButton12;
        bt[12]=jButton13;
        bt[13]=jButton14;
        bt[14]=jButton15;
        bt[15]=jButton16;
        
        for (int i = 0; i < 15; i++) {
            bt[i].setText(list.get(i)+"");
    
        }
        
        btt[0][0]=jButton1;
        btt[0][1]=jButton2;
        btt[0][2]=jButton3;
        btt[0][3]=jButton4;
        btt[1][0]=jButton5;
        btt[1][1]=jButton6;
        btt[1][2]=jButton7;
        btt[1][3]=jButton8;
        btt[2][0]=jButton9;
        btt[2][1]=jButton10;
        btt[2][2]=jButton11;
        btt[2][3]=jButton12;
        btt[3][0]=jButton13;
        btt[3][1]=jButton14;
        btt[3][2]=jButton15;
        btt[3][3]=jButton16;
      
        
    }
    public void clickButton(JButton btn){
        for (int i = 0; i <btt.length; i++) {
            for (int j = 0; j < btt[i].length; j++) {
                if (btt[i][j].getText().length()==0) {///nếu tìm thấy button ko có text
                    if (btn==btt[i][j+1]) {
                          btt[i][j].setText(btn.getText());
                        btn.setText("");
                      
                        
                        
                    } else if (btn==btt[i][j-1]) {
                        btt[i][j-1].setText(btn.getText());
                        btn.setText("");
                        
                    } else if (btt[i+1][j]==btn) {
                        btt[i+1][j].setText(btn.getText());
                        btn.setText("");
                        
                    } else if (btt[i+1][j-1]==btn) {
                        btt[i+1][j-1].setText(btn.getText());
                        btn.setText("");
                        
                    } else if (btt[i+1][j+1]==btn) {
                        btt[i+1][j+1].setText(btn.getText());
                        btn.setText("");
                        
                    } if (btt[i-1][j]==btn) {
                        btt[i-1][j].setText(btn.getText());
                        btn.setText("");
                        
                    }
            
        
}
            }}}
    /**
     * 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() {

        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();
        jButton16 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(4, 4));

        jButton1.setText("1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);

        jButton4.setText("2");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton4);

        jButton7.setText("3");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton7);

        jButton2.setText("15");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);

        jButton9.setText("4");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton9);

        jButton6.setText("5");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton6);

        jButton5.setText("6");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton5);

        jButton8.setText("7");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton8);

        jButton10.setText("8");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton10);

        jButton3.setText("9");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);

        jButton11.setText("10");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton11);

        jButton12.setText("11");
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton12);

        jButton13.setText("12");
        jButton13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton13ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton13);

        jButton14.setText("13");
        jButton14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton14ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton14);

        jButton15.setText("14");
        jButton15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton15ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton15);

        jButton16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton16ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton16);

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // 
        clickButton(jButton1);
    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        clickButton(jButton4);
    }                                        

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        clickButton(jButton7);
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        clickButton(jButton2);
    }                                        

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        clickButton(jButton9);
    }                                        

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        clickButton(jButton6);
    }                                        

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        clickButton(jButton5);
    }                                        

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        clickButton(jButton8);
    }                                        

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        clickButton(jButton10);
    }                                         

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        clickButton(jButton3);
    }                                        

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        clickButton(jButton11);
    }                                         

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        clickButton(jButton12);
    }                                         

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        clickButton(jButton13);
    }                                         

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        clickButton(jButton14);
    }                                         

    private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        clickButton(jButton15);
    }                                         

    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        clickButton(jButton16);
    }                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(demoo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(demoo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(demoo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(demoo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new demoo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton16;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    // End of variables declaration                   
}

 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
Yêu màu Tím đã bình luận 19:18 19-07-2017

Chào anh chị ạ> Bài của e có 16 Button . 15 Button cho text từa 1-15 không trùng (e đã cho random vào ròi ạ) và 1 Button trống (Không có text).Đề bài là mỗi khi nhấn Button gần Button trống kia thì sẽ đổi text cho nhau.Em đã tạo 1 mảng 2 chiều (JButton [][] btn ) rồi tạo 1 hàm đưa từng button sau đó xét từng trường hợp mà không đuwọc. bác nào giúp e với.Em Cảm ơn ạ !

Yêu màu Tím đã bình luận 19:16 19-07-2017

e xin lỗi ! tại chèn code nên nó mất cái yêu cầu 
Chào anh chị ạ> Bài của e có 16 Button . 15 Button cho text từa 1-15 không trùng (e đã cho random vào ròi ạ) và 1 Button trống (Không có text).Đề bài là mỗi khi nhấn Button gần Button trống kia thì sẽ đổi text cho nhau.Em đã tạo 1 mảng 2 chiều (JButton [][] btn ) rồi tạo 1 hàm đưa từng button sau đó xét từng trường hợp mà không đuwọc. bác nào giúp e với.Em Cảm ơn ạ !

Phạm Tấn Thành Moderator đã bình luận 19:14 19-07-2017

câu hỏi của bạn là gì đưa code ai hiểu đây

Câu hỏi mới nhất