在 Java Swing 库中使用带有面板的图形的问题

发布于 2024-07-08 12:30:04 字数 948 浏览 10 评论 0原文

大家好,我正在尝试运行以下程序,但收到 NullPointerException。 我是 Java swing 库的新手,所以我可能会做一些非常愚蠢的事情。 不管怎样,这是我的两个课程,我现在只是在玩,我想做的就是画一个该死的圆圈(我想画一个绞刑架,最后上面有一个刽子手)。

package hangman2;

import java.awt.*;
import javax.swing.*;

public class Hangman2 extends JFrame{
    private GridLayout alphabetLayout = new GridLayout(2,2,5,5);
    private Gallow gallow = new Gallow();

    public Hangman2() {

        setLayout(alphabetLayout);

        setSize(1000,500);
        setVisible( true );

    }

    public static void main( String args[] ) {   
     Hangman2 application = new Hangman2();
     application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
}


package hangman2;

import java.awt.*;
import javax.swing.*;

public class Gallow extends JPanel {
    private Graphics g;

    public Gallow(){
        g.fillOval(10, 20, 40, 25);       
    }
}

NullPointerException 出现在 g.fillOval 行。

预先感谢,

托梅克

Hey everyone, I am trying to run the following program, but am getting a NullPointerException. I am new to the Java swing library so I could be doing something very dumb. Either way here are my two classes I am just playing around for now and all i want to do is draw a damn circle (ill want to draw a gallow, with a hangman on it in the end).

package hangman2;

import java.awt.*;
import javax.swing.*;

public class Hangman2 extends JFrame{
    private GridLayout alphabetLayout = new GridLayout(2,2,5,5);
    private Gallow gallow = new Gallow();

    public Hangman2() {

        setLayout(alphabetLayout);

        setSize(1000,500);
        setVisible( true );

    }

    public static void main( String args[] ) {   
     Hangman2 application = new Hangman2();
     application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
}


package hangman2;

import java.awt.*;
import javax.swing.*;

public class Gallow extends JPanel {
    private Graphics g;

    public Gallow(){
        g.fillOval(10, 20, 40, 25);       
    }
}

The NullPointerException comes in at the g.fillOval line.

Thanks in advance,

Tomek

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痕至 2024-07-15 12:30:04

您收到 NPE 是因为未设置 g,因此它是 null。 此外,您不应该在构造函数中进行绘图。 重载 改为 paintComponent(Graphics g)

public class Gallow extends JPanel {
    public paintComponent(Graphics g){
        g.fillOval(10, 20, 40, 25);       
    }
}

我还会研究 BufferedImage< /a>.

You're getting NPE because g is not set, therefore, it's null. Furthermore, you shouldn't be doing the drawing in the constructor. Overload paintComponent(Graphics g) instead.

public class Gallow extends JPanel {
    public paintComponent(Graphics g){
        g.fillOval(10, 20, 40, 25);       
    }
}

I'd also look into BufferedImage.

苯莒 2024-07-15 12:30:04

有几点:不要忘记将面板添加到 JFrame 中。 并重写 JPanelpaint() 方法来进行自定义绘画。 您不需要声明 Graphics 对象,因为在任何情况下 JPanel 的 Paint 方法都会引用该对象。

package hangman2;

import java.awt.*;
import javax.swing.*;

public class Hangman2 extends JFrame{
    private GridLayout alphabetLayout = new GridLayout(2,2,5,5);
    private Gallow gallow = new Gallow();

    public Hangman2() {

        setLayout(alphabetLayout);
        add(gallow, BorderLayout.CENTER);//here
        setSize(1000,500);
        setVisible( true );

    }

    public static void main( String args[] ) {   
        Hangman2 application = new Hangman2();
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
}


package hangman2;

import java.awt.*;
import javax.swing.*;

public class Gallow extends JPanel {

    public Gallow(){
        super();
    }

    public void paint(Graphics g){
        g.fillOval(10, 20, 40, 25);       
    }
}

A couple of things: Don't forget to add the panel to the JFrame. And override the paint() method of JPanel for your custom painting. You do not need to declare a Graphics object since the JPanel's paint method will have a reference to one in any case.

package hangman2;

import java.awt.*;
import javax.swing.*;

public class Hangman2 extends JFrame{
    private GridLayout alphabetLayout = new GridLayout(2,2,5,5);
    private Gallow gallow = new Gallow();

    public Hangman2() {

        setLayout(alphabetLayout);
        add(gallow, BorderLayout.CENTER);//here
        setSize(1000,500);
        setVisible( true );

    }

    public static void main( String args[] ) {   
        Hangman2 application = new Hangman2();
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
}


package hangman2;

import java.awt.*;
import javax.swing.*;

public class Gallow extends JPanel {

    public Gallow(){
        super();
    }

    public void paint(Graphics g){
        g.fillOval(10, 20, 40, 25);       
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文