如何将 JPanel 添加到 JFrame 十次 [重复]

Posted

技术标签:

【中文标题】如何将 JPanel 添加到 JFrame 十次 [重复]【英文标题】:How to add a JPanel to a JFrame Ten Times [duplicate] 【发布时间】:2017-04-17 00:21:27 【问题描述】:

我的目标是创建一个随机的汽车或卡车十次,它应该有一个随机的位置。当我尝试向我的 JFrame 添加大量 JPanel 时,似乎只出现了一个。如何调用 randomVehicle() 方法一次绘制不止一辆车辆,我尝试添加打印语句并检查我的逻辑,但我不知所措。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
public class GraphicsTester 

    public static void main(String[] args) 
        //1. Create the frame.
        JFrame frame = new JFrame();
        //2. Optional: What happens when the frame closes?
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //4. Size the frame.
        frame.setSize(2000, 2000);
        //5. Show it.

        Vehicle frame1 = new Vehicle();
        frame.add(frame1.randomVehicle());
        frame.add(frame1.randomVehicle());
        frame.setVisible(true);
    


车辆类别

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Graphics;
public class Vehicle extends JPanel 

    public Vehicle() 
    xPos = (int) (Math.random() * 500) + 1;
    yPos = (int) (Math.random() * 500) + 1;
    

    public Vehicle randomVehicle() 
        int vehicleRandom = (int) (Math.random() * 2) + 1;
        System.out.println(vehicleRandom);
        if (vehicleRandom==1) 
            return new Car();
        
        if (vehicleRandom==2) 
            return new Truck();
        
        return null;
    


    protected int xPos;
    protected int yPos;


汽车

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;

public class Car extends Vehicle 

    public Car() 
        super();
        System.out.println(xPos);
        System.out.println(yPos);
    

    public void paint(Graphics g) 
        Graphics2D g2d = (Graphics2D) g;
        g2d.draw(new Rectangle(xPos + 100, yPos + 100, 200, 50));
        g2d.draw(new Rectangle(xPos + 150, yPos + 85, 100, 15));
        g2d.draw(new Ellipse2D.Double(xPos + 120, yPos + 150, 50, 35));
        g2d.draw(new Ellipse2D.Double(xPos + 220, yPos + 150, 50, 35));
    


卡车

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;

public class Truck extends Vehicle 

    public Truck() 
        super();
        System.out.println(xPos);
        System.out.println(yPos);
    

    public void paint(Graphics g) 
        Graphics2D g2d = (Graphics2D) g;
        g2d.draw(new Rectangle(xPos + 100, yPos + 100, 50, 75));
        g2d.draw(new Ellipse2D.Double(xPos + 110, yPos + 175, 35, 35));
        g2d.draw(new Rectangle(xPos + 180, yPos + 70, 350, 106));
        g2d.draw(new Ellipse2D.Double(xPos + 190, yPos + 175, 35, 35));
        g2d.draw(new Ellipse2D.Double(xPos + 225, yPos + 175, 35, 35));
        g2d.draw(new Ellipse2D.Double(xPos + 475, yPos + 175, 35, 35));
        g2d.draw(new Ellipse2D.Double(xPos + 440, yPos + 175, 35, 35));
    


【问题讨论】:

public class Vehicle extends JPanel .. 我会为此使用自定义绘画。所以Vehicle不应该继承JPanel,而是声明CarTruck需要实现的public abstract void draw(Graphics);方法。 【参考方案1】:

使用 for 循环.. 如下所示..

Vehicle frame1 = new Vehicle();

for(int i=0;i<10;i++) 
frame.add(frame1.randomVehicle());


frame.setVisible(true);

【讨论】:

我试过了,问题是即使调用了10次,也只画了一辆车。 这是因为第二个组件是在第一个组件之上添加的.. 参考这个..***.com/questions/13881444/…

以上是关于如何将 JPanel 添加到 JFrame 十次 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 JPanel 和 JFrame 的 contentPane 之间添加间距?

如何在不覆盖JFrame的情况下将JPanel图形添加到JFrame

Jpanel 和 Jframe 在运行时分别显示

将 JTextArea 添加到 JFrame

如何在JFrame上显示带有图像的两个JPanel,并且两个img都可见?

如何将 JPanel 放在 JFrame 的前面