Java-如何刷新JPanel窗口

Java-如何刷新JPanel窗口,java,swing,canvas,jpanel,Java,Swing,Canvas,Jpanel,我有一个JPanel窗口,上面画了一些东西。我想弄清楚我如何能像一个简单的闪光灯一样,在屏幕上闪烁,以引起用户的注意?我发现了之前的一个问题,但它不适用于我试图做的事情。我有一个连续循环,根据几个变量更新屏幕。我只希望屏幕在某一点闪烁,然后恢复正常 谢谢 使用玻璃材质窗格玻璃组件,如下面的示例演示所示: import java.awt.Color; import java.awt.Graphics; import javax.swing.JComponent; import javax.swi

我有一个JPanel窗口,上面画了一些东西。我想弄清楚我如何能像一个简单的闪光灯一样,在屏幕上闪烁,以引起用户的注意?我发现了之前的一个问题,但它不适用于我试图做的事情。我有一个连续循环,根据几个变量更新屏幕。我只希望屏幕在某一点闪烁,然后恢复正常


谢谢

使用玻璃材质窗格玻璃组件,如下面的示例演示所示:


import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Demo {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        final JPanel panel = new JPanel();
        frame.add(panel);

        JComponent flashPane = new JComponent() {
            @Override
            public void paint(Graphics g) {
                // Add code to draw whatever you want to grab attention here
                g.setColor(Color.CYAN);
                g.fillRect(0, 0, panel.getWidth(), panel.getHeight());
                super.paint(g);
            }
        };

        panel.getRootPane().setGlassPane(flashPane);
        frame.setBounds(0, 0, 200, 200);
        frame.setVisible(true);

        // Sample loop to flash every 2 seconds
        while(true) {
            try {
                Thread.sleep(2000);
                flashPane.setVisible(true);
                Thread.sleep(200);
                flashPane.setVisible(false);
            } catch(Exception ex) {
            }
        }
    }
}

如下面的示例演示中所示,使用窗格玻璃组件:


import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Demo {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        final JPanel panel = new JPanel();
        frame.add(panel);

        JComponent flashPane = new JComponent() {
            @Override
            public void paint(Graphics g) {
                // Add code to draw whatever you want to grab attention here
                g.setColor(Color.CYAN);
                g.fillRect(0, 0, panel.getWidth(), panel.getHeight());
                super.paint(g);
            }
        };

        panel.getRootPane().setGlassPane(flashPane);
        frame.setBounds(0, 0, 200, 200);
        frame.setVisible(true);

        // Sample loop to flash every 2 seconds
        while(true) {
            try {
                Thread.sleep(2000);
                flashPane.setVisible(true);
                Thread.sleep(200);
                flashPane.setVisible(false);
            } catch(Exception ex) {
            }
        }
    }
}

我建议使用如下伪代码:

if(your flash condition is true){
yourFrame.visible = False; // no longer display the frame.
Thread.sleep(500); // waits for half a second.
yourFrame.visible = True; // show the frame again.
}

您可能希望在之后验证视图(框架),以确保在线程唤醒并再次显示框架后,模型(变量等)中可能发生的任何更改都会得到反映。

我建议使用如下伪代码:

if(your flash condition is true){
yourFrame.visible = False; // no longer display the frame.
Thread.sleep(500); // waits for half a second.
yourFrame.visible = True; // show the frame again.
}
您可能希望在之后验证视图(框架),以确保在线程唤醒并再次显示框架后,模型(变量等)中可能发生的任何更改都会得到反映。

您可以使用它在类中插入各种属性。下面是一个演示,演示了面板背景的动画:

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.pushingpixels.trident.Timeline;
import org.pushingpixels.trident.Timeline.RepeatBehavior;

public class TestPanel {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.add(new JLabel("Some text"));

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

        final Timeline timeline = new Timeline(panel);
        timeline.addPropertyToInterpolate("background", panel.getBackground(),
                Color.red);
        timeline.setDuration(1000);

        timeline.playLoop(5, RepeatBehavior.REVERSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
编辑:自动隐藏弹出窗口的注释

您可以使用计时器隐藏弹出窗口,例如:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TempPopup {

    public static void main(String[] args) {

        JOptionPane pane = new JOptionPane("Message",
                JOptionPane.INFORMATION_MESSAGE);
        final JDialog dialog = pane.createDialog(null, "Title");

        Timer timer = new Timer(2000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
                dialog.dispose();
            }
        });
        timer.setRepeats(false);
        timer.start();
        dialog.setVisible(true);
        dialog.dispose();
    }
}
可以使用插值类中的各种属性。下面是一个演示,演示了面板背景的动画:

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.pushingpixels.trident.Timeline;
import org.pushingpixels.trident.Timeline.RepeatBehavior;

public class TestPanel {
    private static void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.add(new JLabel("Some text"));

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

        final Timeline timeline = new Timeline(panel);
        timeline.addPropertyToInterpolate("background", panel.getBackground(),
                Color.red);
        timeline.setDuration(1000);

        timeline.playLoop(5, RepeatBehavior.REVERSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
编辑:自动隐藏弹出窗口的注释

您可以使用计时器隐藏弹出窗口,例如:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TempPopup {

    public static void main(String[] args) {

        JOptionPane pane = new JOptionPane("Message",
                JOptionPane.INFORMATION_MESSAGE);
        final JDialog dialog = pane.createDialog(null, "Title");

        Timer timer = new Timer(2000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
                dialog.dispose();
            }
        });
        timer.setRepeats(false);
        timer.start();
        dialog.setVisible(true);
        dialog.dispose();
    }
}

我自己会使用()并调用
setBackground(…)
在计时器的ActionListener中传递一个与null交替的颜色。到目前为止,你尝试了什么,效果如何?请展示您的代码尝试。将战壕外套留在家中,然后弹出一个
JOptionPane
。您也可以改变或。我已经决定,闪烁窗口不是我想要完成的最佳想法…我宁愿有一个小窗口弹出约一秒钟,上面有一些简单的文本…我如何完成此任务?@kaptaincooke您可以使用timer隐藏弹出窗口,例如查看我的上一次编辑。我会使用()并调用
setBackground(…)
在timer的ActionListener中传递一个与null交替的颜色。到目前为止,你尝试了什么,效果如何?请展示您的代码尝试。将战壕外套留在家中,然后弹出一个
JOptionPane
。您也可以改变或。我已经决定,闪烁窗口不是我想要完成的最佳想法…我宁愿有一个小窗口弹出约一秒钟,上面有一些简单的文本…我如何完成此任务?@kaptaincooke您可以使用计时器隐藏弹出窗口,例如查看我的上一次编辑。请不要在事件调度线程上使用
sleep()
,也不要在其他线程上使用。那么,您建议垃圾神做什么?我的评论中引用的任何一个示例。请不要使用
sleep()
在事件调度线程上,也不要在其他线程上绘制。那么,你建议垃圾神做什么?我的评论中引用的任何一个例子。请不要在事件调度线程上绘制
sleep()
,也不要在其他线程上绘制。此外,“Swing程序应该重写
paintComponent()
,而不是重写
paint()。这里的推动力是使用玻璃窗格,而不是制作防弹演示。还要避免
null
布局和吞没异常。请不要在事件调度线程上使用
sleep()
,也不要在其他线程上使用。此外,“Swing程序应该重写
paintComponent()
,而不是重写
paint()。这里的动力是使用玻璃窗格,而不是制作防弹演示。还要避免
null
布局和异常。