如何在java中为二十一点保留子类修复此经销商方法

时间:2011-06-23 13:10:54

标签: java

如何修复stay类以使其循环

打包gamecomponents;

import gamecomponents.Card;
import gamecomponents.GameLogic;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Graphical1
{
    // Creating the components + global variables

    // declaring the deal,hit and stay JButtons
    private JButton deal;
    private JButton stay;
    // Three hit buttons are needed because clicking
    // the same JButton would change the JLabel
    // displaying the card
    private JButton hitbutton1;
    private JButton hitbutton2;
    private JButton hitbutton3;

    // declaring the dealer and player card JLabels
    // as well as the dealers and players card count
    private JLabel dcard1;
    private JLabel dcard2;
    private JLabel dcard3;
    private JLabel dcard4;
    private JLabel dcard5;
    private JLabel pcard1;
    private JLabel pcard2;
    private JLabel pcard3;
    private JLabel pcard4;
    private JLabel pcard5;
    private JLabel dcardcount;
    private JLabel pcardcount;

    // declaring ImageIcon
    ImageIcon cardBack = new ImageIcon ("resources/images/cards/CardBack.gif");

    // Using components from another class
    GameLogic g = new GameLogic();

    public Graphical1()
    {
        //Creating the window
        JFrame GameFrame = new JFrame ("BlackJack");
        GameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GameFrame.setSize(500,525);
        GameFrame.setResizable(false);
        //centers the window in the middle of the screen
        GameFrame.setLocationRelativeTo(null);      
        //Set background Colour to custom RGB value
        GameFrame.setBackground(new Color(0, 139, 69)); 

        //Setting up JButton properties 
        deal = new JButton("Deal");
        deal.setBounds(75,450,75,25);
        deal.addActionListener(new Deal());

        stay = new JButton("Stay");
        stay.setBounds(215,450,75,25);
        stay.addActionListener(new Stay());

        hitbutton1 = new JButton("Hit");
        hitbutton1.setBounds(350,450,75,25);
        hitbutton1.addActionListener(new HitJButton1());

        hitbutton2 = new JButton("Hit");
        hitbutton2.setBounds(350,450,75,25);
        hitbutton2.addActionListener(new HitJButton2());

        hitbutton3 = new JButton("Hit");
        hitbutton3.setBounds(350,450,75,25);
        hitbutton3.addActionListener(new HitJButton3());

        // Setting up JLabel Properties
        // These are the dealers card JLabels

        dcard1 = new JLabel("");
        dcard1.setBounds(50,50,107,148);

        dcard2 = new JLabel("");
        dcard2.setBounds(100,50,107,148);

        dcard3 = new JLabel("");
        dcard3.setBounds(150,50,107,148);

        dcard4 = new JLabel("");
        dcard4.setBounds(200,50,107,148);

        dcard5 = new JLabel("");
        dcard5.setBounds(250,50,107,148);


        // these are the player card JLabels

            pcard1 = new JLabel("");
            pcard1.setBounds(50,225,107,148);

            pcard2 = new JLabel("");
            pcard2.setBounds(100,225,107,148);

            pcard3 = new JLabel("");
            pcard3.setBounds(150,225,107,148);

            pcard4 = new JLabel("");
            pcard4.setBounds(200,225,107,148);

            pcard5 = new JLabel("");
            pcard5.setBounds(250,225,107,148);


        // this shows the dealers score 
        dcardcount = new JLabel("Dealers Card Count:");
        dcardcount.setBounds(50,200,200,25);

        // this shows the players score
        pcardcount = new JLabel("Players Card Count:");
        pcardcount.setBounds(50,375,200,25);

        //Create the container for the components and set layout
        Container contentPane = GameFrame.getContentPane();
        contentPane.setLayout(null);

        //Add the components to the contentPane
        contentPane.add(deal);
        contentPane.add(stay);
        contentPane.add(hitbutton1);
        contentPane.add(hitbutton2);
        contentPane.add(hitbutton3);
        contentPane.add(dcard5);
        contentPane.add(dcard4); 
        contentPane.add(dcard3); 
        contentPane.add(dcard2); 
        contentPane.add(dcard1);
        contentPane.add(pcard5);
        contentPane.add(pcard4); 
        contentPane.add(pcard3); 
        contentPane.add(pcard2);
        contentPane.add(pcard1);
        contentPane.add(dcardcount);
        contentPane.add(pcardcount);

        //Hit buttons not visible
        hitbutton2.setVisible(false);
        hitbutton3.setVisible(false);

        //Disable the hit and stay button
        hitbutton1.setEnabled(false);
        stay.setEnabled(false);

        //Setting the frame to be visible
        GameFrame.setVisible(true);

    }

        class Deal implements ActionListener{
              public void actionPerformed(ActionEvent e){

            Card dealerCard1 = g.drawdealer(); 
            Card playerCard1 = g.drawplayer();
            Card playerCard2 = g.drawplayer();          

                //Dealers first card
                dcard1.setIcon(new ImageIcon("resources/images/cards/"+dealerCard1+".gif"));

                //Update the dealers card count
                dcardcount.setText("Dealers Card Count: " + g.dsum());

                //Dealers second card
                dcard2.setIcon(cardBack);

                //Players first card
                pcard1.setIcon(new ImageIcon("resources/images/cards/"+playerCard1+".gif"));

                //Update the players card count
                pcardcount.setText("Players Card Count: " + g.psum());

                //Players second card  
                pcard2.setIcon(new ImageIcon("resources/images/cards/"+playerCard2+".gif"));

                //Update the players card count
                pcardcount.setText("Players Card Count: " + g.psum());

                //Disable the deal JButton so the dealt cards cannot change
                deal.setEnabled(false);

                //Enable the first hit button
                hitbutton1.setEnabled(true);

                //Enable the stay button
                stay.setEnabled(true);
              }

    }

        class HitJButton1 implements ActionListener{
              public void actionPerformed(ActionEvent e){  

            Card playerCard3 = g.drawplayer();

                //Players second card  
                    pcard3.setIcon(new ImageIcon("resources/images/cards/"+playerCard3+".gif"));

                //Update the players card count
               pcardcount.setText("Players Card Count: " + g.psum());

                //Hide the Hit JButton and 
                    hitbutton1.setVisible(false);
                    hitbutton2.setVisible(true);
              }


        }

        class HitJButton2 implements ActionListener{
              public void actionPerformed(ActionEvent e){

            Card playerCard4 = g.drawplayer();

                //Players second card  
                    pcard4.setIcon(new ImageIcon("resources/images/cards/"+playerCard4+".gif"));

                //Update the players card count
              pcardcount.setText("Players Card Count: " + g.psum());

                //Hide the Hit JButton and 
                    hitbutton2.setVisible(false);
                    hitbutton3.setVisible(true);
              }
        }

        class HitJButton3 implements ActionListener{
              public void actionPerformed(ActionEvent e){

            Card playerCard5 = g.drawplayer();

                //Players second card  
                    pcard5.setIcon(new ImageIcon("resources/images/cards/"+playerCard5+".gif"));

                //Update the players card count
               pcardcount.setText("Players Card Count: " + g.psum());

                //Hide the Hit JButton and 
                    hitbutton3.setEnabled(false);
              }
        }

        class Stay implements ActionListener{
              public void actionPerformed(ActionEvent e){

                Card dealerCard2 = g.drawdealer();
                Card dealerCard3 = g.drawdealer();
                Card dealerCard4 = g.drawdealer();
                Card dealerCard5 = g.drawdealer();

                //Disable the stay button 
                stay.setEnabled(false);

                //Disable all the hit buttons
                hitbutton1.setEnabled(false);
                hitbutton2.setEnabled(false);
                hitbutton3.setEnabled(false);   

                //Checks if the count is less than 17
                  if (g.dsum < 17) {

                    //Dealers second card

                    dcard2.setIcon(new ImageIcon("resources/images/cards/"+dealerCard2+".gif"));

                    //Update the dealers card count
                    dcardcount.setText("Dealers Card Count: " + g.dsum());

                  } 

                //Checks if the count is less than 17
                  if (g.dsum < 17) {

                    //Dealers third card

                    dcard3.setIcon(new ImageIcon("resources/images/cards/"+dealerCard3+".gif"));

                    //Update the dealers card count
                    dcardcount.setText("Dealers Card Count: " + g.dsum());

                  } 

                //Checks if the count is less than 17
                 if (g.dsum < 17) {

                    //Dealers second card

                    dcard4.setIcon(new ImageIcon("resources/images/cards/"+dealerCard4+".gif"));

                    //Update the dealers card count
                    dcardcount.setText("Dealers Card Count: " + g.dsum());

                 }  

                //Checks if the count is less than 17
                  if (g.dsum < 17) {

                    //Dealers second card

                    dcard5.setIcon(new ImageIcon("resources/images/cards/"+dealerCard5+".gif"));

                    //Update the dealers card count
                    dcardcount.setText("Dealers Card Count: " + g.dsum());  
                  }

              }

        }



    public static void main (String[] args)
    {

    //Create an instance of my class
    new Graphical1();
    }



}

1 个答案:

答案 0 :(得分:2)

我不确定你为什么要在循环中运行Stay类。使用Swing,只需通过单击按钮来控制GUI,该按钮将调用已注册为该按钮的ActionListener的类的actionPerformed方法。我注意到你已经这样做了。

一旦发出所有卡片,用户应该可以通过单击按钮选择要做什么。我仍然不确定你打算做什么但是在这种情况下我看不到Stay类中循环的原因。

希望这有帮助。