在 JFrame 中设置 JPanel?
因此,我一直在努力重做我的代码,以便绘画全部在 JPanel 而不是 JFrame 中完成,这样我就可以做一些非常需要的图像缓冲。
我已经搜索过 StackOverflow,并且用我的手指进行了谷歌搜索,我以为我已经正确设置了它,但它不起作用。我只是在终端中看到一个空白屏幕和一些错误脚本。任何帮助表示赞赏。这是代码
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
public class RacerDoom extends JFrame {
private JPanel panel;
final int WIDTH = 900, HEIGHT = 640;
int counter = 0;
Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
Rectangle right = new Rectangle((WIDTH/9)*8,0,WIDTH/9,HEIGHT);
Rectangle top = new Rectangle(0,0,WIDTH,HEIGHT/9);
Rectangle bottom = new Rectangle(0,(HEIGHT/9)*8,WIDTH,HEIGHT);
Rectangle center = new Rectangle((int)((WIDTH/9)*2.5),(int)((HEIGHT/9)*2.5),(int)((WIDTH/9)*4),(HEIGHT/9)*4);
Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2,WIDTH/30,WIDTH/30);
Rectangle finishtop = new Rectangle(WIDTH/9,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5),HEIGHT/70);
//Starting lines
Rectangle startO = new Rectangle(WIDTH/9,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
public RacerDoom() {
//create JFrame
super("Racer Doom Squared");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
getContentPane().add(panel);
panel = new MainPanel();
panel.setBounds(0,0,WIDTH,HEIGHT);
this.getContentPane().add(panel);
//set up Game countdown timer
final Timer timer=new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(counter>=10) {
((Timer)e.getSource()).stop();
}
else{
counter++;
}
System.out.println(counter);
}
});
//start timer
timer.start();
}
private class MainPanel extends JPanel {
public MainPanel() {
super();
}
//draw graphics
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0,0,WIDTH,HEIGHT);
//boundaries
g.setColor(Color.LIGHT_GRAY);
g.fillRect(left.x,left.y,left.width,left.height);
g.fillRect(right.x,right.y,right.width,right.height);
g.fillRect(top.x,top.y,top.width,top.height);
g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
g.fillRect(center.x,center.y,center.width,center.height);
//start line
g.setColor(Color.WHITE);
g.fillRect(startO.x,startO.y,startO.width,startO.height);
//finish line
g.setColor(Color.CYAN);
g.fillRect(finishtop.x,finishtop.y,finishtop.width,finishtop.height);
//p1
g.setColor(Color.BLUE);
g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);
//HUD
g.setColor(Color.WHITE);
Font f = new Font("Monospaced", Font.BOLD, 24);
g.setFont(f);
g.drawString("Boosts: "+p1Boost,(WIDTH-(WIDTH/6)),(HEIGHT-(int)(HEIGHT/1.1)));
g.drawString("Time: "+(10-counter),540,100);
}
}
public static void main (String [] args) {
new RacerDoom();
}
}
错误脚本:
线程“main”中的异常 java.lang.NullPointerException
在 java.awt.Container.addImpl(来源未知)
在 java.awt.Container.add(来源未知)
在 RacerDoom。(RacerDoom.java:46)
在 RacerDoom.main(RacerDoom.java:232)
我很确定我只是个白痴,答案可能是在错误消息中向我竖起中指,但这仍然是希腊语 我。但即使(尤其是?)白痴也需要帮助。
So, I've been working to redo my code so that the painting is all done in a JPanel instead of a JFrame so I can do some very much needed image buffering.
I've scoured StackOverflow and I've googled my fingers raw and I thought I had it set up right, but it's not working. I just get a blank white screen and some error script in the terminal. Any help is appreciated. Here is the code
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
public class RacerDoom extends JFrame {
private JPanel panel;
final int WIDTH = 900, HEIGHT = 640;
int counter = 0;
Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
Rectangle right = new Rectangle((WIDTH/9)*8,0,WIDTH/9,HEIGHT);
Rectangle top = new Rectangle(0,0,WIDTH,HEIGHT/9);
Rectangle bottom = new Rectangle(0,(HEIGHT/9)*8,WIDTH,HEIGHT);
Rectangle center = new Rectangle((int)((WIDTH/9)*2.5),(int)((HEIGHT/9)*2.5),(int)((WIDTH/9)*4),(HEIGHT/9)*4);
Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2,WIDTH/30,WIDTH/30);
Rectangle finishtop = new Rectangle(WIDTH/9,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5),HEIGHT/70);
//Starting lines
Rectangle startO = new Rectangle(WIDTH/9,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
public RacerDoom() {
//create JFrame
super("Racer Doom Squared");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
getContentPane().add(panel);
panel = new MainPanel();
panel.setBounds(0,0,WIDTH,HEIGHT);
this.getContentPane().add(panel);
//set up Game countdown timer
final Timer timer=new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(counter>=10) {
((Timer)e.getSource()).stop();
}
else{
counter++;
}
System.out.println(counter);
}
});
//start timer
timer.start();
}
private class MainPanel extends JPanel {
public MainPanel() {
super();
}
//draw graphics
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0,0,WIDTH,HEIGHT);
//boundaries
g.setColor(Color.LIGHT_GRAY);
g.fillRect(left.x,left.y,left.width,left.height);
g.fillRect(right.x,right.y,right.width,right.height);
g.fillRect(top.x,top.y,top.width,top.height);
g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
g.fillRect(center.x,center.y,center.width,center.height);
//start line
g.setColor(Color.WHITE);
g.fillRect(startO.x,startO.y,startO.width,startO.height);
//finish line
g.setColor(Color.CYAN);
g.fillRect(finishtop.x,finishtop.y,finishtop.width,finishtop.height);
//p1
g.setColor(Color.BLUE);
g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);
//HUD
g.setColor(Color.WHITE);
Font f = new Font("Monospaced", Font.BOLD, 24);
g.setFont(f);
g.drawString("Boosts: "+p1Boost,(WIDTH-(WIDTH/6)),(HEIGHT-(int)(HEIGHT/1.1)));
g.drawString("Time: "+(10-counter),540,100);
}
}
public static void main (String [] args) {
new RacerDoom();
}
}
The error script:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at RacerDoom.(RacerDoom.java:46)
at RacerDoom.main(RacerDoom.java:232)
I'm quite sure I'm just an idiot and the answer is probably giving me the middle finger in the error message, but it's still Greek to me. But even (especially?) idiots need help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的错误非常简单:
您在实际创建面板之前将面板添加到寡妇中。尝试像这样重新排列它:
Your error is pretty simple:
You're adding the panel to the widow before the panel is actually created. Try rearranging it like this:
我相信第 46 行是:
此时,您还没有创建面板,因此它为空。您可以' t 将空组件添加到容器。删除该行;您稍后的
this.getContentPane().add(panel);
就是您所需要的。I believe line 46 is:
At that point, you haven't created the panel yet, so it is null. You can't add a null component to a container. Remove that line; your later
this.getContentPane().add(panel);
is all you need.发生 NullPointerException 是因为您尝试添加未初始化的变量面板:
您必须首先初始化此变量,然后将其添加到 getContentPane()
The NullPointerException occurs because you try to add not initialized variable panel:
You have to first initialize this variable, and than add it to getContentPane()