Java对象值不一致

Java对象值不一致,java,object,Java,Object,我找不到解决我问题的任何其他方法,因为我不确定如何用足够少的词来描述它 当我分配activeList时,它是currentActiveList类中随机生成的矩形的一个字段,它可以很好地接收值 public class ActiveList { Rectangle[] activeList = new Rectangle[10]; public ActiveList() { for(int i = 0; i < activeList.length; i++)

我找不到解决我问题的任何其他方法,因为我不确定如何用足够少的词来描述它

当我分配
activeList
时,它是
currentActiveList
类中随机生成的矩形的一个字段,它可以很好地接收值

public class ActiveList {
    Rectangle[] activeList = new Rectangle[10];

    public ActiveList() {
        for(int i = 0; i < activeList.length; i++)
            activeList[i] = null;
    }

    public void addToList(Rectangle x) {
        for(int i = 0; i < this.activeList.length; i++) {
            if(this.activeList[i] == null) {
                this.activeList[i] = x;
                i = this.activeList.length+1;
            }

            else
                this.activeList[activeList.length-1] = x;
        }
    }

    public Rectangle[] getActiveList() {
        return this.activeList;
    }

    public int getLength() {
        //System.out.print(this.activeList.length);
        return this.activeList.length;
    }

    public void deleteFromList(int x) {
        this.activeList[x] = null;
    }

    public Rectangle getFromList(int x) {
        Rectangle retVal = this.activeList[x];
        //System.out.println("Returning getFromList(int x): " +retVal);
        return retVal;
    }

    public void genRandomRectangle() {
        Random randomNumberGenerator = new Random();
        double[] pointVal = new double[4];
        double randomInt = randomNumberGenerator.nextInt(400-10);
        pointVal[0] = randomInt;
        randomInt = randomNumberGenerator.nextInt(400-10);
        pointVal[1] = randomInt;
        randomInt = randomNumberGenerator.nextInt((int) (400-pointVal[0]));
        if(randomInt < 5) {
            randomInt = randomInt+pointVal[0]+5;
        }

        else
            pointVal[2] = randomInt+pointVal[0];

        randomInt = randomNumberGenerator.nextInt((int) (400-pointVal[1]));
        if(randomInt < 5) {
            randomInt = randomInt+pointVal[1]+5;
        }

        else
            pointVal[3] = randomInt+pointVal[1];

        Rectangle newRandom = new Rectangle(pointVal[0], pointVal[1], pointVal[2], pointVal[3]);
        //System.out.println(pointVal[0]);
        //System.out.println(pointVal[1]);
        //System.out.println(pointVal[2]);
        //System.out.println(pointVal[3]);
        System.out.println("New Random: " +newRandom);

        addToList(newRandom);
    }
}
随机生成器方法由动作侦听器调用

public class ButtonPrompt extends GraphicGen {

    ActionListener actionListenerRandom = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
          //currentActiveList.genRandomRectangle();
            callGenRandom();
            callRepaintOnMain();
        }
    };

    JButton randomBtn = new JButton("Add Random Rectangle");        
    JButton inputCoordinates = new JButton("Input Rectangle Coordinates");

    public ButtonPrompt() {
        JFrame f = new JFrame("Add Rectangles");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BoxLayout boxLayout = new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS);
        f.setLayout(boxLayout);

        randomBtn.addActionListener(actionListenerRandom);

        f.setSize(200, 200);
        f.setLocation(600, 200);
        f.setVisible(true);
        f.add(randomBtn);
        f.add(inputCoordinates);
        f.pack();
    }
}

这是范围界定问题还是引用问题?我在这里真的很茫然。

您的实际问题还不清楚,但只要阅读前两种方法就足以发现什么是bug:

public ActiveList() {
    for(int i = 0; i < activeList.length; i++)
        activeList[i] = null;
}
public-ActiveList(){
for(int i=0;i
上面的代码是完全无用的。对象数组元素的默认值为null。因此,循环将null分配给一个变量,该变量已经为null

public void addToList(Rectangle x) {
    for(int i = 0; i < this.activeList.length; i++) {
        if(this.activeList[i] == null) {
            this.activeList[i] = x;
            i = this.activeList.length+1;
        }

        else
            this.activeList[activeList.length-1] = x;
    }
}
public void addToList(矩形x){
for(int i=0;i
如果列表仅包含null,则矩形将存储在索引0处,循环将停止。对于此方法的所有后续调用,循环将发现索引0处的元素不是null,因此将在数组的最后一个索引处存储x,然后在第一个非null索引处存储x

我不知道您到底想实现什么,以及实际的问题是什么,但您可能应该忘记基于数组实现自己的列表,而改用ArrayList。

此处:

public static void main(String[] args) {
    ...
    mainFrame.add(new GraphicGen());
    ...
    ButtonPrompt buttonPrompter = new ButtonPrompt();
}
按钮compat
扩展了
GraphicGen
,它是一个
JPanel
。在
buttoncomp
的构造函数中,您创建了一个
JFrame
,并向其添加了两个
JButton
s

因此,当您的应用程序启动时,屏幕上将出现两个jFrame,一个是包含一个
GraphicGen
mainFrame
,另一个是包含两个按钮的
ButtonPomper

单击按钮时,将调用
actionPerformed()
,它实际上调用
buttoncomputer
callGenRandom()
如果随机生成逻辑正确,则生成的矩形将添加到
buttoncomputer
。但是您没有将此
按钮compter
添加到任何
JFrame
中,您将看不到它。


您可能想要的:

ButtonPrompt
不扩展
GraphicGen
,而是为
ButtonPrompt
提供添加到
大型机的
GraphicGen
的引用

public class ButtonPrompt extends GraphicGen {
    JButton randomBtn = new JButton("Add Random Rectangle");        
    JButton inputCoordinates = new JButton("Input Rectangle Coordinates");
    final GraphicGen gg;

    public ButtonPrompt(GraphicGen gg) {
        this.gg = gg;
        ......

        ActionListener actionListenerRandom = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                gg.callGenRandom();
                gg.callRepaintOnMain();
            }
        };

        randomBtn.addActionListener(actionListenerRandom);

        ......
    }
}
在您的
main()
中:


此外,代码还有其他问题


例如,
GraphicGen
是一个
JPanel
主类,它有一个
JFrame
字段,当应用程序运行时,该字段实际上包含
GraphicGen
实例——这看起来很糟糕。我对Swing不太了解。。。是否必须调用包含的
JFrame
repaint()
,而不是调用
JPanel
repaint()
,如果有的话?

是否也可以将主类(它如何使用
ActiveList
)放在这里?例如,谁调用了
addToList
?我希望能更新它,让它更清晰。如果矩形列表已经有10个元素,您希望代码做什么?覆盖最后一个元素。我修复了那个逻辑位,但这并不是导致问题的原因。我知道ActiveList对象的默认构造函数是无用的,我是迫不得已才尝试的。我的问题是,当我在currentActiveList上调用genRandomRectangle()时,在它完成创建随机矩形后,它会使用addToList将随机矩形指定给当前对象的activeList。但是,当我返回到GraphicsGen并且如果执行(currentActiveList.getFromList(I)!=null){时,.getFromList(I)将始终返回null。我确实希望有两个JFrames,但我希望第二个JFrames上的ButtonPompt调用主JFrames上的repaint,我如何在不使按钮提示GraphicGen扩展的情况下执行该操作?
public class ButtonPrompt extends GraphicGen {
    JButton randomBtn = new JButton("Add Random Rectangle");        
    JButton inputCoordinates = new JButton("Input Rectangle Coordinates");
    final GraphicGen gg;

    public ButtonPrompt(GraphicGen gg) {
        this.gg = gg;
        ......

        ActionListener actionListenerRandom = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                gg.callGenRandom();
                gg.callRepaintOnMain();
            }
        };

        randomBtn.addActionListener(actionListenerRandom);

        ......
    }
}
public static void main(String[] args) {
    ...
    GraphicGen gg = new GraphicGen();
    mainFrame.add(gg);
    ...
    ButtonPrompt buttonPrompter = new ButtonPrompt(gg);
}