Java Applet 在 focusLost 和 focusGained 上闪烁

发布于 2024-08-23 21:04:09 字数 926 浏览 8 评论 0原文

我在网页中嵌入了一个 Java JApplet。 JApplet 窗口包含扩展 JPanel 的类的单个实例 - 与 JApplet 大小相同。 当用户单击面板中的按钮时,小程序可以生成弹出窗口 (JFrame)。

每次我单击按钮弹出窗口时,我的小程序都会在重新绘制时闪烁。当我再次单击它或当它获得焦点并单击不同的窗口时,它也会执行相同的操作 - 我的结论:这是在 focusLost() 和 focusGained() 事件上强制进行的重绘。

我在面板的 Paint() 方法中实现双缓冲,如下所示:

@Override
public void paint(Graphics g)
{
    if(resized)
    {
        offscreen = createImage(getWidth(),getHeight());
        resized = false;
    }

    Graphics offscreenG = offscreen.getGraphics();

    /// DRAW HERE:

    // paint the main window contents:
    view.paint(offscreenG);
    // paint the child components of our panel.
    super.paint(offscreenG);

    /// FRAW FINISHED

    g.drawImage(offscreen,0,0,this);
    offscreenG.dispose();
}

视图对象不是 swing 组件,而只是一个知道将所有内容绘制到 Graphics 对象上的类。

JApplet 的paint() 方法没有被重写。

我可能可以重写 JApplet 的 focusGained/focusLost 方法来防止重新绘制 - 但我宁愿听到更好的问题解决方案。

I have a Java JApplet embedded in a web page.
The JApplet window contains a single instance of a class that extends JPanel - same size as the JApplet.
The applet can spawn pop-up windows (JFrames) when the user clicks a button that's in the panel.

Every time I click on a button to pop-up a window, my applet flickers as it repaints. It also does the same when I click on it again or when it had focus and I click on a different window - my conclusion: it's a repaint that's being forced on focusLost() and focusGained() events.

I am implementing double buffering in the panel's paint() method like this:

@Override
public void paint(Graphics g)
{
    if(resized)
    {
        offscreen = createImage(getWidth(),getHeight());
        resized = false;
    }

    Graphics offscreenG = offscreen.getGraphics();

    /// DRAW HERE:

    // paint the main window contents:
    view.paint(offscreenG);
    // paint the child components of our panel.
    super.paint(offscreenG);

    /// FRAW FINISHED

    g.drawImage(offscreen,0,0,this);
    offscreenG.dispose();
}

The view object is not a swing component but just a class that knows hot to draw everything onto a Graphics object.

The JApplet's paint() method is not overridden.

I could probably override the focusGained/focusLost methods of my JApplet to prevent repainting - but I would rather hear a better solution to the problem.

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

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

发布评论

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

评论(1

俯瞰星空 2024-08-30 21:04:09

不需要实现双缓冲,这是由 Swing 自动完成的。构建小程序的方式与构建应用程序的方式相同。也就是说,您将组件添加到 JApplet 的内容窗格中。

如果您必须进行自定义绘制,那么您可以重写 JPanel 的 PaintComponent() 方法并将面板添加到小程序中。

阅读 Swing 教程中有关 自定义绘制 的部分以获取示例。

There is no need to implement double buffering, this is done automatically by Swing. You build an applet the same way you build an application. That is you add components to the content pane of the JApplet.

If you must do custom painting then you override the paintComponent() method of a JPanel and add the panel to the applet.

Read the section from the Swing tutorial on Custom Painting for examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文