在JPanel中放置JPanel

时间:2014-12-21 13:33:18

标签: java swing

我正在尝试编写一个简单的Pong,我有一个包含Bar面板的背景面板。所以当然我需要能够将尺寸放在尺寸上并根据要求垂直移动。现在我只想把它放在一个起始位置。如果我不禁用布局,则无论位置设置如何,都会将栏放置在顶部中心,但如果我禁用布局并设置位置,则它不会显示。我不确定我错过了什么。以下是一段代码片段:

public PongPanel() {
    setLayout(null);
    setPreferredSize(SIZE);
    setBackground(Color.BLACK);
    player_one_bar = new Bar();
    add(player_one_bar);
    player_one_bar.setLocation(10, getSize().height/2-3);
}

2 个答案:

答案 0 :(得分:3)

如果您将布局管理器设置为null,则必须指定面板的确切坐标,这意味着像 -

setBounds(10, 10, 20, 100);

将面板放在位置(10,10),宽度为20,高度为100。

答案 1 :(得分:3)

如果通过" bar"你的意思是Pong游戏 paddle ,那么它根本不应该是一个组件,而是一个代表一个位置的逻辑实体,它可以通过在JPanel&#39中绘制的精灵来直观地表示。 ; s paintComponent方法。

例如:

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class PongPaddle extends JPanel {
   private static final int PREF_W = 800;
   private static final int PREF_H = 500;
   private static final int RECT_X = 20;
   private static final int RECT_W = 10;
   private static final int RECT_H = 60;
   private static final int STARTING_Y = (PREF_H - RECT_H) / 2;
   private static final int TIMER_DELAY = 15;
   private static final int DELTA_PADDLE = 3;
   private boolean paddle1GoingDown = true;
   private boolean paddle2GoingDown = false;
   private Rectangle paddle1 = new Rectangle(RECT_X, STARTING_Y, RECT_W, RECT_H);
   private Rectangle paddle2 = new Rectangle(PREF_W - RECT_X - RECT_W,
         STARTING_Y, RECT_W, RECT_H);

   public PongPaddle() {
      setBackground(Color.black);
      new Timer(TIMER_DELAY, new TimerListener()).start();
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private class TimerListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         int deltaPaddle1 = paddle1GoingDown ? 1 : -1;
         deltaPaddle1 *= DELTA_PADDLE;

         int x = paddle1.getLocation().x;
         int y = paddle1.getLocation().y + deltaPaddle1;

         if (y + RECT_H >= PREF_H) {
            paddle1GoingDown = false;
         }
         if (y <= 0) {
            paddle1GoingDown = true;
         }         
         paddle1.setLocation(x, y);

         int deltaPaddle2 = paddle2GoingDown ? 1 : -1;
         deltaPaddle2 *= DELTA_PADDLE;

         x = paddle2.getLocation().x;
         y = paddle2.getLocation().y + deltaPaddle2;

         if (y + RECT_H >= PREF_H) {
            paddle2GoingDown = false;
         }
         if (y <= 0) {
            paddle2GoingDown = true;
         }         
         paddle2.setLocation(x, y);

         repaint();

         if (!PongPaddle.this.isShowing()) {
            ((Timer) e.getSource()).stop();
         }
      }
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setColor(Color.white);
      if (paddle1 != null) {
         g2.fill(paddle1);
      }
      if (paddle2 != null) {
         g2.fill(paddle2);
      }
   }

   private static void createAndShowGui() {
      PongPaddle mainPanel = new PongPaddle();

      JFrame frame = new JFrame("PongPaddle");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}