简体   繁体   中英

How to add an array list to a jpanel?

So far this is my code.Ultimately, I need to create a small version of the Keybricks text Game and I'm having trouble adding the tiles to the frame. Can anyone explain how I can add this and then the tiles to the frame?

package code;

import javax.swing.JFrame;
import java.awt.Component;
import java.awt.LayoutManager;
import java.util.ArrayList;``
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.BoxLayout;



public class Game implements Runnable {
static final int width = 300;
static final int height = 500;


@Override
public void run(){

    JFrame f = new JFrame("KeyBricks Game");
    JPanel p = new JPanel();
    JLabel j = new JLabel();
    p.add(j);
    p.add(Tile);
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    f.setSize(width,height);
    p.setVisible(true);
    f.add(p);
    f.setVisible(true);
    Tile A = null;
    Tile B = null;
    Tile C = null;
    Tile D = null;
    ArrayList<Tile> Tile = new ArrayList<Tile>();
    for(int i=0; i < 4; i++){

        Tile.add(A);
            Tile.add(B);
            Tile.add(C);
            Tile.add(D);
    }
        }

In java, the variable name can't be the same as the object name, so instead of:

ArrayList<Tile> Tile = new ArrayList<Tile>();

use

ArrayList<Tile> tile = new ArrayList<Tile>();

and then make the modifications at the for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM