在java中的Jframe上绘制简单的矩形 [英] drawing simple rectangles on a Jframe in java

查看:55
本文介绍了在java中的Jframe上绘制简单的矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样扩展 JFrame:

I am extending JFrame like this:

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);
}

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

However, only one square is being drawn on the screen, does anybody know why?

My Square 类也是这样的:

also My Square class looks like this:

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);
}

推荐答案

JFrame的contentPane默认使用BorderLayout.当您向其中添加一个 Square 时,它​​会默认添加 BorderLayout.CENTER 并覆盖任何以前添加的 Squares.您将需要阅读 Swing GUI 可用的所有布局管理器.

The JFrame's contentPane uses BorderLayout by default. When you add a Square to it, it gets added by default BorderLayout.CENTER and covers up any previously added Squares. You will want to read up on all the layout managers available to Swing GUI's.

例如,从这里开始:在容器中布置组件

但话虽如此,我会做不同的事情.我将只创建一个 JPanel 并使其能够绘制多个正方形.例如这样的:

But having said this, I would do things differently. I would create just one JPanel and make it able to paint multiple squares. For example something like so:

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);
      }
   }

}

这篇关于在java中的Jframe上绘制简单的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆