这是我的代码,当我运行它时,只显示瓷砖'd'。我想知道为什么当我在Java中运行时,只出现一个图块以及如何制作它们以便它们全部出现?感谢所有答案。
package code;
import javax.swing.JFrame;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JButton;
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();
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
f.setSize(width,height);
p.setVisible(true);
f.add(p);
f.pack();
f.setVisible(true);
Tile A = new Tile('a');
Tile B = new Tile('b');
Tile C = new Tile('c');
Tile D = new Tile('d');
ArrayList<Tile> tile = new ArrayList<Tile>();
tile.add(A);
tile.add(B);
tile.add(C);
tile.add(D);
for(int i=0; i < tile.size(); i++){
JButton j = new JButton("" + tile.get(i).getChar());
f.add(j);
}
}