单击GUI时,数组丢失数据

时间:2012-02-27 21:17:54

标签: java arrays swing nullpointerexception

问题:

  • 制作一个对象数组
  • 一旦我点击GUI,数组就会设置为null
  • 我跟踪了代码并看到我的阵列正在正确设置

以下是代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Puzzle1 extends JPanel{
        public Square[] sq;
        public String zero = "16";

    public class Square{
            public int realPos;
            public int curPos;
        }

    public String setAdjacent(String curZero, int z){
        String adj = "-1";
        int zeroInt = Integer.parseInt(curZero);
        if(z==0){
                adj = Integer.toString(zeroInt+1);
                if(zeroInt+1 == 17 || zeroInt+1 == 13 || zeroInt+1 == 9 || zeroInt+1 == 5){
                    adj = "-1";
                }
            }
            if(z==1){
                adj = Integer.toString(zeroInt-1);
                if(zeroInt-1 == 0|| zeroInt-1 == 4 || zeroInt-1 == 8 || zeroInt-1 == 12){
                    adj = "-1";
                }
            }
            if(z==2){
                adj = Integer.toString(zeroInt+4);
                if(zeroInt+4 > 16){
                    adj = "-1";
                }
            }
            if(z==3){
                adj = Integer.toString(zeroInt-4);
                if(zeroInt-4 < 1){
                    adj = "-1";
                }
            }

        return adj;
    }
    public static void buildGUI(){
        JFrame frame = new JFrame("Puzzle");
        Puzzle1 panel = new Puzzle1();
        GridLayout mainLayout = new GridLayout(1, 1);
        panel.setPreferredSize(new Dimension(300, 300));
        panel.setMinimumSize(new Dimension(300, 300));
        panel.setMaximumSize(new Dimension(300, 300));
        frame.setResizable(false);
        panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
        Puzzle1 p1 = new Puzzle1();
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        p1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        p1.setName("1");
        panel.setLayout(mainLayout);    
        p1.add(new JLabel("1"));
        panel.add(p1);
        frame.setContentPane(panel);
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes thread on close. 
        frame.setVisible(true);
    }

    public void clicked(JPanel pnlClick){
                String adj[] = new String[4]; 
            for(int z=0; z<4; z++){
                adj[z] = setAdjacent(zero, z);
            }
            String clicked = pnlClick.getName();
            for(int i = 0; i<4; i++){
                String stAdj = adj[i];
                System.out.println(stAdj);
                    int clkd = Integer.parseInt(clicked);
                sq[clkd].curPos = 0;
                System.out.println("clicked and adj");
            }
    }
    class MouseEventHandler implements MouseListener{       
        @Override
        public void mouseClicked(MouseEvent e) {
            JPanel pnlClick = (JPanel)(e.getSource());
            clicked(pnlClick);
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
        @Override
        public void mouseExited(MouseEvent arg0) {
        }
        @Override
        public void mousePressed(MouseEvent arg0) {
        }
        @Override
        public void mouseReleased(MouseEvent arg0) {
        }

    }

    public Puzzle1() {
        MouseEventHandler handler = new MouseEventHandler();
        this.addMouseListener(handler);
    }

    public void createSquares(){
        sq = new Square[16];
        for(int j = 0; j < 16; j++){
            sq[j] = new Square();
        }
        for(int i = 0; i < 16; i++){
            if(i == 0){
                sq[i].realPos = 16;
                sq[i].curPos = 0;
            }
            else{
                sq[i].realPos = i;
                sq[i].curPos = i;
            }

        }
    }

    public static void main(String[] args){
        Puzzle1 Puzzle1 = new Puzzle1();
        Puzzle1.buildGUI();
        Puzzle1.createSquares();
    }
}

我尝试在点击后删除'sq'变量可能被删除的任何地方,但调试器总是直接进入mouseClicked事件并且调试器显示'sq'为空。

2 个答案:

答案 0 :(得分:3)

在调用addMouseListener

之前,在Puzzle1构造函数中调用createSquares

答案 1 :(得分:2)

尝试clicked方法的这种变体并仔细检查输出。

public void clicked(JPanel pnlClick){
            String adj[] = new String[4];
        for(int z=0; z<4; z++){
            adj[z] = setAdjacent(zero, z);
        }
        String clicked = pnlClick.getName();
        System.out.println("Clicked name: " + clicked);
        for(int i = 0; i<4; i++){
            String stAdj = adj[i];
            System.out.println(stAdj);
            if(clicked.equals(stAdj)){
                int clkd = Integer.parseInt(clicked);
                sq[clkd].curPos = 0;
                System.out.println("clicked and adj");
            }
        }
}