簡體   English   中英

Java ArrayList不保留元素

[英]Java ArrayList not keeping elements

我編寫了一個簡單的程序,用戶可以在其中進行正方形運算。 我有一個JOptionPane類,為用戶提供3個選項:1.制作一個正方形; 2.顯示制作的正方形; 0。退出。 我想將所有平方存儲在ArrayList中,以便當用戶選擇2時,它使用format()方法返回所有已制成的平方。 因此,JOptionPane類具有靜態的arraylist,但從不保留存儲的正方形。 主方法類Launcher調用此JOptionPane類。 但是,當我按2顯示所有正方形時,它什么也不返回。 還有一個非常簡單的Position類,它用x和y值設置一個檢出位置。

班級廣場:

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)您將static持久性混淆,要么(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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM