在Java中的JFrame上绘制简单的矩形

8

我正在像这样扩展JFrame:

public GameFrame() {
    this.setBounds(30, 30, 500, 500);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    initializeSquares();
}

private void initializeSquares(){
    for(int i = 0; i < 5; i++){
        this.getContentPane().add(new Square(i*10, i*10, 100, 100));
    }
    this.setVisible(true);
}

然而,屏幕上只绘制了一个正方形,有人知道为什么吗?

同时,我的Square类看起来像这样:

private int x;
private int y;
private int width;
private int height;
public Square(int x, int y, int width, int height){
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawRect(x, y, width, height);
}
4个回答

13

JFrame的contentPane默认使用边界布局(BorderLayout)。当你添加一个Square到它上面时,它会默认以BorderLayout.CENTER方式添加并覆盖之前添加的任何Squares。你需要了解所有可用于Swing GUI的布局管理器。

例如,可以从这里开始:在容器内放置组件

不过,我会采用不同的方式来处理。我会创建一个JPanel,并使其能够绘制多个正方形。例如:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

public class GameFrame extends JFrame {
   public GameFrame() {
      super("Game Frame");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Squares squares = new Squares();
      getContentPane().add(squares);
      for (int i = 0; i < 15; i++) {
         squares.addSquare(i * 10, i * 10, 100, 100);
      }

      pack();
      setLocationRelativeTo(null);
      setVisible(true);

   }

   public static void main(String[] args) {
      new GameFrame();
   }

}

class Squares extends JPanel {
   private static final int PREF_W = 500;
   private static final int PREF_H = PREF_W;
   private List<Rectangle> squares = new ArrayList<Rectangle>();

   public void addSquare(int x, int y, int width, int height) {
      Rectangle rect = new Rectangle(x, y, width, height);
      squares.add(rect);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      for (Rectangle rect : squares) {
         g2.draw(rect);
      }
   }

}

好的,看起来就是这样了,有没有不使用布局管理器而直接使用绝对定位的方法呢? - wfbarksdale
您可以通过将容器的布局设置为“null”来实现,但这并不推荐。最好像上面的示例那样做。 - Hovercraft Full Of Eels

1
对于绝对定位,请调用setLayout(null)。然后图标将在其getLocation()方法返回的位置上绘制,因此您可能需要先调用setLocation()。

你需要做更多的工作,因为你将完全负责设置组件的位置和大小。但是,这并不是推荐的做法。 - Hovercraft Full Of Eels

0

我在使用绝对布局(即将布局设置为null)在jframe上显示多个矩形时遇到了问题。

JFrame frame = new JFrame("hello");
frame.setLayout(null);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.getContentPane().setBackground(Color.red);
frame.getContentPane().add(new Square(10,10,100,100));//My rectangle class is similar to the Square mentioned in the question and it extends JComponent.

我仍然遇到问题,因为矩形没有显示出来。除非你明确设置正方形(JComponent)的边界,否则它不会起作用。即使正方形在构造函数中传递了位置,setbounds也只是解决了问题。所以以下代码解决了这个问题,而且这个问题是针对绝对布局的jframe。

Square square = new Square(10,10,100,100);
square.setBounds(10,10,100,100);
frame.getContentPane().add(square);

0
我的简单代码是JPanel rect = new JPanel(); rect.setBounds(0,0,300,200); rect.setBackground(Color.BLUE); window.add(rect);

根据目前的写法,你的回答不够清晰。请编辑以添加更多细节,帮助其他人理解这如何回答所提出的问题。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接