1

我有一个 JPanel 窗口,上面画了一些东西。我想弄清楚如何使屏幕闪烁,例如一个简单的闪烁,以引起用户的注意?我发现了一个关于此的先前问题,但它不适用于我正在尝试做的事情。我有一个连续循环,可以根据一些变量更新屏幕。我只想让屏幕在某个点闪烁,然后恢复正常。

谢谢!

4

3 回答 3

4

您可以使用Trident在您的类中插入各种属性。这是一个为面板背景设置动画的演示:

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();
    }
}
于 2012-08-01T00:20:19.600 回答
2

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


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) {
            }
        }
    }
}
于 2012-07-31T18:00:34.543 回答
-1

我建议这样的伪代码:

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.
}

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

于 2012-07-31T23:13:59.417 回答