Java 添加到父JPanel时,JPanel不显示
我在这里目瞪口呆。我有一个Java 添加到父JPanel时,JPanel不显示,java,swing,Java,Swing,我在这里目瞪口呆。我有一个JPanel(deboardpanel),我正在将它添加到父JPanel(GamePanel)中,如下所示: public GamePanel(SetupBoard sb) { this.setLayout(new BorderLayout()); // this.setBackground(Color.yellow); JPanel defBoardPanel = new JPanel(); defBoardPanel.setBac
JPanel
(deboardpanel),我正在将它添加到父JPanel
(GamePanel)中,如下所示:
public GamePanel(SetupBoard sb) {
this.setLayout(new BorderLayout());
// this.setBackground(Color.yellow);
JPanel defBoardPanel = new JPanel();
defBoardPanel.setBackground(Color.yellow);
for (int r = 0; r < sb.boardSize; r++){
for (int c = 0; c < sb.boardSize; c++){
Cell c = new Cell(r, c);
c.label.setOpaque(true);
if (sb.getCell(r, c).status == sb.getCell(r,c).status.occupied){
c.label.setBackground(Color.black);
System.out.println("LABEL IS OCCUPIED");
}
else {
c.label.setBackground(Color.white);
}
defBoardPanel.add(c.label);
}
}
defBoardPanel.revalidate();
defBoardPanel.setVisible(true);
this.add(defBoardPanel, BorderLayout.WEST);
this.revalidate();
this.setVisible(true);
我的问题是未显示子面板(defBoardPanel)。游戏面板本身会显示出来(我已经用setBackground(Color.yellow)
方法验证过了),但我添加到上面的面板不会显示出来
我在这里忽略了什么愚蠢的错误
编辑:正在从SetupBoard类中调用StartName():
void startGame(){
mf.startGame(this);
}
其中mf是对创建SetupBoard实例的大型机的引用。GamePanel显示的事实证实了它被正确调用 如果我修剪了我没有的代码,似乎可以正常工作。最有可能的问题是来自您没有向我们展示的内容。因此,生产一种新的产品将大大有利于您。同时,您可以随时利用(找出与代码的差异)以下代码,这一代码高度源自您的代码(我尽可能填补了一些空白):
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.GridLayout;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类游戏面板扩展了JPanel{
专用静态最终整数COLS=10;
私有静态最终整数行=10;
公共游戏小组(){
此.setLayout(新的BorderLayout());
//这个.背景(颜色.黄色);
JPanel defBoardPanel=新JPanel(新网格布局(行、列));
除霜板。立根地面(颜色。黄色);
对于(int r=0;r0.5){
标签。背景(颜色:黑色);
标签。设置前景(颜色。白色);
System.out.println(“标签被占用”);
}否则{
标签.背景(颜色.白色);
}
除雾板面板。添加(标签);
}
}
添加(defBoardPanel,BorderLayout.WEST);
}
公共静态类大型机扩展JFrame{
公共主机(){
此.setLayout(新的BorderLayout());
这个。设置大小(500500);
}
void startGame(){
GamePanel gp=新GamePanel();
这个.setContentPane(gp);
包装();
setVisible(真);
}
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
大型机=新的大型机();
mainFrame.startGame();
}
});
}
}
将游戏面板添加到大型机后,此代码序列将替换游戏面板
public MainFrame() {
this.setLayout(new BorderLayout());
this.setSize(500, 500);
SetupBoard sb = new SetupBoard(10, this); // invokes startGame
this.setContentPane(sb); <----- GamePanel replaced here
}
void startGame(SetupBoard sb) {
GamePanel gp = new GamePanel(sb);
this.setContentPane(gp);
this.revalidate();
}
公共大型机(){
此.setLayout(新的BorderLayout());
这个。设置大小(500500);
SetupBoard sb=新的SetupBoard(10,this);//调用startGame
此.setContentPane(sb);您是否真的调用了startGame
?为了更好地帮助您尽快发布@Reimeus是的,请参阅编辑。此外,我尝试创建SSCCE,但无法复制该行为。您似乎没有在defBoardPanel中放置任何组件,也没有为其指定特定的大小。defBoardPanel可能没有大小,因此您不是查看它。尝试设置面板的大小,然后在JFrame对象上调用pack(),根据面板和组件的大小重新定位所有内容。@Arkanon-我最初没有发布将组件添加到defBoardPanel的位置,但现在已经发布了。此外,我尝试设置面板的大小,但没有结果。您调用了该包吗()JFrame对象的方法?+1在大型机构造函数中调用setContentPane
将覆盖startName
发出的调用(如果startName
在SetupBoard
构造函数中被调用)。正如我所说的“问题很可能来自您没有向我们展示的内容。”;-)在构造函数中进行的调用,在调用startGame()
后不会被调用。startGame()在已经存在的大型机实例上被调用。另外,这不会导致再次显示设置板吗?不会,只会显示游戏面板,而不是我添加到其中的面板。@GuillaumePolet:startGame()
不是在SetupBoard构造函数中调用的,它是由一个单独的方法调用的,该方法是在SetupBoard按钮上的ActionListener
中调用的。@drewmore我只能建议提供一个。请不要犹豫,使用我提供的方法来显示您的问题。如果没有更多信息,将很难为您提供更多帮助。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GamePanel extends JPanel {
private static final int COLS = 10;
private static final int ROWS = 10;
public GamePanel() {
this.setLayout(new BorderLayout());
// this.setBackground(Color.yellow);
JPanel defBoardPanel = new JPanel(new GridLayout(ROWS, COLS));
defBoardPanel.setBackground(Color.yellow);
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++) {
JLabel label = new JLabel((r + 1) + " " + (c + 1));
label.setOpaque(true);
if (Math.random() > 0.5) {
label.setBackground(Color.black);
label.setForeground(Color.WHITE);
System.out.println("LABEL IS OCCUPIED");
} else {
label.setBackground(Color.white);
}
defBoardPanel.add(label);
}
}
this.add(defBoardPanel, BorderLayout.WEST);
}
public static class MainFrame extends JFrame {
public MainFrame() {
this.setLayout(new BorderLayout());
this.setSize(500, 500);
}
void startGame() {
GamePanel gp = new GamePanel();
this.setContentPane(gp);
pack();
setVisible(true);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainFrame mainFrame = new MainFrame();
mainFrame.startGame();
}
});
}
}
public MainFrame() {
this.setLayout(new BorderLayout());
this.setSize(500, 500);
SetupBoard sb = new SetupBoard(10, this); // invokes startGame
this.setContentPane(sb); <----- GamePanel replaced here
}
void startGame(SetupBoard sb) {
GamePanel gp = new GamePanel(sb);
this.setContentPane(gp);
this.revalidate();
}