Java ArrayList不保留元素

时间:2015-05-23 13:38:29

标签: java user-interface arraylist

我写了一个简单的程序,用户可以在其中制作正方形。我有一个JOptionPane类,为用户提供3个选项:1。制作一个正方形,2。显示制作的正方形和0.退出。 我想将所有正方形存储在ArrayList中,因此当用户选择2时,它会返回所有制作的正方形,并使用format()方法。 所以JOptionPane类有一个静态arraylist,但它永远不会保存存储的方块。 这个JOptionPane类由主方法类Launcher调用。但当我按2显示所有方块时,它什么也没有返回。 还有一个非常简单的类Position,它使用x和y值设置certian位置。

班级广场:

package domain;
import java.awt.Color;

public class Square {

    private Color color;
    private int size;
    private Position position;

    public Square(Color color, int size, Position position) {
        this.setColor(color);
        this.setSize(size);
        this.setPosition(position);
    }

    public Color getColor() {
        return color;
    }
    public void setColor(Color color) {
        this.color = color;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public Position getPosition() {
        return position;
    }
    public void setPosition(Position position) {
        this.position = position;
    }

    public String format() {
        return "Square with size " + this.getSize() + " on " +     this.getPosition().format(); 
    }

}

JOptionPane类:

package ui;

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JColorChooser;
import javax.swing.JOptionPane;

import domain.Position;
import domain.Square;

public class JPaintMenu {

    public static ArrayList<Square> squares = new ArrayList<Square>();

    public int menu() {
        String choice = JOptionPane.showInputDialog("1. Create square\n2. Show squares\n\n0. Quit\nMake your choice:\n");
        return Integer.parseInt(choice);
    }

    public Square newSquare() {
        Color color = JColorChooser.showDialog(null, "Color of the square?", null);
        String sizeString = JOptionPane.showInputDialog("Size of the square?");
        int size = Integer.parseInt(sizeString);
        String xString = JOptionPane.showInputDialog("X coordinate?");
        int x = Integer.parseInt(xString);
        String yString = JOptionPane.showInputDialog("Y coordinate?");
        int y = Integer.parseInt(yString);

        Position position = new Position(x, y);
        Square square = new Square(color, size, position);
        squares.add(square);
        return square;
    }

    public String format() {
        String result = "";
        for(Square square : squares) {
            result += square.format();
        }
        return result;
    }

}

主要方法类Launcher:

package ui;
import javax.swing.JOptionPane;

public class Launcher {

    public static JPaintMenu menu = new JPaintMenu();

    public static void main(String[] args) {
        int result = menu.menu();
        if(result == 1) {
            menu.newSquare();
        }
        if (result == 2) {
            JOptionPane.showMessageDialog(null, menu.format());
        }
    }   
}

1 个答案:

答案 0 :(得分:4)

我认为(1)您将静态持久性混淆,或者(2)您的main方法应该在循环中实现。

使用static表示您不会将对象的状态存储到另一个对象中,而是在同一类型的所有对象上共享。 持久性存储意味着您存储对象的状态,以便下次调用程序时可以访问该对象。在这种情况下,您需要序列化数据;并将其存储到文件(或网络服务器,...)

另一种可能性是让程序多次查询用户;像:

int result = -1;
do {
    result = menu.menu();
    if(result == 1) {
        menu.newSquare();
    }
    if (result == 2) {
        JOptionPane.showMessageDialog(null, menu.format());
    }
} while(result == 1 || result == 2);