简体   繁体   English

JPanels没有出现在JFame中

[英]JPanels don't appear in the JFame

I'm new to java. 我是java的新手。 I do have a java class but I want to get ahead. 我确实有一个java类,但我想要领先。 I really like it! 我很喜欢! This is my problem. 这是我的问题。 I'm trying to draw the two paddles needed for the game. 我正在尝试绘制游戏所需的两个拨片。 I did create 2 objects for them, and they both "Show" but the following happens: 我确实为它们创建了两个对象,它们都是“显示”,但会发生以下情况:

Main Runner 主要赛跑者

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

}

PaddleA and PaddleB are both drawRect Graphics and draw a specific rectangle. PaddleAPaddleB都是drawRect Graphics并绘制一个特定的矩形。

The graphics that I tell JFrame to add first are the only ones visible. 我告诉JFrame首先添加的图形是唯一可见的图形。 The one under it doesnt get added into the Frame I wanted to know why,and how i can draw both paddles in the same JFrame ... I'm reading my book and looking over the internet as much as possible, but no luck within these 2 days. 它下面的那个没有被添加到框架中我想知道为什么,以及我如何在同一个JFrame绘制两个拨片...我正在阅读我的书并尽可能多地查看互联网,但内心没有运气这两天。 Some help would be nice. 一些帮助会很好。 I just started learning java and I think I'm making progress. 我刚开始学习java,我想我正在取得进步。 Some tips would be nice too thanks :), specialy on actionListener since im going to have to use them to move the paddles! 一些提示也会很好谢谢:),特别是在actionListener因为我将不得不使用它们来移动桨!

SOURCE CODE OF BOTH PADDLES: 两个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: 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);

    }

}

As you've added the 2 paddles into the same BorderLayout.CENTER location of the main PongFrame , only the second paddle will be displayed. 当您将2个拨片添加到主PongFrame的相同BorderLayout.CENTER位置时,将仅显示第二个拨片。

This type of painting is more easily done using one Graphics object on one JComponent . 在一个JComponent上使用一个Graphics对象可以更轻松地完成这种类型的绘制。

Swing components aren't really designed to be used like this. Swing组件实际上并没有像这样使用。 Each of your paddle JPanels is trying to paint a rectangle, but the container (the JFrame) and its layout manager will control the size and location of the JPanels. 你的每个paddle JPanel都试图绘制一个矩形,但容器(JFrame)及其布局管理器将控制JPanels的大小和位置。 So the rectangle they paint may be outside their bounds, and won't appear at all. 因此,他们绘制的矩形可能超出其范围,并且根本不会出现。

Some Layout Managers will only show one of the JPanels if you add them one after another like this - see answer by Reimus . 一些布局管理器只会显示其中一个JPanels,如果你这样一个接一个地添加它们 - 请参阅Reimus的回答。

To do animated graphics like this, you probably need to start with a single JComponent (eg a JPanel) that fills the JFrame, and paint both paddles as rectangles onto it. 要做这样的动画图形,您可能需要从填充JFrame的单个JComponent(例如JPanel)开始,并将两个paddles作为矩形绘制到它上面。

This is because you add the first paddle and then add the second paddle over the first thus replacing them: 这是因为你添加了第一个桨,然后在第一个桨上添加第二个桨,从而替换它们:

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

try using LayoutManager s here is just a quick sample: 尝试使用LayoutManager这里只是一个快速示例:

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

also your g.fillRect(...); 你的g.fillRect(...); has x and y co-ordinates that are too large for the JFrame and thus are not displayed. 具有x和y坐标,这些坐标对于JFrame来说太大,因此不会显示。 Change it like so: 像这样改变它:

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

Not related though: 虽然不相关:

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

UPADTE: UPADTE:

As others have mentioned the logic for the game is a bit off. 正如其他人提到的那样,游戏的逻辑有点过时了。 You should have a single drawing panel which is added to the JFrame and all drawing is done on the single JPanel using Graphic s and not JPanel s. 您应该有一个添加到JFrame 绘图面板 ,并且所有绘图都是使用Graphic而不是JPanel在单个JPanel上完成的。 Here is a small example: 这是一个小例子:

在此输入图像描述

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

Please note this snippet is not perfect and merely demonstrates the stereotypical logic needed for drawing rectangles to form a game of sorts 请注意这个片段并不完美,只是展示了绘制矩形以形成各种游戏所需的刻板逻辑

I don't have time to try running your code, but try setting the size and position of the "paddle" objects, or set them as non-opaque. 我没有时间尝试运行代码,但尝试设置“paddle”对象的大小和位置,或将它们设置为非不透明。 I believe that Swing tries to speed up rendering by not drawing components which are completely covered by another opaque component. 我相信Swing试图通过不绘制完全由另一个不透明组件覆盖的组件来加速渲染。 It may not be drawing one of your paddles for that reason. 由于这个原因,它可能没有绘制你的一个桨。

If you do set the size and position of your paddles, you may have to modify your paintComponent methods: if my memory is correct, I think the Graphics object which is passed in is clipped to the area of the component, so 0,0 would be the top-left corner of the component. 如果你设置了paddle的大小和位置,你可能需要修改paintComponent方法:如果我的内存是正确的,我认为传入的Graphics对象被剪切到组件的区域,所以0,0会是组件的左上角。

you shouldn't add your JPanels to your JFrame by the JFrame#add(Component) method. 您不应该通过JFrame#add(Component)方法将JPanel添加到JFrame Better add them to the contentPane: frame.getContentPane().add(panel); 最好将它们添加到contentPane: frame.getContentPane().add(panel); You should although read about LayoutManager s. 你应该阅读有关LayoutManager的内容。 They can help you by positining Components in your Frame or mess things up if you use them incorrect. 他们可以通过在你的框架中构建组件来帮助你,或者如果你使用它们不正确就搞砸了。

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

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