Java ArrayList在actionPerformed方法中重置

Java ArrayList在actionPerformed方法中重置,java,arraylist,graphics,Java,Arraylist,Graphics,我正在尝试制作一个纸牌游戏,但是在actionPerformed方法中arrayList存在一个问题,它是静态的。arrayList用于包含已单击的卡片,如果其中有两张以上,则会删除索引0处的卡片。这两张卡片都是用来比较的,如果这样做是合法的,那么索引较低的卡片将移至索引较高的卡片。但问题是,当单击的第一张卡成功添加到arrayList时,当尝试向arrayList添加第二张卡时,arrayList突然显示为空。System.out.println语句被放入带有remove方法调用的if语句中,

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

import java.applet.*;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.*;
公共类游戏扩展JPanel实现ActionListener{
静态图像图标Deck1,Temp1,ThreeS,
Ace1,Ace2,Ace3,Ace4,
国王,国王,国王C,国王D;
形象;形象;
静态按钮A1、A2、A3、A4、KS、KH、KD、KC、D1、T1、TS;
静态JButton列1;
静态阵列式甲板;
静态卡阵列;
静态卡[]温度;
静态整数计数;
静态JFrame;
静态JPanel面板;
静态博弈遗传算法;
静态int c=1;
静态数组列表numClicks;
静态数组列表col1;
静态阵列列表col2;
静态数组列表col3;
静态阵列列表col4;
静态数组列表col5;
静态数组列表col6;
静态数组列表col7;
静态卡电流卡;
静态双dragFromX,dragFromY;
公共布尔移动(卡a、卡b){
setLayout(空);
如果((a.getValue()
Unrelated:静态是良好OO设计中的一个异常。您不能使用“所有事物都是静态的”。恰恰相反。那么。方法“canMove()”的命名就像它只是提供一些信息一样。但它确实改变了一切。这让读者非常困惑。问题是:这段代码很难阅读,因此调试起来也很困难。也要做这样的事情!我想,每次单击数组时,都会再次初始化
numClicks=newarraylist()。您需要在
EventHandler
之外初始化变量。
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();
        }
    }
}