Java的如何闪光的JPanel窗口(Java- How to Flash JPanel Windo

2019-10-17 02:55发布

我有它绘制的几件事情一个JPanel窗口。 我试图找出我怎么能闪屏,就像一个简单的闪存,以获得用户的关注? 我发现这个以前的问题,但它并没有什么,我试图做的工作。 我有一个连续的循环,更新基于几个变量的画面。 我只想屏幕在某一点上闪烁,然后再恢复正常。

谢谢!

Answer 1:

您可以使用三叉戟在类内插的各种属性。 下面是动画面板的背景演示:

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();
    }
}


Answer 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) {
            }
        }
    }
}


Answer 3:

我建议这样的伪代码:

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

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



文章来源: Java- How to Flash JPanel Window