2

我正在为一个简单的战舰克隆设计一个简单的 GUI。我以前使用彩绘方块做过类似的事情,但决定用 JPanel 尝试一下。除了顶部的标签(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();
    }
}
4

1 回答 1

4

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

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

您的代码的工作版本可以在这里找到:

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();
    }
}
于 2013-07-09T18:34:56.170 回答