java - JFrame 中的 JPanel

标签 java swing jframe jpanel tetris

我刚开始学习 Java,现在正在为俄罗斯方 block 制作 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);

最佳答案

TetrisPanelmain 面板,那么你应该用 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 generated by the code

关于java - JFrame 中的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10637788/

相关文章:

java - 除了 "dev"之外,Spring redirectAttributes 无法在其他配置文件中工作

java - JList 换行问题

java - 我可以使 JFrame 看起来像 JWindow 吗?

java - 当未指定索引之一时,多维数组如何分配内存

java - J2EE 应用程序中的速度优化,客户端?

Java8 FileSystems.getDefault() 抛出由 WindowsPathParser 中的 NullPointerException 引起的 ExceptionInInitializerError

java - 从大量条目中实现选择框的最佳方法

java - 是否有适用于 Java 的 WYSIWYG 编辑器?

java - 需要设置frame.setResizable(false)为repaint()

java - 使用MS Access基础制作授权JFrame