Java 为什么可以';我不能画一张JPanel吗?
它没有画任何东西。为什么?我已经定义了paint component方法,所以我不明白为什么。Java 为什么可以';我不能画一张JPanel吗?,java,swing,Java,Swing,它没有画任何东西。为什么?我已经定义了paint component方法,所以我不明白为什么。 编辑:我编辑了我的代码,请看一看您从未创建游戏类的实例,也从未将游戏类添加到框架中。即使您确实创建了一个实例,其大小仍然是(0,0),因此没有任何可绘制的内容 基本上,代码的整个结构都是错误的 我建议您重新开始,从上的Swing教程中的演示代码开始 您不会创建游戏类的实例,也不会将其添加到JFrame中 这是您正在绘制的游戏面板: Game.java public void display() {
编辑:我编辑了我的代码,请看一看您从未创建
游戏
类的实例,也从未将游戏
类添加到框架中。即使您确实创建了一个实例,其大小仍然是(0,0),因此没有任何可绘制的内容
基本上,代码的整个结构都是错误的
我建议您重新开始,从上的Swing教程中的演示代码开始 您不会创建
游戏
类的实例,也不会将其添加到JFrame
中
这是您正在绘制的游戏面板:
Game.java
public void display() {
pan.repaint();
fen.add(pan);
fen.addKeyListener(this);
fen.setResizable(false);
fen.setTitle("Le Jeu 2D");
img.setText("Coucou");
pan.add(img);
pan.repaint();
pan.setBackground(Color.yellow);
fen.setVisible(true);
fen.setSize(480, 272);
pan.repaint();
fen.revalidate();
}
public void paintComponent(Graphics g) {
System.out.println("zzz");
pan.paint(g);
g.setColor(Color.red);
g.drawRect(10, 10, 10, 10);
}
public class Game extends JPanel {
@Override
public final void paintComponent(final Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawRect(10, 10, 10, 10);
}
@Override
public Dimension getMinimumSize() {
return new Dimension(300, 300);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(300, 300);
}
}
public class GameFrame extends JFrame {
public GameFrame() {
setLocationRelativeTo(null);
setResizable(false);
setTitle("2D Game");
Game game = new Game();
setContentPane(game);
pack();
setVisible(true);
}
}
然后,您需要创建此游戏面板的实例并将其添加到JFrame:
GameFrame.java
public void display() {
pan.repaint();
fen.add(pan);
fen.addKeyListener(this);
fen.setResizable(false);
fen.setTitle("Le Jeu 2D");
img.setText("Coucou");
pan.add(img);
pan.repaint();
pan.setBackground(Color.yellow);
fen.setVisible(true);
fen.setSize(480, 272);
pan.repaint();
fen.revalidate();
}
public void paintComponent(Graphics g) {
System.out.println("zzz");
pan.paint(g);
g.setColor(Color.red);
g.drawRect(10, 10, 10, 10);
}
public class Game extends JPanel {
@Override
public final void paintComponent(final Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawRect(10, 10, 10, 10);
}
@Override
public Dimension getMinimumSize() {
return new Dimension(300, 300);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(300, 300);
}
}
public class GameFrame extends JFrame {
public GameFrame() {
setLocationRelativeTo(null);
setResizable(false);
setTitle("2D Game");
Game game = new Game();
setContentPane(game);
pack();
setVisible(true);
}
}
然后,当您创建JFrame的实例时:
Example.java
public void display() {
pan.repaint();
fen.add(pan);
fen.addKeyListener(this);
fen.setResizable(false);
fen.setTitle("Le Jeu 2D");
img.setText("Coucou");
pan.add(img);
pan.repaint();
pan.setBackground(Color.yellow);
fen.setVisible(true);
fen.setSize(480, 272);
pan.repaint();
fen.revalidate();
}
public void paintComponent(Graphics g) {
System.out.println("zzz");
pan.paint(g);
g.setColor(Color.red);
g.drawRect(10, 10, 10, 10);
}
public class Game extends JPanel {
@Override
public final void paintComponent(final Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawRect(10, 10, 10, 10);
}
@Override
public Dimension getMinimumSize() {
return new Dimension(300, 300);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(300, 300);
}
}
public class GameFrame extends JFrame {
public GameFrame() {
setLocationRelativeTo(null);
setResizable(false);
setTitle("2D Game");
Game game = new Game();
setContentPane(game);
pack();
setVisible(true);
}
}
将添加面板,并喷涂:
代码的基本结构似乎很奇怪。
您可以在主控件类中实例化JFrame,也就是说,在本例中它应该是游戏类。然后您可以创建一个新的JPanel类并将其对象添加到JPanel对象中。在JPanel类中,您可以创建所需的所有元素并设置相应的参数。您还可以在内部类或单独的类中添加事件侦听器。好的,但同样,它不起作用。为什么要在JPanel内部创建JFrame?您不知道。也许这不是他完整的源代码。。但是你是对的。。有点不对劲。。为什么要在JPanel中创建JFrame实例。应该是相反的。@johnada,编辑代码没有帮助。根据发布的代码,您的代码的整个结构都是错误的。学习挥杆没有捷径。阅读教程。下载工作代码。花点时间理解演示代码。然后修改代码。从结构良好的代码开始并进行更改更容易。最小、干净、可运行的示例说明了这一点,仅此而已!我喜欢。实际上,我也想这样做,但你更快:)你不应该设置帧的大小。每个回转组件应负责确定其自身的首选尺寸。因此,游戏类应该确定自己的大小。Swing教程演示了如何实现这一点。