繁体   English   中英

JPanels没有出现在JFame中

[英]JPanels don't appear in the JFame

我是java的新手。 我确实有一个java类,但我想要领先。 我很喜欢! 这是我的问题。 我正在尝试绘制游戏所需的两个拨片。 我确实为它们创建了两个对象,它们都是“显示”,但会发生以下情况:

主要赛跑者

import javax.swing.JFrame;


public class PongRunner {

    public static void main (String[] args)
    {

        new PongRunner();
    }

    public PongRunner()
    {
        JFrame PongFrame= new JFrame();
        PongFrame.setSize(800,600);
        PongFrame.add(new PaddleB());
        PongFrame.add(new PaddleA());
        PongFrame.setLocationRelativeTo(null);
        PongFrame.setTitle("My Pong Game");
        PongFrame.setResizable(false);
        PongFrame.setVisible(true);
        PongFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

PaddleAPaddleB都是drawRect Graphics并绘制一个特定的矩形。

我告诉JFrame首先添加的图形是唯一可见的图形。 它下面的那个没有被添加到框架中我想知道为什么,以及我如何在同一个JFrame绘制两个拨片...我正在阅读我的书并尽可能多地查看互联网,但内心没有运气这两天。 一些帮助会很好。 我刚开始学习java,我想我正在取得进步。 一些提示也会很好谢谢:),特别是在actionListener因为我将不得不使用它们来移动桨!

两个PADDLES的源代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;

public class PaddleA extends JPanel implements ActionListener
{
    private Timer timer;

    public void timer()
    {
        timer = new Timer(25, this);
        timer.start();

    }

public void actionPerformed(ActionEvent e)
{
    repaint();
}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    g.setColor(Color.RED);
    g.fillRect(70,200,20,100);      
}
}

PaddleB:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;

public class PaddleB extends JPanel implements ActionListener
{

private Timer timer;

    public void timer()
    {
        timer = new Timer(25, this);
        timer.start();

    }

    public void actionPerformed(ActionEvent e)
    {
        repaint();
    }


    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.fillRect(500, 200, 20, 100);

    }

}

当您将2个拨片添加到主PongFrame的相同BorderLayout.CENTER位置时,将仅显示第二个拨片。

在一个JComponent上使用一个Graphics对象可以更轻松地完成这种类型的绘制。

Swing组件实际上并没有像这样使用。 你的每个paddle JPanel都试图绘制一个矩形,但容器(JFrame)及其布局管理器将控制JPanels的大小和位置。 因此,他们绘制的矩形可能超出其范围,并且根本不会出现。

一些布局管理器只会显示其中一个JPanels,如果你这样一个接一个地添加它们 - 请参阅Reimus的回答。

要做这样的动画图形,您可能需要从填充JFrame的单个JComponent(例如JPanel)开始,并将两个paddles作为矩形绘制到它上面。

这是因为你添加了第一个桨,然后在第一个桨上添加第二个桨,从而替换它们:

    PongFrame.add(new PaddleB());
    PongFrame.add(new PaddleA());

尝试使用LayoutManager这里只是一个快速示例:

    PongFrame.getContentPane().add(new PaddleB(),BorderLayout.WEST);
    PongFrame.getContentPane().add(new PaddleA(),BorderLayout.EAST);

你的g.fillRect(...); 具有x和y坐标,这些坐标对于JFrame来说太大,因此不会显示。 像这样改变它:

    g.fillRect(0,0, 20, 100);

虽然不相关:

  • 不要使用JFrame.add()而不是JFrame.getContentPane().add()

UPADTE:

正如其他人提到的那样,游戏的逻辑有点过时了。 您应该有一个添加到JFrame 绘图面板 ,并且所有绘图都是使用Graphic而不是JPanel在单个JPanel上完成的。 这是一个小例子:

在此输入图像描述

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PongRunner {

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

            @Override
            public void run() {
                new PongRunner();
            }
        });
    }

    public PongRunner() {
        JFrame PongFrame = new JFrame();
        PongFrame.setTitle("My Pong Game");
        PongFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PongFrame.setResizable(false);
        PongFrame.setSize(400,400);
        //PongFrame.setLocationRelativeTo(null);
        PongFrame.getContentPane().add(new DrawingPanel());
        PongFrame.setVisible(true);
    }
}

class DrawingPanel extends JPanel {

    Rect rect1, rect2;

    public DrawingPanel() {
        super(true);
        rect1 = new Rect(80, 80, 20, 100, Color.yellow);
        rect2 = new Rect(100, 200, 20, 100, Color.red);

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(rect1.getColor());
        g.fillRect(rect1.getX(), rect1.getY(), rect1.getWidth(), rect1.getHeight());
        g.setColor(rect2.getColor());
        g.fillRect(rect2.getX(), rect2.getY(), rect2.getWidth(), rect2.getHeight());
    }
}

class Rect {

    private Color color;
    private int x, y, height, width;

    Rect(int x, int y, int width, int height, Color color) {
        this.x = x;
        this.y = y;
        this.height = height;
        this.width = width;
        this.color = color;
    }

    public Color getColor() {
        return color;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, width, height);
    }
}

请注意这个片段并不完美,只是展示了绘制矩形以形成各种游戏所需的刻板逻辑

我没有时间尝试运行代码,但尝试设置“paddle”对象的大小和位置,或将它们设置为非不透明。 我相信Swing试图通过不绘制完全由另一个不透明组件覆盖的组件来加速渲染。 由于这个原因,它可能没有绘制你的一个桨。

如果你设置了paddle的大小和位置,你可能需要修改paintComponent方法:如果我的内存是正确的,我认为传入的Graphics对象被剪切到组件的区域,所以0,0会是组件的左上角。

您不应该通过JFrame#add(Component)方法将JPanel添加到JFrame 最好将它们添加到contentPane: frame.getContentPane().add(panel); 你应该阅读有关LayoutManager的内容。 他们可以通过在你的框架中构建组件来帮助你,或者如果你使用它们不正确就搞砸了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM