简体   繁体   中英

How to display new text within a JLabel

I wanted to know how I can display text within jPanel5 in this slot machine code. This is my code

        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;
}

I forgot how to add text to a label, I want to assign text to jLabel5 so that every time I roll, my initial number(my ideal number would be 30) goes down by 1. I know I can just do Money --; but I don't know how to DISPLAY the new number every time. Additionally, I have scoured the internet for any help, but unfortunately I could not find any information regarding this topic. Thanks in advance!

您只应像这样设置其文本:

jLabel5.setText("newDesiredText");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM