繁体   English   中英

Java-如何Flash JPanel窗口

[英]Java- How to Flash JPanel Window

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

谢谢!

您可以使用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();
    }
}

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


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

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

暂无
暂无

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

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