简体   繁体   English

如何重绘java.awt.Window?

[英]How to repaint a java.awt.Window?

why is my java.awt.Window not repainting after I called the repaint() method? 为什么在调用repaint()方法后我的java.awt.Window无法重绘?

public class Counter extends Window implements ActionListener {

private static final long serialVersionUID = 1L;
private Timer timer;
private int time;

public Counter() {
    super(null);
    setAlwaysOnTop(true);
    setBounds(getGraphicsConfiguration().getBounds());
    setBackground(new Color(0, true));
    setVisible(true);
    timer = new Timer(1000, this);
    timer.start();
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.clearRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.RED);
    g.drawString(String.valueOf(time), getWidth()/2, getHeight()/2);
}

@Override
public void update(Graphics g) {
    super.update(g);
}

@Override
public void actionPerformed(ActionEvent e) {
    time++;
    repaint();
}

As you can see i created a timer with a delay of 1 second. 如您所见,我创建了一个延迟为1秒的计时器。 After that i call repaint() to draw the counter's number on the screen. 之后,我调用repaint()在屏幕上绘制计数器的编号。 But it only draws a zero on my screen and stops drawing then (the zero stays on screen). 但它只会在我的屏幕上绘制一个零,然后停止绘制(该零会保留在屏幕上)。 First i thought that the paint method is only called once, but i tested a System.out.prinln() and prooved that the paint method is executed every second so it should actually repaint the window... So i don't know where i made a mistake. 首先,我认为paint方法仅被调用一次,但是我测试了System.out.prinln()并证明了paint方法每秒执行一次,因此它实际上应该重新绘制窗口...所以我不知道在哪里我犯了一个错误。

And yes it is my intention to use the awt.Window and not a JFrame or Frame etc.. 是的,我打算使用awt.Window而不是JFrame或Frame等。

I finally got to run the code on Windows 7, and I could replicate the issue. 我终于可以在Windows 7上运行代码,并且可以复制该问题。 For some reason, paint isn't being called; 由于某种原因,没有调用paint why, I don't know. 为什么,我不知道。 Because I wouldn't do it this way, I've never had that issue. 因为我不会这样做,所以我从来没有遇到过这个问题。

Instead, I'd start by having the counter class extend from something JPanel or JComponent (just remember to make JPanel transparent) and then add it to a JWindow , something like this: 相反,我将从计数器类从JPanelJComponent扩展(只记得使JPanel透明)开始,然后将其添加到JWindow ,如下所示:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.Timer;


public class Counter extends JPanel implements ActionListener {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JWindow window = new JWindow();
                window.add(new Counter());
                window.pack();
                window.setLocationRelativeTo(null);
                window.setBackground(new Color(0, 0, 0, 0));
                window.setVisible(true);
            }
        });
    }

    private static final long serialVersionUID = 1L;
    private Timer timer;
    private int time;

    public Counter() {
        setOpaque(false);
        timer = new Timer(1000, this);
        timer.start();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
        System.out.println(time);
        g.clearRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.RED);
        g.drawString(String.valueOf(time), getWidth() / 2, getHeight() / 2);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        time++;
        System.out.println("..." + time);
        repaint();
    }
}

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

相关问题 java.awt.Window的全屏问题 - Full screen issue with java.awt.Window Java“ java.awt.Window”数组在位置0添加项目 - Java “java.awt.Window” Array add Item at position 0 在Ubuntu上将全屏设置为java.awt.Window - Setting the full screen to a java.awt.Window on Ubuntu 如何在java.awt.Window屏幕上侦听变化? 使用setFullScreenWindow()时是否有办法将焦点放在其他Windows /程序上? - How to listen for a change in java.awt.Window screen? Is there a way to give focus to other windows/programs whilst using setFullScreenWindow()? 在Swing应用程序启动期间,首次调用JFrame构造函数需要很长时间(因为java.awt.Window()) - First call to JFrame constructor takes a long time during Swing application startup (because of java.awt.Window()) 你为什么要处理一个超出范围的java.awt.Window? - Why should you have to dispose() a java.awt.Window that goes out of scope? Java Swing或AWT重绘频率 - Java Swing or AWT repaint frequency Java awt Panel 不会从线程重新绘制,只会在我调整窗口大小时更新 - Java awt Panel doesn't repaint from thread and only updates when I resize the window 如何重新粉刷我的 window? (爪哇) - How do I repaint my window? (Java) 如何重绘JAVA FX 2.2中的窗口(舞台) - How to repaint a window (stage) in JAVA FX 2.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM