简体   繁体   English

在Java中重用swing组件

[英]Reusing swing components in Java

If some component was changed after component was set to invisible, it is only repainted AFTER the component is set to visible. 如果在将组件设置为不可见后更改了某个组件,则仅在组件设置为可见后重新绘制。 That makes flickers (old graphics is visible for a few milliseconds): 这使得闪烁(旧图形可见几毫秒):

package test;

import javax.swing.*;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;

class ReusingWindow extends JWindow {

    JLabel label;

    public ReusingWindow() {

        JPanel panel = new JPanel(new BorderLayout());
        panel.setPreferredSize(new Dimension(300, 200));
        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        label = new JLabel("Lazy cat");
        label.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
        label.setBackground(Color.red);
        label.setOpaque(true);
        panel.add(label, BorderLayout.WEST);
        add(panel);

        pack();
        setLocationRelativeTo(null);
    }

    public static void main(String args[]) {
        ReusingWindow window = new ReusingWindow();

        StringBuilder sb = new StringBuilder();
        sb.append("<html>");
        for (int a = 0; a < 10; a++){
            sb.append("Not very lazy cat. Extremelly fast cat.<br>");
        }
         sb.append("</html>");

        while (true) {

            window.label.setText("Lazy cat");
            window.setVisible(true);
            pause();
            window.setVisible(false);
            pause();

            window.label.setText(sb.toString());
            window.setVisible(true);
            pause();
            window.setVisible(false);
            pause();
        }
    }

    private static void pause() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(ReusingWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Is there any solution besides creating the new window each time before setting it visible? 除了在每次设置可见之前创建新窗口之外还有其他解决方案吗?

The problem may be somewhere with your "reusing window". 问题可能出在你的“重用窗口”的某个地方。 With the simple class like 跟简单的类一样

static class ReusingWindow extends JFrame {
    JLabel label = new JLabel();
    public ReusingWindow() {
        add(label);
        setBounds(0, 0, 100, 100);
    }
}

I do not observe any flickering. 我没有观察到任何闪烁。

why can't you call 为什么你不能打电话

    window.repaint(); 

manually before calling 在打电话之前手动

   setVisible(true); 

? i cannot reproduce flickering on my machine.. if this didn't help you can try call 我无法在我的机器上重现闪烁..如果这没有帮助你可以尝试通话

   window.revalidate(); 

aswell before repaint 在重新粉刷之前

Solved :) 解决了 :)

Just need to update(getGraphics()); 只需要update(getGraphics()); (just an example): (只是一个例子):

while (true) {

        window.label.setText("Lazy cat");
        window.update(window.getGraphics());//<------ new line
        window.setVisible(true);
        pause();
        window.setVisible(false);
        pause();

        window.label.setText(sb.toString());
        window.update(window.getGraphics());//<------ new line
        window.setVisible(true);
        pause();
        window.setVisible(false);
        pause();
    }

Here is very good info about the difference between repaint() and update(Graphics g) . 这里是关于repaint()update(Graphics g)之间区别的非常好的信息。

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

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