Java 在GUI中显示来自Deck arraylist的卡

Java 在GUI中显示来自Deck arraylist的卡,java,swing,user-interface,arraylist,playing-cards,Java,Swing,User Interface,Arraylist,Playing Cards,我有一门卡片和甲板课。卡片类功能和卡片对象的arraylist。我需要从卡片组中随机抽取一张卡片,并显示在GUI标签中。我试图在“deck”资产文件夹中创建一个指向JPG卡的路径,由于某些原因,GUI组件不喜欢suit和rank值。可能是可变范围问题。。。。下面是我的按钮事件中的内容:path.concat(d.drawRandomCard().tostring()).concat(ftype)。我让这些代码在我的非GUI测试类中工作。我试图使视图以面向对象的方式与我的模型一起工作 showCa

我有一门卡片和甲板课。卡片类功能和卡片对象的arraylist。我需要从卡片组中随机抽取一张卡片,并显示在GUI标签中。我试图在“deck”资产文件夹中创建一个指向JPG卡的路径,由于某些原因,GUI组件不喜欢suit和rank值。可能是可变范围问题。。。。下面是我的按钮事件中的内容:path.concat(d.drawRandomCard().tostring()).concat(ftype)。我让这些代码在我的非GUI测试类中工作。我试图使视图以面向对象的方式与我的模型一起工作

showCard.java Deck.java 我正在尝试建立一个随机卡的路径。例如,4颗红心将位于路径:deck/h04.jpg。我需要解包或解析Card对象,以便连接:deck/+h+04+.jpg。由于某些原因,“h04”部分在GUI组件中不起作用

这是我用来测试卡片和卡片组的tester类。您可以看到资源的路径在非GUI版本中工作:

测试类
我建议你改变你的整个方法。我会在程序开始时创建我的ImageIcons,遍历所有卡片文件,将它们放入一个集合(如数组列表或数组),然后根据需要使用它们。或者更好的方法是,将一个图标与相应的卡片对象相关联。制作
cardSuit
cardRank
enum
,这样您就永远不会超出范围。。。
    import javax.swing.*;

import java.awt.event.*;
import java.awt.Color;
import java.awt.Image;

import javax.swing.ImageIcon;


public class ShowCards implements ActionListener {
    final static String LABEL_TEXT = "Random Card!!";
    JFrame frame;
    JPanel contentPane;

    JButton drawCard;
    Card c;
    Deck d;
    JLabel dieFace;
    JLabel testing;

    int[] location = new int[2];
    Image image;

    String path = "deck/";
    String ftype = ".jpg";


    public ShowCards(){
        JButton drawCard;
        JLabel dieFace;
        JLabel testing;

            /* Create and set up the frame */
            frame = new JFrame("Roll");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            /* Create a content pane with a BoxLayout and
             empty borders */
            contentPane = new JPanel();
            contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
            contentPane.setBackground(Color.white);
            contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));

            /* Create a label that shows a die face */
            dieFace = new JLabel(new ImageIcon(this.getClass().getResource("deck/h04.jpg")));
            dieFace.setAlignmentX(JLabel.CENTER_ALIGNMENT);
            dieFace.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
            contentPane.add(dieFace);


            /* Create a label for testing */
            testing = new JLabel("Kiley");
            testing.setAlignmentX(JLabel.CENTER_ALIGNMENT);
            testing.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
            contentPane.add(testing);

            /* Create a Roll Die button */
            drawCard = new JButton("Draw Card");
            drawCard.setAlignmentX(JButton.CENTER_ALIGNMENT);
            drawCard.addActionListener(this);
            contentPane.add(drawCard);

            /* Add content pane to frame */
            frame.setContentPane(contentPane);

            /* Size and then display the frame. */
            frame.pack();
            frame.setVisible(true);
        }


        /**
         * Handle a button click
         * pre: none
         * post: A die has been rolled. Matching image shown.
         */
        public void actionPerformed(ActionEvent event) {

            String drawncard = d.store.getCardSuit().toString()+d.store.getCardRank().toString();       


            //dieFace.setIcon(new ImageIcon(this.getClass().getResource("deck/d.drawRandomCard() + ".jpg")));
            //dieFace.setIcon(new ImageIcon(this.getClass().getResource("deck/s13.jpg")));
            //dieFace = new JLabel(new ImageIcon(this.getClass().getResource("deck/" + d.drawRandomCard().toString()+ ".jpg")));
            //dieFace = new JLabel(new ImageIcon(this.getClass().getResource(path.concat(d.store.getCardSuit().toString()).concat(d.store.getCardRank().toString()).concat(ftype))));
            dieFace = new JLabel(new ImageIcon(this.getClass().getResource(path.concat(drawncard).concat(ftype))));

            // parsing  dieFace = new JLabel(new ImageIcon(this.getClass().getResource(path.concat(String.valueOf(d.drawRandomCard().toString())).concat(ftype))));
            //dieFace = new JLabel(new ImageIcon(this.getClass().getResource("deck/s13.jpg")));
            dieFace.setAlignmentX(JLabel.CENTER_ALIGNMENT);
            dieFace.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
            contentPane.add(dieFace);
            /* Add content pane to frame */
            frame.setContentPane(contentPane);

            /* Size and then display the frame. */
            frame.pack();
            frame.setVisible(true);



            /* int newRoll;

            newRoll = (int)(6 * Math.random() + 1);
            if (newRoll == 1) {
                dieFace.setIcon(new ImageIcon("die1.gif"));
            } else if (newRoll == 2) {
                dieFace.setIcon(new ImageIcon("die2.gif"));
            } else if (newRoll == 3) {
                dieFace.setIcon(new ImageIcon("die3.gif"));
            } else if (newRoll == 4) {
                dieFace.setIcon(new ImageIcon("die4.gif"));
            } else if (newRoll == 5) {
                dieFace.setIcon(new ImageIcon("die5.gif"));
            } else if (newRoll == 6) {
                dieFace.setIcon(new ImageIcon("die6.gif"));
            }

            */


        }


    public Image getImage(){
        ImageIcon ii = new ImageIcon(this.getClass().getResource("deck/h04.jpg"));
        image = ii.getImage();
        return image;
    }
    public void setLocation(int x, int y){
        location[0] = x;
        location[1] = y;
    }
    public int [] getLocation(){
        return location;
    }


    /**
     * Create and show the GUI.
     */
    private static void runGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);

        ShowCards greeting = new ShowCards();

    }


    public static void main(String[] args) {
        /* Methods that create and show a GUI should be
           run from an event-dispatching thread */
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                runGUI();
            }
        });
    }
}
import java.util.Random;
import java.util.ArrayList;

public class Deck 
{

private ArrayList<Card> cards;
String mysuit,myrank;
Card store; // create an object variable to store card drawn

public Deck()
{
    cards = new ArrayList<Card>();


    for(int a =0; a<=3; a++)
    {
        for(int b =0; b<=12;b++)
        {
            cards.add(new Card(a,b));
        }
    }  
}

public Card drawRandomCard()
{
    Random generator = new Random();
    int index = generator.nextInt(cards.size());
    //return cards.remove(index);
    //return cards.get();
    store = cards.get(index);
    mysuit = store.getCardSuit().toString();
    myrank = store.getCardRank().toString();
    return cards.remove(index);




}




public String toString()
{
    String result = "Cards remaining in deck: " + cards;

        return result;

    }    
}
 public class Card
{
    //private String suit;
    private int suit, rank; 
    //private int type, value;
    //private String[] cardSuit = {"Clubs", "Spades", "Diamonds", "Hearts"};
    //private String[] cardRank = {"Ace", "King", "Queen", "Jack", "10",
       //                            "9", "8", "7", "6", "5", "4", "3", "2"};

    private String[] cardSuit = {"c", "d", "h", "s"};
    private String[] cardRank = {"01", "02", "03", "04", "05",
                                   "06", "07", "08", "09", "10", "11", "12", "13"};




    //constructor called to fill in Deck object
    public Card(int suit, int rank)
    {
        this.suit = suit;
        this.rank = rank;
    }

    //access data
    public int getSuit()
    {
    return this.suit;
    }
    public int getRank()
    {
        return this.rank;
    }   

    // have to change Card class to get letter + number combos

    //access data
        public String getCardSuit()
        {
        return cardSuit[this.suit];
        }
        public String getCardRank()
        {
            return cardRank[this.rank];
        }   






    /* public String toString(){

        String cardinfo;
        cardinfo = this.suit + " card " + this.rank;
        return cardinfo;

    }  */

     public String toString()
        {
            //String finalCard = cardSuit[this.suit] + " of " + cardRank[this.rank];
         String finalCard = cardSuit[this.suit] + cardRank[this.rank];

            return finalCard;
        }



}// end class
public class TestClasses {



    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Card c = new Card(3,12);
        System.out.println(c.getRank());
        System.out.println(c.getSuit());
        System.out.println(c);
        Deck d = new Deck();
        System.out.println("Random card:" + d.drawRandomCard());
        System.out.println("Random card rank:"+ d.store.getCardRank());
        System.out.println("Random card suit:"+ d.store.getCardSuit());
        System.out.println("Random card:" + d.drawRandomCard());
        // we need to parse THIS instance of the random card to display it.
        // A new card is removed each time the method is fired!
        System.out.println("Random card:" + d);
        System.out.println("Card Suit:" + c.getCardSuit());
        System.out.println("Card Rank:" + c.getCardRank());
        System.out.println("Random card:" + d.drawRandomCard().toString());
        System.out.println("Random card:" + d.drawRandomCard().getSuit());
        System.out.println("Random card:" + d.drawRandomCard().getRank());
        System.out.println("Random card:" + d.drawRandomCard().getCardSuit()+"Hello");
        System.out.println("Random card:" + d.drawRandomCard().getCardRank()+"09");
        System.out.println("Random card rank:"+ d.store.getCardRank());
        System.out.println("Random card suit:"+ d.store.getCardSuit());

        System.out.println(d);

        String drawncard = d.store.getCardSuit().toString()+d.store.getCardRank().toString();  

        System.out.println("Card pulled from deck:" + drawncard);
        //System.out.println();


    }

}