Java Swing显示一些jLabel,但不显示其他jLabel

Java Swing显示一些jLabel,但不显示其他jLabel,java,swing,jlabel,repaint,Java,Swing,Jlabel,Repaint,我正在为一个简单的battlships克隆设计一个简单的GUI。我以前也曾使用画框做过类似的事情,但我决定用JPanels试试。除了顶部(1-14)的标签外,这里的一切都按照我的预期工作。它显示了1到4之间的任意位置,但从来没有全部显示14个,而且显示多少似乎有点随机。这段代码应该运行,以便问题很容易被发现。打印语句仅用于确认值是否正确。谢谢你的帮助 import java.awt.Color; import java.awt.Container; import java.awt.Dimensi

我正在为一个简单的battlships克隆设计一个简单的GUI。我以前也曾使用画框做过类似的事情,但我决定用JPanels试试。除了顶部(1-14)的标签外,这里的一切都按照我的预期工作。它显示了1到4之间的任意位置,但从来没有全部显示14个,而且显示多少似乎有点随机。这段代码应该运行,以便问题很容易被发现。打印语句仅用于确认值是否正确。谢谢你的帮助

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Foo {
    private JFrame frame;
    private JPanel[][] opponentBoard;
    private JLabel[] coordLabels;
    private final int FRAME_HEIGHT = 800;
    private final int FRAME_WIDTH = 600;
    private final int SQ_SIZE = 30;
    public Foo() {
        frame = new JFrame();
        frame.setBounds(new Rectangle(FRAME_WIDTH, FRAME_HEIGHT));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.RED); // for debugging porpoises
        frame.setLayout(null);

        Container container = frame.getContentPane();
        container.setBackground(Color.BLACK);
        container.setVisible(true);

        opponentBoard = new JPanel[15][11];

        coordLabels = new JLabel[24];

        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 11; j++) {
                opponentBoard[i][j] = new JPanel();
                container.add(opponentBoard[i][j]);
                opponentBoard[i][j].setLocation(i + i * SQ_SIZE, j + j * SQ_SIZE);
                opponentBoard[i][j].setSize(new Dimension(SQ_SIZE, SQ_SIZE));
                opponentBoard[i][j].setVisible(true);
                if ((i == 0) ^ (j == 0)) {
                    opponentBoard[i][j].setBackground(Color.GRAY);
                    if (i == 0) {
                        coordLabels[j - 1] = new JLabel((char) (j + 64) + "");
                        opponentBoard[i][j].add(coordLabels[j - 1]);
                        System.out.println(j-1);
                    }
                    if (j == 0) {
                        coordLabels[i + 9] = new JLabel(i + "");
                        System.out.println(i + 9 + "    " + i + " " + coordLabels[i + 9].getText());
                        opponentBoard[i][j].add(coordLabels[i + 9]);
                    }
                    opponentBoard[i][j].repaint();
                } else {
                    opponentBoard[i][j].setBackground(Color.DARK_GRAY);
                }
            }
        }
        opponentBoard[0][0].setBackground(Color.BLACK);
    }

    public static void main(String[] args){
        new Foo();
    }
}
导入java.awt.Color;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.awt.Rectangle;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公开课Foo{
私有JFrame;
私人JPanel[][]对手板;
私有JLabel[]合作标签;
专用最终内部框架高度=800;
专用最终整数帧_宽度=600;
私人最终整容面积=30;
公共食物({
frame=新的JFrame();
框.立根(新矩形(框宽、框高));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.RED);//用于调试海豚
frame.setLayout(空);
Container=frame.getContentPane();
容器.立根(颜色.黑色);
container.setVisible(true);
opponentBoard=新JPanel[15][11];
coordLabels=新的JLabel[24];
对于(int i=0;i<15;i++){
对于(int j=0;j<11;j++){
对面板[i][j]=新JPanel();
添加(对面板[i][j]);
对置板[i][j]。设置位置(i+i*SQ_尺寸,j+j*SQ_尺寸);
对置板[i][j]。设置尺寸(新尺寸(SQ_尺寸,SQ_尺寸));
对面板[i][j]。设置可见(真);
如果((i==0)^(j==0)){
对向板[i][j].立根台(颜色.灰色);
如果(i==0){
coordLabels[j-1]=新的JLabel((char)(j+64)+“”);
对面板[i][j]。添加(coordLabels[j-1]);
系统输出打印LN(j-1);
}
如果(j==0){
coordLabels[i+9]=新的JLabel(i+“”);
System.out.println(i+9+“”+i+“”+coordLabels[i+9].getText());
对面板[i][j]。添加(coordLabels[i+9]);
}
对面板[i][j]。重新绘制();
}否则{
对向板[i][j]。挫折背景(颜色:深灰色);
}
}
}
对置板[0][0]。立根台(颜色.黑色);
}
公共静态void main(字符串[]args){
新Foo();
}
}

将所有面板添加到容器后,需要调用
container.validate()
。此外,我将在内部for循环中删除对
repaint()
的调用

或者,在添加面板之前,不能将框架设置为可见;这性能更好

可以在以下位置找到代码的工作版本:

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Foo {
    private JFrame frame;
    private JPanel[][] opponentBoard;
    private JLabel[] coordLabels;
    private final int FRAME_HEIGHT = 800;
    private final int FRAME_WIDTH = 600;
    private final int SQ_SIZE = 30;
    public Foo() {
        frame = new JFrame();
        frame.setBounds(new Rectangle(FRAME_WIDTH, FRAME_HEIGHT));
        //frame.setVisible(true); Build the UI before making it visible
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.RED); // for debugging porpoises
        frame.setLayout(null);

        Container container = frame.getContentPane();
        container.setBackground(Color.BLACK);
        container.setVisible(true);

        opponentBoard = new JPanel[15][11];

        coordLabels = new JLabel[24];

        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 11; j++) {
                opponentBoard[i][j] = new JPanel();
                container.add(opponentBoard[i][j]);
                opponentBoard[i][j].setLocation(i + i * SQ_SIZE, j + j * SQ_SIZE);
                opponentBoard[i][j].setSize(new Dimension(SQ_SIZE, SQ_SIZE));
                opponentBoard[i][j].setVisible(true);
                if ((i == 0) ^ (j == 0)) {
                    opponentBoard[i][j].setBackground(Color.GRAY);
                    if (i == 0) {
                        coordLabels[j - 1] = new JLabel((char) (j + 64) + "");
                        opponentBoard[i][j].add(coordLabels[j - 1]);
                        System.out.println(j-1);
                    }
                    if (j == 0) {
                        coordLabels[i + 9] = new JLabel(i + "");
                        System.out.println(i + 9 + "    " + i + " " + coordLabels[i + 9].getText());
                        opponentBoard[i][j].add(coordLabels[i + 9]);
                    }
                    //opponentBoard[i][j].repaint();
                } else {
                    opponentBoard[i][j].setBackground(Color.DARK_GRAY);
                }
            }
        }
        opponentBoard[0][0].setBackground(Color.BLACK);

        // If the UI is already visible call validate
        // I've chose to not make the frame visible until all of the children
        // have been added so the call to validate isn't really needed.
        //container.validate();
        frame.setVisible(true);
    }

    public static void main(String[] args){
        new Foo();
    }
}
导入java.awt.Color;
导入java.awt.Container;
导入java.awt.Dimension;
导入java.awt.Rectangle;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公开课Foo{
私有JFrame;
私人JPanel[][]对手板;
私有JLabel[]合作标签;
专用最终内部框架高度=800;
专用最终整数帧_宽度=600;
私人最终整容面积=30;
公共食物({
frame=新的JFrame();
框.立根(新矩形(框宽、框高));
//setVisible(true);在使UI可见之前构建UI
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.RED);//用于调试海豚
frame.setLayout(空);
Container=frame.getContentPane();
容器.立根(颜色.黑色);
container.setVisible(true);
opponentBoard=新JPanel[15][11];
coordLabels=新的JLabel[24];
对于(int i=0;i<15;i++){
对于(int j=0;j<11;j++){
对面板[i][j]=新JPanel();
添加(对面板[i][j]);
对置板[i][j]。设置位置(i+i*SQ_尺寸,j+j*SQ_尺寸);
对置板[i][j]。设置尺寸(新尺寸(SQ_尺寸,SQ_尺寸));
对面板[i][j]。设置可见(真);
如果((i==0)^(j==0)){
对向板[i][j].立根台(颜色.灰色);
如果(i==0){
coordLabels[j-1]=新的JLabel((char)(j+64)+“”);
对面板[i][j]。添加(coordLabels[j-1]);
系统输出打印LN(j-1);
}
如果(j==0){
coordLabels[i+9]=新的JLabel(i+“”);
System.out.println(i+9+“”+i+“”+coordLabels[i+9].getText());
对面板[i][j]。添加(coordLabels[i+9]);
}
//对面板[i][j]。重新绘制();
}否则{
对向板[i][j]。挫折背景(颜色:深灰色);
}
}
}
对置板[0][0]。立根台(颜色.黑色);
//如果UI已经可见,请调用validate
//我选择在所有的孩子都看到之前不让框架可见
//已添加,因此实际上不需要调用验证。
//container.validate();
frame.setVisible(true);
}
公共静态void main(字符串[]args){
新Foo();
}
}

您需要调用
container.validate()
毕竟