LeoSchimidt

Jogo do Bicho JFrame

Apr 26th, 2018
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.07 KB | None | 0 0
  1. import javax.swing.JOptionPane;
  2.  
  3. public class JFrameFruta extends javax.swing.JFrame {
  4.  
  5.     /**
  6.      * Creates new form JFrameFruta
  7.      */
  8.     public JFrameFruta() {
  9.         initComponents();
  10.     }
  11.  
  12.     /**
  13.      * This method is called from within the constructor to initialize the form.
  14.      * WARNING: Do NOT modify this code. The content of this method is always
  15.      * regenerated by the Form Editor.
  16.      */
  17.     @SuppressWarnings("unchecked")
  18.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  19.     private void initComponents() {
  20.  
  21.         jPanel1 = new javax.swing.JPanel();
  22.         Icon1 = new javax.swing.JLabel();
  23.         Icon2 = new javax.swing.JLabel();
  24.         Icon3 = new javax.swing.JLabel();
  25.         btnSortear = new javax.swing.JButton();
  26.  
  27.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  28.  
  29.         Icon1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  30.         Icon1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/1.jpg"))); // NOI18N
  31.  
  32.         Icon2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  33.         Icon2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/2.jpg"))); // NOI18N
  34.  
  35.         Icon3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
  36.         Icon3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/3.jpg"))); // NOI18N
  37.  
  38.         btnSortear.setText("Sortear");
  39.         btnSortear.addActionListener(new java.awt.event.ActionListener() {
  40.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  41.                 btnSortearActionPerformed(evt);
  42.             }
  43.         });
  44.  
  45.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  46.         jPanel1.setLayout(jPanel1Layout);
  47.         jPanel1Layout.setHorizontalGroup(
  48.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  49.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  50.                 .addGap(42, 42, 42)
  51.                 .addComponent(Icon1)
  52.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 75, Short.MAX_VALUE)
  53.                 .addComponent(Icon2)
  54.                 .addGap(62, 62, 62)
  55.                 .addComponent(Icon3)
  56.                 .addGap(42, 42, 42))
  57.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  58.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  59.                 .addComponent(btnSortear, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
  60.                 .addGap(151, 151, 151))
  61.         );
  62.         jPanel1Layout.setVerticalGroup(
  63.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  64.             .addGroup(jPanel1Layout.createSequentialGroup()
  65.                 .addGap(86, 86, 86)
  66.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  67.                     .addComponent(Icon1)
  68.                     .addComponent(Icon2)
  69.                     .addComponent(Icon3))
  70.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
  71.                 .addComponent(btnSortear, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
  72.                 .addGap(33, 33, 33))
  73.         );
  74.  
  75.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  76.         getContentPane().setLayout(layout);
  77.         layout.setHorizontalGroup(
  78.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  79.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  80.         );
  81.         layout.setVerticalGroup(
  82.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  83.             .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  84.         );
  85.  
  86.         pack();
  87.     }// </editor-fold>                        
  88.  
  89.     private void btnSortearActionPerformed(java.awt.event.ActionEvent evt) {                                          
  90.  
  91.         int R1 = (int)(Math.round(Math.random()*2)+1);
  92.         int R2 = (int)(Math.round(Math.random()*2)+1);
  93.         int R3 = (int)(Math.round(Math.random()*2)+1);
  94.  
  95.         Icon1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/"+R1+".jpg")));
  96.         Icon2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/"+R2+".jpg")));
  97.         Icon3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/"+R3+".jpg")));
  98.        
  99.         if(R1 == R2 && R2 == R3){
  100.             JOptionPane.showMessageDialog(null, "Parabéns, você ganhou");
  101.         }
  102.        
  103.         else{
  104.             JOptionPane.showMessageDialog(null, "Você perdeu, trouxa.");
  105.         }
  106.        
  107.        
  108.  
  109.  
  110.         // TODO add your handling code here:
  111.     }                                          
  112.  
  113.     /**
  114.      * @param args the command line arguments
  115.      */
  116.     public static void main(String args[]) {
  117.         /* Set the Nimbus look and feel */
  118.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  119.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  120.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  121.          */
  122.         try {
  123.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  124.                 if ("Nimbus".equals(info.getName())) {
  125.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  126.                     break;
  127.                 }
  128.             }
  129.         } catch (ClassNotFoundException ex) {
  130.             java.util.logging.Logger.getLogger(JFrameFruta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  131.         } catch (InstantiationException ex) {
  132.             java.util.logging.Logger.getLogger(JFrameFruta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  133.         } catch (IllegalAccessException ex) {
  134.             java.util.logging.Logger.getLogger(JFrameFruta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  135.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  136.             java.util.logging.Logger.getLogger(JFrameFruta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  137.         }
  138.         //</editor-fold>
  139.  
  140.         /* Create and display the form */
  141.         java.awt.EventQueue.invokeLater(new Runnable() {
  142.             public void run() {
  143.                 new JFrameFruta().setVisible(true);
  144.             }
  145.         });
  146.     }
  147.  
  148.     // Variables declaration - do not modify                    
  149.     private javax.swing.JLabel Icon1;
  150.     private javax.swing.JLabel Icon2;
  151.     private javax.swing.JLabel Icon3;
  152.     private javax.swing.JButton btnSortear;
  153.     private javax.swing.JPanel jPanel1;
  154.     // End of variables declaration                  
  155. }
Add Comment
Please, Sign In to add comment