如何将多个 类 添加到单个 JFrame?
How to add multiple classes to a single JFrame?
所以我尝试使用 JPanel 'panel' 向我的 JFrame 'frame' 添加多个 classes,但它似乎没有任何效果。这是我的主要 class:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame
{
public static void main (String[] args)
{
JPanel panel = new JPanel();
panel.setBackground (Color.WHITE);
panel.add (new Player()); // Class with paintComponent method.
panel.add (new Terrain()); // Class with paintComponent method.
JFrame frame = new JFrame ("Java Game");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (1000, 600);
frame.getContentPane().add (panel);
frame.setVisible (true);
}
}
当我 运行 程序时,JFrame 显示为白色背景,但 Player 和 Terrain classes 的 paintComponent 方法未被调用,因此没有其他内容被渲染.这段代码有什么问题吗?谢谢
编辑:这是我的玩家和地形 classes:
玩家:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
@SuppressWarnings ("serial")
public class Player extends JComponent
{
int x = 50;
int y = 450;
public void paintComponent (Graphics graphics)
{
graphics.setColor (Color.BLACK);
graphics.fillRect (x, y, 50, 50);
}
}
地形:
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JComponent;
@SuppressWarnings ("serial")
public class Terrain extends JComponent
{
Player playerClass = new Player();
public void paintComponent (Graphics graphics)
{
graphics.setColor (Color.GREEN);
graphics.fillRect (0, 500, 1000, 500);
}
}
- 您未能覆盖
Player
和 Terrain
的 getPreferredSize
,导致它们以 0x0
的默认大小进行布局
- 您没有调用
super.paintComponent
就破坏了绘画链,这可能会导致绘画问题和瑕疵无休止
Terrain
中Player
的引用与屏幕上的引用无关
查看 Laying Out Components Within a Container, Painting in AWT and Swing and Performing Custom Painting 了解更多详情
所以我尝试使用 JPanel 'panel' 向我的 JFrame 'frame' 添加多个 classes,但它似乎没有任何效果。这是我的主要 class:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame
{
public static void main (String[] args)
{
JPanel panel = new JPanel();
panel.setBackground (Color.WHITE);
panel.add (new Player()); // Class with paintComponent method.
panel.add (new Terrain()); // Class with paintComponent method.
JFrame frame = new JFrame ("Java Game");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (1000, 600);
frame.getContentPane().add (panel);
frame.setVisible (true);
}
}
当我 运行 程序时,JFrame 显示为白色背景,但 Player 和 Terrain classes 的 paintComponent 方法未被调用,因此没有其他内容被渲染.这段代码有什么问题吗?谢谢
编辑:这是我的玩家和地形 classes:
玩家:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
@SuppressWarnings ("serial")
public class Player extends JComponent
{
int x = 50;
int y = 450;
public void paintComponent (Graphics graphics)
{
graphics.setColor (Color.BLACK);
graphics.fillRect (x, y, 50, 50);
}
}
地形:
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JComponent;
@SuppressWarnings ("serial")
public class Terrain extends JComponent
{
Player playerClass = new Player();
public void paintComponent (Graphics graphics)
{
graphics.setColor (Color.GREEN);
graphics.fillRect (0, 500, 1000, 500);
}
}
- 您未能覆盖
Player
和Terrain
的getPreferredSize
,导致它们以0x0
的默认大小进行布局
- 您没有调用
super.paintComponent
就破坏了绘画链,这可能会导致绘画问题和瑕疵无休止 Terrain
中Player
的引用与屏幕上的引用无关
查看 Laying Out Components Within a Container, Painting in AWT and Swing and Performing Custom Painting 了解更多详情