似乎没有1 jButton来完成这两项任务

时间:2014-11-26 01:30:28

标签: java loops jbutton

我很难搞清楚为什么我的BlackJack纸牌游戏程序不会同时处理经销商和用户。如果我在buttonAction中注释掉代码的经销商部分,那么用户(底部)的工作完全正常。我注意到如果我注释掉用户部分,经销商部分(顶部)仍然无效。所以我把它缩小到我的dealDealerHand和经销商功能的设置方式。任何帮助表示赞赏。

* Project_4.java代码*

package project_4;

public class Project_4 {
public static void main(String[] args) {

    Card [] deck;
    int suite, face, i;

    deck = new Card[52];

    i = 0;
    for(suite = 0; suite < 4; suite++) {
        for(face = 0; face < 13; face++) {
          deck[i] = new Card(face, suite);
          i++;
        }
    }

    for(i = 0; i < deck.length; i++) {
        System.out.println(deck[i] + " value: " + deck[i].cardValue());
    }
}
}

* GameFrame.java *

package project_4;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*; 
import javax.swing.*;
import java.util.Random;

public class GameFrame extends javax.swing.JFrame {
public GameFrame() {
    initComponents();

    rand = new Random();
    loadDeck();
    loadLabels();
    hand = new Card[5];

}

/**
 * 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() {

    lblCard1 = new javax.swing.JLabel();
    btnDeal = new javax.swing.JButton();
    lblCard2 = new javax.swing.JLabel();
    lblCard3 = new javax.swing.JLabel();
    lblCard4 = new javax.swing.JLabel();
    lblCard5 = new javax.swing.JLabel();
    lblScoreUser = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    lblResult = new javax.swing.JLabel();
    lblCard6 = new javax.swing.JLabel();
    lblCard7 = new javax.swing.JLabel();
    lblCard8 = new javax.swing.JLabel();
    lblCard9 = new javax.swing.JLabel();
    lblCard10 = new javax.swing.JLabel();
    lblScoreDealer = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    btnDeal.setText("Deal");
    btnDeal.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnDealActionPerformed(evt);
        }
    });

    lblScoreUser.setText("Score: ");

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

    lblScoreDealer.setText("Score: ");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(11, 11, 11)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(lblCard1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lblCard6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(18, 18, 18)
                    .addComponent(btnDeal))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(lblResult, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblScoreDealer, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(lblCard2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(lblCard3, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(lblCard4, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(lblCard5, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 133, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addComponent(lblCard7, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(lblCard8, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(lblCard9, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(lblCard10, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblScoreUser, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(137, 137, 137)
                            .addComponent(lblScoreDealer, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(lblResult, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lblCard10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblCard6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblCard7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblCard8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblCard9, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                .addGroup(layout.createSequentialGroup()
                    .addGap(18, 18, 18)
                    .addComponent(lblScoreUser, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(lblCard5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                .addComponent(lblCard2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                .addComponent(lblCard3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                .addComponent(lblCard4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                .addComponent(lblCard1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnDeal)
                .addComponent(jButton1))
            .addGap(23, 23, 23))
    );

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

private void btnDealActionPerformed(java.awt.event.ActionEvent evt) {                                        


    dealHandDealer();
    dealerLabels[j].setIcon(new ImageIcon(File.separator + "Users" + File.separator + "colteaves" + File.separator + "Documents" + File.separator + "images" + File.separator + dealerHand[j].cardAbbrevation() + ".png"));
    score2 = score2 + dealerHand[j].cardValue();
    lblScoreDealer.setText("Dealer Score: " + score2);
    j++;

    if(dealerHand[j].getFace() == 0 && (score2 + dealerHand[j].cardValue()) > 21) {
        score2 = score2 - 10;
    }

    if(score2 > 21) {

        lblResult.setText("You LOSE!");
        score2 = 0;
    }

    dealHand();
    cardLabels[i].setIcon(new ImageIcon(File.separator + "Users" + File.separator + "colteaves" + File.separator + "Documents" + File.separator + "images" + File.separator + hand[i].cardAbbrevation() + ".png"));
    score = score + hand[i].cardValue();
    lblScoreUser.setText("User Score: " + score);
    i++;

    if(hand[i].getFace() == 0 && (score + hand[i].cardValue()) > 21) {
        score = score - 10;
    }

    if(score > 21) {

        lblResult.setText("You LOSE!");
        score = 0;
    }
}                                       

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int k;
    for(k = 0; k < hand.length; k++) {
        cardLabels[k].setIcon(null);
    }
    i = 0;
    score = 0;
    lblResult.setText(" ");
    loadDeck();
}                                        

public void dealHand() {

    hand[i] = dealCard();
}

public void dealHandDealer() {

    dealerHand[i] = dealCardDealer();
}

public void loadDeck() {
    int i = 0, j, k;
    deck = new Card[52];

    for(j = 0; j < 13; j++) {
        for (k = 0; k < 4; k++) {
            deck[i] = new Card(j, k);
            i++;
        }
    }
}

public void loadLabels() {
    int i;
    cardLabels = new JLabel[5];
    cardLabels[0] = lblCard1;
    cardLabels[1] = lblCard2;
    cardLabels[2] = lblCard3;
    cardLabels[3] = lblCard4;
    cardLabels[4] = lblCard5;

}

public void loadDealerLabels() {
    int j;
    dealerLabels = new JLabel[5];
    dealerLabels[0] = lblCard6;
    dealerLabels[1] = lblCard7;
    dealerLabels[2] = lblCard8;
    dealerLabels[3] = lblCard9;
    dealerLabels[4] = lblCard10;
}

public Card dealCard() {
    Card currentCard;

    currentCard = deck[rand.nextInt(52)];

    while(currentCard.getAvailable() == false) {
           currentCard = deck[rand.nextInt(52)];
    }
    currentCard.setAvailable(false);
    return currentCard;
}

public Card dealCardDealer() {
    Card currentCard;

    currentCard = deck[rand.nextInt(52)];

    while(currentCard.getAvailable() == false) {
           currentCard = deck[rand.nextInt(52)];
    }
    currentCard.setAvailable(false);
    return currentCard;
}
/**
 * @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(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(GameFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(GameFrame.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 GameFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton btnDeal;
private javax.swing.JButton jButton1;
private javax.swing.JLabel lblCard1;
private javax.swing.JLabel lblCard10;
private javax.swing.JLabel lblCard2;
private javax.swing.JLabel lblCard3;
private javax.swing.JLabel lblCard4;
private javax.swing.JLabel lblCard5;
private javax.swing.JLabel lblCard6;
private javax.swing.JLabel lblCard7;
private javax.swing.JLabel lblCard8;
private javax.swing.JLabel lblCard9;
private javax.swing.JLabel lblResult;
private javax.swing.JLabel lblScoreDealer;
private javax.swing.JLabel lblScoreUser;
// End of variables declaration                   

private Card [] deck;
private Card [] hand;
private Card [] dealerHand;
private int i = 0;
private int j = 0;
private int score = 0;
private int score2 = 0;
private JLabel [] cardLabels;
private JLabel [] dealerLabels;
private Random rand;
}

*** Card.java ****

package project_4;

public class Card {
//Instance Variables
private int suite;
private int face;
private boolean available;

//Constructor
public Card(int face, int suite) {
    setFace(face);
    setSuite(suite);
    setAvailable(true);
}

public void setSuite(int suite) {
    if(suite >= 0 && suite <= 3) {
        this.suite = suite;
    }else {
        System.out.println(suite + " is an invalid suite value.");
    }
}

public void setFace(int face) {
    if(suite >= 0 && suite <= 12) {
        this.face = face;
    }else {
        System.out.println(face + " is an invalid face value.");
    }
}

public void setAvailable(boolean available) {
    this.available = available;
}

public int getSuite() {
    return suite;
}

public int getFace() {
    return face;
}

public boolean getAvailable() {
    return available;
}

public String suiteText() {
    String [] suites = {"diamonds", "spades", "clubs", "hearts"};
    return suites[suite];
}

public String faceText() {
    String [] faces = {"ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king"};
    return faces [face];
}

public int cardValue() {
    if(face == 0) {
        return 11;
    }else if(face < 9) {
        return face + 1;
    }
    return 10;
}

public String cardAbbrevation() {
    return (face + 1) + suiteText().substring(0, 1);
}

public String toString() {
    return faceText() + " " + suiteText();
}

}

0 个答案:

没有答案