简体   繁体   English

Java AWT Window 实现未运行paint()函数

[英]Java AWT Window implementation not running paint() function

(I am fully aware this is an old way to go about this - I have to do it this way) (我完全知道这是一种古老的方法 - 我必须这样做)

I am trying to use the Window class of AWT to draw basic visuals to the screen - the window shows up, but, despite the fact that the paint() function is being run(Tested with sysout ) - nothing shows up.我正在尝试使用AWTWindow类将基本视觉效果绘制到屏幕上 - 窗口显示出来,但是,尽管正在运行paint()函数(使用sysout测试) - 没有任何显示。

The problematic code:有问题的代码:

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;

public class a {

    static a a = new a();
    public static void main(String[] args) {
        b b = a.new b();
        b.setVisible(true);
        b.pack();
    }
    class b extends Frame {
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 16, 16);
            repaint();
        }
    }
}

If anyone knows what is causing this or how to rectify it I would greatly appreciate your assistance.如果有人知道是什么导致了这种情况或如何纠正它,我将非常感谢您的帮助。

Thanks in advance!提前致谢!

Turns out I needed an extra Canvas object to draw onto - here's the functioning code for anyone having the problem I had原来我需要一个额外的 Canvas 对象来绘制 - 这是任何遇到问题的人的功能代码

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;

public class a {

    static a a = new a();
    public static void main(String[] args) {
        Frame f = new Frame();
        b b = a.new b();
        f.add(b);
        f.setVisible(true);
        f.pack();
    }
    class b extends Canvas {
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 16, 16);
            repaint();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM