我剛開始學習Java,現在我正在爲俄羅斯方塊製作GUI。我想有這樣的事情: 1 http://desmond.imageshack.us/Himg846/scaled.php?server=846&filename=tetrisf.png&res=landingJFrame中的JPanel
,但我得到:
2 http://img835.imageshack.us/img835/8466/tetrisissue.png
編輯:有人能告訴我如何解決這一問題?
這是一個TetrisFrame的(擴展的JFrame)構造的一部分:
setLayout(new BorderLayout());
/* adding a panel of the left side */
left = new TetrisPanel(Constants.WIDTH, Constants.HEIGHT);
getContentPane().add(BorderLayout.LINE_START, left);
left.setSize(Constants.WIDTH * Constants.BLOCK_SIZE,
Constants.HEIGHT * Constants.BLOCK_SIZE);
/* adding a panel on the right side */
JPanel right = new JPanel();
getContentPane().add(BorderLayout.LINE_END, right);
nextPiecePreview = new TetrisPanel(4 , 4);
points = new JLabel("0");
right.add(nextPiecePreview);
setSize(Constants.WIDTH * Constants.BLOCK_SIZE + Constants.RIGHT_MENU_SIZE,
Constants.HEIGHT * Constants.BLOCK_SIZE + Constants.BOTTOM_MARGIN);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
這是我TetrisPanel類的一部分:
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBounds(0, 0, Constants.BLOCK_SIZE * width ,
Constants.BLOCK_SIZE * height);
for(int i = 0; i < width; ++i) {
for(int j = 0; j < height; ++j) {
drawSquare(g, i, j, colorsMap.get(BlocksType.Z_SHAPED));
}
}
}
private void drawSquare (Graphics g, int x_temp, int y_temp, Color color) {
int x = x_temp * Constants.BLOCK_SIZE;
int y = y_temp * Constants.BLOCK_SIZE;
g.setColor(color);
g.fillRect(x + 1, y + 1, Constants.BLOCK_SIZE - 2, Constants.BLOCK_SIZE - 2);
g.setColor(color.brighter());
g.drawLine(x, y + Constants.BLOCK_SIZE - 1, x, y);
g.drawLine(x, y, x + Constants.BLOCK_SIZE - 1, y);
g.setColor(color.darker());
g.drawLine(x + 1, y + Constants.BLOCK_SIZE - 1,
x + Constants.BLOCK_SIZE - 1, y + Constants.BLOCK_SIZE - 1);
g.drawLine(x + Constants.BLOCK_SIZE - 1, y + Constants.BLOCK_SIZE - 1,
x + Constants.BLOCK_SIZE - 1, y + 1);
非常感謝。其實,這是最好的答案。 – nme