Java 图形无法显示

发布于 2024-12-21 06:17:09 字数 635 浏览 1 评论 0原文

这是我的代码:

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

public class Survival extends JFrame { 
    private static int applicationWidth = 1400;
    private static int applicationHeight = 900;  

    public Survival() {
        setTitle("Survival");
        setResizable(false);
        setSize(applicationWidth, applicationHeight);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        g.drawString("Test", 0, 0);
    }

    public static void main(String[] args) {
        new Survival();
    }
}

为什么没有显示“测试”?

Here is my code:

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

public class Survival extends JFrame { 
    private static int applicationWidth = 1400;
    private static int applicationHeight = 900;  

    public Survival() {
        setTitle("Survival");
        setResizable(false);
        setSize(applicationWidth, applicationHeight);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        g.drawString("Test", 0, 0);
    }

    public static void main(String[] args) {
        new Survival();
    }
}

Why isn't "Test" showing up?

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

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

发布评论

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

评论(2

陌上芳菲 2024-12-28 06:17:09

不要覆盖 paint。每当您自定义组件时,请覆盖 paintComponent

示例 -

@Override
protected final void paintComponent(final Graphics g){
    super.paintComponent(g);
    final Graphics gCopy = g.create(); // Prevents clobbering
    gCopy.drawString("Test", 0, 0);
    gCopy.dispose();
}

Do not override paint. Whenever you customize a component, override paintComponent.

Example -

@Override
protected final void paintComponent(final Graphics g){
    super.paintComponent(g);
    final Graphics gCopy = g.create(); // Prevents clobbering
    gCopy.drawString("Test", 0, 0);
    gCopy.dispose();
}
过期情话 2024-12-28 06:17:09

您需要调用超类的paint()方法。 (文章 - 在 AWT 和 Swing 中绘画

 public Survival() {
        setTitle("Survival");
        setResizable(false);
        setSize(applicationWidth, applicationHeight);
        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        repaint();
    }

    public void paint(Graphics g) {
        super.paint(g);    
        g.drawString("Test", 120, 120); //change the co-odrinates
    }

覆盖 JPanel 的 PaintComponent 。

 public Survival() {
        setTitle("Survival");
        setResizable(false);
        setSize(applicationWidth, applicationHeight);
        setVisible(true);
        add(new DrawPanel());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
     }

   class DrawPanel extends JPanel
   {
    @Override
    protected  void paintComponent( Graphics g){
       g.drawString("Test", 220,220);
      }
   }

You need to invoke paint() method of super class. (Article - Painting in AWT and Swing)

 public Survival() {
        setTitle("Survival");
        setResizable(false);
        setSize(applicationWidth, applicationHeight);
        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        repaint();
    }

    public void paint(Graphics g) {
        super.paint(g);    
        g.drawString("Test", 120, 120); //change the co-odrinates
    }

Override the paintComponent of JPanel.

 public Survival() {
        setTitle("Survival");
        setResizable(false);
        setSize(applicationWidth, applicationHeight);
        setVisible(true);
        add(new DrawPanel());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
     }

   class DrawPanel extends JPanel
   {
    @Override
    protected  void paintComponent( Graphics g){
       g.drawString("Test", 220,220);
      }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文