Java JPanel上的元素未出现
我对使用swing进行GUI编程相当陌生,我确信这是一个noob问题 我创建了一个JFrame,里面有一个JPanel。然后我尝试为数组中的每个元素添加一个JLabel。问题是这些元素没有出现在面板上。我已经检查过,以确保数组元素正在使用println语句注册,所以这不是问题所在。我猜我在什么地方漏掉了一句话。。。请告知 这是我的密码:Java JPanel上的元素未出现,java,swing,Java,Swing,我对使用swing进行GUI编程相当陌生,我确信这是一个noob问题 我创建了一个JFrame,里面有一个JPanel。然后我尝试为数组中的每个元素添加一个JLabel。问题是这些元素没有出现在面板上。我已经检查过,以确保数组元素正在使用println语句注册,所以这不是问题所在。我猜我在什么地方漏掉了一句话。。。请告知 这是我的密码: public class MazeFrame extends javax.swing.JFrame { Maze m; /** * C
public class MazeFrame extends javax.swing.JFrame {
Maze m;
/**
* Creates new form MazeFrame
*/
public MazeFrame(Maze m) {
this.m = m;
setSize(new Dimension(800, 600));
JPanel pan = new JPanel();
add(pan);
setVisible(true);
// pan.setBackground(Color.yellow);
pan.setLayout(new GridLayout(m.width, m.height));
for (int curr = 0; curr < m.height; curr++){
for (Cell c: m.maze[curr]){
JLabel lab = new JLabel();
switch (c.state){
case border:
lab.setBackground(Color.black);
System.out.println("addedborder");
break;
case wall:
lab.setBackground(Color.DARK_GRAY);
System.out.println("addedwall");
break;
case open:
lab.setBackground(Color.LIGHT_GRAY);
System.out.println("addedopen");
break;
case travelled:
lab.setBackground(Color.RED);
}
lab.setSize(new Dimension(50, 50));
lab.setVisible(true);
pan.add(lab);
// System.out.println("added");
}
}
pan.revalidate();
pan.repaint();
}
}
公共类MazeFrame扩展了javax.swing.JFrame{
迷宫m;
/**
*创建新表单MazeFrame
*/
公共迷宫名称(迷宫m){
这个,m=m;
设置尺寸(新尺寸(800600));
JPanel pan=新的JPanel();
添加(pan);
setVisible(真);
//平底锅(颜色:黄色);
平移设置布局(新网格布局(m.宽度,m.高度));
对于(int curr=0;curr
下面是迷宫课程:
package robots;
import java.util.Random;
public class Maze {
public Cell[][] maze;
final int width;
final int height;
public Maze(){
width = 20;
height = 20;
maze = new Cell[width][height];
for (int row = 0; row < height; row++){
for (int col = 0; col < width; col++){
maze[row][col] = new Cell(row, col);
}
}
// set borders
for (int curr = 0; curr < height; curr++) {
maze[0][curr].setState("border");
maze[curr][0].setState("border");
maze[height - 1][curr].setState("border");
maze[curr][width - 1].setState("border");
}
// initially mark all cells as walls
for (int row = 1; row < height - 1; row++) {
for (int col = 1; col < width - 1; col++) {
maze[row][col].setState("wall");
}
}
}
private boolean isValidTurn(int row, int col) {
if (row >= 0 && col < width && col > 0 &&
row < 20 && (!this.maze[row][col].getState().matches("open"))) {
return true;
}
return false;
}
public void makeRoute() {
Random r = new Random();
int row = 0;
int col = r.nextInt(width);
maze[row][col].setState("open");
row = row+1;
maze[row][col].setState("open");
// System.out.println(this);
while (row < (this.height - 1)) {
// Assuming the mouse moves in only 3 directions left right or down
// in the maze. 0 indicates left turn 1 indicates right turn and
// 2 indicates down movement in the maze.
int nextDir = r.nextInt(3);
switch (nextDir) {
case 0: // left turn
if (this.isValidTurn(row, (col - 1))) {
--col;
this.maze[row][col].setState("open");
}
break;
case 1: // right turn
if (this.isValidTurn(row, (col + 1))) {
++col;
this.maze[row][col].setState("open");
}
break;
case 2: // down movement
++row;
this.maze[row][col].setState("open");
break;
}
System.out.println("turn : " + nextDir);
// System.out.println(this);
}
System.out.println(this);
}
}
class Cell {
int row;
int col;
int above, below, toLeft, toRight;
enum state {border, wall, open, travelled};
state state;
public Cell(int row, int col){
this.row = row;
this.col = col;
above = row + 1;
below = row -1;
toLeft = col -1;
toRight = col +1;
}
@Override
public String toString(){
String out = new String();
if (state == state.border) {
out = "0";
}
if (state == state.wall) {
out = "#";
}
if (state == state.open) {
out = ".";
}
if (state == state.open) {
out = "-";
}
return out;
}
public void setState(String toSet){
switch (toSet){
case "border":
state = state.border;
break;
case "wall":
state = state.wall;
break;
case "open":
state = state.open;
break;
case "travelled":
state = state.travelled;
break;
}
}
public String getState() {
return state.toString();
}
}
包装机器人;
导入java.util.Random;
公共类迷宫{
公共小区[]迷宫;
最终整型宽度;
最终整数高度;
公共迷宫(){
宽度=20;
高度=20;
迷宫=新单元[宽度][高度];
对于(int row=0;row=0&&col0&&
行<20&(!this.maze[row][col].getState().matches(“open”)){
返回true;
}
返回false;
}
public void makeRoute(){
随机r=新随机();
int行=0;
int col=r.nextInt(宽度);
迷宫[行][col].setState(“打开”);
行=行+1;
迷宫[行][col].setState(“打开”);
//System.out.println(本文件);
而(行<(此高度-1)){
//假设鼠标仅向左右或向下3个方向移动
//在迷宫中,0表示左转1表示右转
//2表示迷宫中的向下运动。
int-nextDir=r.nextInt(3);
开关(下一个){
案例0://左转
如果(此.isValidTurn(行,(列-1))){
--上校;
this.maze[row][col].setState(“打开”);
}
打破
案例1://右转
如果(此.isValidTurn(行,(列+1))){
++上校;
this.maze[row][col].setState(“打开”);
}
打破
案例2://向下运动
++行;
this.maze[row][col].setState(“打开”);
打破
}
System.out.println(“turn:+nextDir”);
//System.out.println(本文件);
}
System.out.println(本文件);
}
}
类单元{
int行;
int col;
上、下、左、右内景;
枚举状态{边界、墙、开放、移动};
国家;
公共单元格(整数行,整数列){
this.row=行;
this.col=col;
以上=行+1;
下=第1行;
toLeft=col-1;
toRight=col+1;
}
@凌驾
公共字符串toString(){
String out=新字符串();
if(state==state.border){
out=“0”;
}
if(state==state.wall){
out=“#””;
}
if(state==state.open){
out=“.”;
}
if(state==state.open){
out=“-”;
}
返回;
}
公共无效设置状态(字符串到设置){
开关(toSet){
“边界”一案:
state=state.border;
打破
案例“墙”:
state=state.wall;
打破
案例“未结”:
state=state.open;
打破
“旅行”一案:
state=state.traveled;
打破
}
}
公共字符串getState(){
返回状态。toString();
}
}
但是,正如我所说的,我知道maze类工作得很好,因为当我运行它时,它会完美地输出到控制台。另外,中的println语句
// update panel
pan.revalidate();
pan.repaint();
// adding panel to frame
this.add(pan);
this.pack();
this.setVisible(true);
lab.setOpaque(true);
public TestMaze(Maze m) {
this.m = m;
setSize(new Dimension(800, 600));
JPanel pan = new JPanel();
add(pan);
pan.setLayout(new GridLayout(m.width, m.height));
for (int curr = 0; curr < m.height; curr++) {
for (Cell c : m.maze[curr]) {
JLabel lab = new JLabel();
lab.setOpaque(true); // <-- Add me...
switch (c.state) {
case border:
lab.setBackground(Color.black);
break;
case wall:
lab.setBackground(Color.DARK_GRAY);
break;
case open:
lab.setBackground(Color.LIGHT_GRAY);
break;
case travelled:
lab.setBackground(Color.RED);
}
lab.setSize(new Dimension(50, 50));
lab.setVisible(true);
pan.add(lab);
// System.out.println("added");
}
}
setVisible(true);
}
public class MazeFrame extends javax.swing.JFrame {
Maze m;
/**
* Creates new form MazeFrame
*/
public MazeFrame(Maze m) {
this.m = m;
// Don't manually set the size of a frame. Let the preferred size of you components determine the size.
// This is done by invoking pack() after all components have been added to the frame.
// setSize(new Dimension(800, 600));
JPanel pan = new JPanel();
add(pan);
// setVisible(true); // do after all components added.
// pan.setBackground(Color.yellow);
pan.setLayout(new GridLayout(m.width, m.height));
for (int curr = 0; curr < m.height; curr++){
for (Cell c: m.maze[curr]){
JLabel lab = new JLabel();
lab.setOpaque(true); // as suggested by MadProgrammer
switch (c.state){
case border:
lab.setBackground(Color.black);
System.out.println("addedborder");
break;
case wall:
lab.setBackground(Color.DARK_GRAY);
System.out.println("addedwall");
break;
case open:
lab.setBackground(Color.LIGHT_GRAY);
System.out.println("addedopen");
break;
case travelled:
lab.setBackground(Color.RED);
}
// Set the preferred size so layout managers can do there job
// lab.setSize(new Dimension(50, 50));
lab.setPreferredSize(new Dimension(50, 50));
// Not required. This is the default for all components except top level containers like JFrame, JDialog
// lab.setVisible(true);
pan.add(lab);
// System.out.println("added");
}
}
// No neeed to revalidate or repaint because the frame is not visible yet
// pan.revalidate();
// pan.repaint();
pack(); // let the layout manager determine the size of the frame
setVisible(); // show the frame
}
}