1

我刚刚开始学习 Java,现在我正在为俄罗斯方块制作 GUI。我想要这样的东西: 1 http://desmond.imageshack.us/Himg846/scaled.php?server=846&filename=tetrisf.png&res=landing

但我得到:
2

编辑:有人可以告诉我如何解决这个问题吗?

它是 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);
4

4 回答 4

3

TetrisPanel是主面板,那么您应该使用BorderLayout.CENTER而不是添加它BorderLayout.LINE_START

并且,参数正确的顺序是getContentPane(component, layoutOrientation)

下面是一个布局示例:

public class GuiExample extends JPanel {
    public GuiExample() {
        JPanel gamePanel = new JPanel();
        gamePanel.setBackground(Color.GREEN);
        gamePanel.setPreferredSize(new Dimension(300, 400));

        JPanel infoPanel = new JPanel();
        infoPanel.setBackground(Color.WHITE);

        JPanel previewPanel = new JPanel();
        previewPanel.setBackground(Color.BLUE);
        previewPanel.setPreferredSize(new Dimension(100, 100));

        JPanel pointsPanel = new JPanel();
        pointsPanel.setBackground(Color.RED);
        pointsPanel.setPreferredSize(new Dimension(100, 50));

        infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
        infoPanel.setPreferredSize(new Dimension(100, 400));
        infoPanel.add(previewPanel);
        infoPanel.add(pointsPanel);

        add(gamePanel, BorderLayout.CENTER);
        add(infoPanel, BorderLayout.EAST);
    }
    public static void main(String s[]) {
        JFrame frame = new JFrame("Java Rules");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new GuiExample());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

代码生成的JFrame

于 2012-05-17T16:27:01.133 回答
2

Just a side note, when you override paintComponent(...) method of the said JPanel/JComponent make it a customary habbit of overriding it's getPreferredSize() as well. When using a Layout Manager, prefer not to use any setXxXSize() methods of any sorts. Let Layout Manager handles that aspect. Is this what you looking for :

import java.awt.*;
import javax.swing.*;

public class Tetris extends JFrame
{
    private void createAndDisplayGUI()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GamePanel gp = new GamePanel();
        gp.createGUI();

        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BorderLayout());
        PreviewPanel pp = new PreviewPanel();
        pp.createGUI();

        JPanel pointsPanel = new JPanel();
        pointsPanel.setOpaque(true);
        pointsPanel.setBackground(Color.DARK_GRAY);
        JLabel pointsLabel = new JLabel("Points");
        pointsPanel.add(pointsLabel);
        rightPanel.add(pp, BorderLayout.PAGE_START);
        rightPanel.add(pointsPanel, BorderLayout.CENTER);

        getContentPane().add(gp, BorderLayout.CENTER);
        getContentPane().add(rightPanel, BorderLayout.LINE_END);

        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new Tetris().createAndDisplayGUI();
            }
        });
    }
}

class GamePanel extends JPanel
{
    public Dimension getPreferredSize()
    {
        return (new Dimension(500, 500));
    }

    protected void createGUI()
    {
        setOpaque(true);
        setBackground(Color.BLUE);
    }
}

class PreviewPanel extends JPanel
{
    public Dimension getPreferredSize()
    {
        return (new Dimension(200, 200));
    }

    protected void createGUI()
    {
        setOpaque(true);
        setBackground(Color.WHITE);
        JLabel label = new JLabel("Preview of the next Block.");
    }
}

And the output is :

TETRIS OUTPUT

于 2012-05-17T15:39:45.350 回答
2

我真的没有在任何地方看到任何问题,但我认为您的问题是,“为什么我的 GUI 看起来不像我期望的那样?” 或类似于“如何修复布局?”的内容。

您可以做的是获得一个 GUI Builder,例如 NetBeans,并在其中制作您的 GUI。然后像上面那样使用您的代码操作组件。至少这样你就可以保证你想要的 GUI 布局。剩下的就是功能。

于 2012-05-17T14:39:09.067 回答
0

BorderLayout.LINE_START不要使用and添加左右面板,而是BorderLayout.LINE_END尝试使用BorderLayout.CENTERand BorderLayout.EAST(分别)。

于 2012-05-17T14:45:24.810 回答