简体   繁体   English

如何将数组列表添加到 jpanel?

[英]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.到目前为止,这是我的代码。最终,我需要创建 Keybricks 文本游戏的小版本,但在将图块添加到框架时遇到了问题。 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:在java中,变量名不能与对象名相同,所以改为:

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

use

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

and then make the modifications at the for loop.然后在 for 循环中进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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