ArrayList在actionPerformed方法中重置

时间:2017-06-08 14:28:09

标签: java arraylist graphics

我正在尝试制作纸牌游戏,但是在actionPerformed方法中,arrayList存在问题,这是静态的。 arrayList旨在包含已被单击的卡,如果卡内有两个以上,则删除索引0处的卡。如果这样的移动是合法的,那么其中的两张牌都将被比较,并且较低索引处的牌将被移动到较高索引处的牌。但问题是,当第一张卡片被成功添加到arrayList时,当尝试向其添加秒时,arrayList突然显示为空。使用remove方法调用将System.out.println语句放入if语句,以查看是否以某种方式激活了这些语句,但是没有调用这些语句。如果调用add方法向其中添加一个卡并且没有调用remove方法(至少根据它们的测试方式),你是如何将arrayList显示为空的?

import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Game extends JPanel implements ActionListener {

    static ImageIcon Deck1, Temp1, ThreeS,
            Ace1, Ace2, Ace3, Ace4,
            KingH, KingS, KingC, KingD;

    Image DeckImage;
    static JButton A1, A2, A3, A4, KS, KH, KD, KC, D1, T1, TS;
    static JButton column1;
    static ArrayList<Card> deck;
    static Card[] cardArray;
    static Card[] temp;
    static int count;
    static JFrame frame;
    static JPanel panel;
    static Game ga;
    static int c = 1;
    static ArrayList<Card> numClicks;
    static ArrayList<Card> col1;
    static ArrayList<Card> col2;
    static ArrayList<Card> col3;
    static ArrayList<Card> col4;
    static ArrayList<Card> col5;
    static ArrayList<Card> col6;
    static ArrayList<Card> col7;
    static Card currentCard;
    static double dragFromX, dragFromY;

    public boolean canMove(Card a, Card b) {
        setLayout(null);

        if ((a.getValue() < b.getValue()) && (!a.getColor().equals(b.getColor()))) {
            double x = b.getButton().getLocation().getX();
            double y = b.getButton().getLocation().getY();
            a.getButton().setBounds((int) x, (int) y + 50, 90, 100);
            return true;
        }

        return false;
    }

    public void actionPerformed(ActionEvent evt) {
        // check which command has been sent
        if (evt.getSource() == D1) {
            count = 1;
        }

        numClicks = new ArrayList<Card>();
        // checks to see if a certain card is clicked
        if (evt.getSource() == KD) {
            if (numClicks.size() == 0) {
                // adds the card to index 0 in numClicks if it's the first click
                numClicks.add(temp[3]);

            } else if (numClicks.size() == 1) {
                // adds the card to index 1 in numClicks if it's the second click
                numClicks.add(temp[3]);
                // checks to see if the card can be moved and moves it if possible
                canMove(numClicks.get(0),
                        numClicks.get(1));
            } else {
                // if their is more than three cards, 
                // then the card in the first index is removed 
                // and this card is added as the second click 
                numClicks.remove(0);

                numClicks.add(temp[3]);
                canMove(numClicks.get(0), numClicks.get(1));
            }
        }

        if (evt.getSource() == TS) {

            if (numClicks.size() == 0) {
                numClicks.add(temp[3]);
            } else if (numClicks.size() == 1) {
                numClicks.add(temp[4]);
                canMove(numClicks.get(0), numClicks.get(1));

            } else {
                numClicks.remove(0);
                numClicks.add(temp[4]);
                canMove(numClicks.get(0), numClicks.get(1));

            }

            repaint();
        }
    }
}

0 个答案:

没有答案