2014-12-06 54 views
0

我想知道如何在此老虎机代码中的jPanel5中显示文本。这是我的代码如何在JLabel中显示新文本

 jButton1.setActionCo package slotmachine2; 

import java.util.Random; 
import javax.swing.ImageIcon; 

/** 
* 
* @author tempacc 
*/ 
public class SlotMachine extends javax.swing.JFrame { 

    /** 
    * Creates new form SlotMachine 
    */ 
    public SlotMachine() { 
     initComponents(); 
    } 
public int getSlot1(){ 
    Random r = new Random(); 
    roll = r.nextInt(6)+1; 
    return roll; 
} 
public int getSlot2(){ 
    Random r = new Random(); 
    roll2 = r.nextInt(6)+1; 
    return roll2; 
} 
public int getSlot3(){ 
    Random r = new Random(); 
    roll3 = r.nextInt(6)+1; 
    return roll3; 
} 
    /** 
    * 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() { 

     jLabel6 = new javax.swing.JLabel(); 
     jPanel1 = new javax.swing.JPanel(); 
     jButton1 = new javax.swing.JButton(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     jLabel3 = new javax.swing.JLabel(); 
     jLabel4 = new javax.swing.JLabel(); 
     jLabel5 = new javax.swing.JLabel(); 

     jLabel6.setText("jLabel6"); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Slot Machine")); 
mmand("Spin!"); 
     jButton1.setAutoscrolls(true); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SlotMachine/icons/Star.jpeg"))); // NOI18N 

     jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SlotMachine/icons/Star.jpeg"))); // NOI18N 

     jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/SlotMachine/icons/Star.jpeg"))); // NOI18N 

     jLabel4.setText("Push the button to roll ->"); 

     javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(jPanel1Layout.createSequentialGroup() 
       .addGap(84, 84, 84) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) 
        .addGroup(jPanel1Layout.createSequentialGroup() 
         .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(18, 18, 18) 
         .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
       .addContainerGap(54, Short.MAX_VALUE)) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(jLabel5) 
       .addGap(255, 255, 255)) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jLabel5) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING) 
        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)) 
       .addGap(18, 18, 18) 
       .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(30, 30, 30)) 
     ); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(0, 12, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
     ); 

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     if(getSlot1() == 1){ 
      jLabel1.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/1.png"))); 
     }else if(getSlot1() == 2){ 
      jLabel1.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/2.png"))); 
     }else if(getSlot1() == 3){ 
      jLabel1.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/3.png"))); 
     }else if(getSlot1() == 4){ 
      jLabel1.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/4.png"))); 
     }else if(getSlot1() == 5){ 
      jLabel1.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/5.png")));   
     }else if(getSlot1() == 6){ 
      jLabel1.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/6.png"))); 
     }else if(getSlot1() == 7){ 
      jLabel1.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/7.png")));  
    }           
     if(getSlot2() == 1){ 
      jLabel2.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/1.png"))); 
     }else if(getSlot2() == 2){ 
      jLabel2.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/2.png"))); 
     }else if(getSlot2() == 3){ 
      jLabel2.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/3.png"))); 
     }else if(getSlot2() == 4){ 
      jLabel2.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/4.png")));  
     }else if(getSlot2() == 5){ 
      jLabel2.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/5.png"))); 
     }else if(getSlot2() == 6){ 
      jLabel2.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/6.png"))); 
     }else if(getSlot2() == 7){ 
      jLabel2.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/7.png")));  
     } 
     if(getSlot3() == 1){ 
      jLabel3.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/1.png"))); 
     }else if(getSlot3() == 2){ 
      jLabel3.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/2.png"))); 
     }else if(getSlot3() == 3){ 
      jLabel3.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/3.png"))); 
     }else if(getSlot3() == 4){ 
      jLabel3.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/4.png")));  
     }else if(getSlot3() == 5){ 
      jLabel3.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/5.png"))); 
     }else if(getSlot3() == 6){ 
      jLabel3.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/6.png"))); 
     }else if(getSlot3() == 7){ 
      jLabel3.setIcon(new ImageIcon(getClass().getResource("/SlotMachine/icons/7.png"))); 

    } 


    }           

    /** 
    * @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(SlotMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(SlotMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(SlotMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(SlotMachine.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
    `enter code here`//</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
    public void run() { 
    new SlotMachine().setVisible(true); 
    } 
    }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JLabel jLabel4; 
    private javax.swing.JLabel jLabel5; 
    private javax.swing.JLabel jLabel6; 
    private javax.swing.JPanel jPanel1; 
    // End of variables declaration     
    int roll; 
    int roll2; 
    int roll3; 
    int Money = 100; 
} 

我忘了怎么文本添加到一个标签,我想将文本分配到jLabel5让每一位我摇的时候,我最初的号码(我的理想的数字是30),由1下降我知道我可以做钱 - 但我不知道如何每次显示新号码。此外,我已经搜索了互联网寻求帮助,但不幸的是,我找不到任何关于此主题的信息。提前致谢!

+2

'的JLabel#setText'? – MadProgrammer 2014-12-06 09:30:45

+1

请只提供相关代码。大部分代码与您要问的问题无关。 – EWit 2014-12-06 09:34:50

+0

你忘了?尝试谷歌,或读一本书。 – Marichyasana 2014-12-06 09:38:53

回答

0

您应该只设置其文字是这样的:

jLabel5.setText("newDesiredText"); 
+0

它告诉我一个int不能转换为字符串,我应该怎么做? – 2014-12-08 07:27:07

+0

@JonnySmith,这个问题可能会有所帮助http://stackoverflow.com/questions/4105331/how-to-convert-from-int-to-string – SummerCode 2014-12-08 09:41:15