簡體   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