2
我正在爲簡單的battlships克隆設計一個簡單的GUI。我以前使用塗色方塊做過類似的事情,但決定用JPanel來嘗試。除了頂部的標籤(1-14)外,這裏的一切都按我的意圖工作。它顯示1到4之間的任何地方,但從來沒有全部14,並且它似乎有點隨機有多少出現。此代碼應該運行,以便問題很容易看到。打印語句只是爲了確認這些值是否正確。謝謝您的幫助!Swing顯示一些JLabels但不是其他人
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();
}
}
精彩!謝謝! – rbauer