Java-如何刷新JPanel窗口 [英] Java- How to Flash JPanel Window

查看:759
本文介绍了Java-如何刷新JPanel窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JPanel窗口,上面画了一些东西。我想弄清楚如何像一个简单的闪光灯那样使屏幕闪烁,以引起用户的注意?我对此找到了一个先前的问题,但对我想做的事情没有用。我有一个连续的循环,它根据一些变量来更新屏幕。我只想让屏幕在某个时刻闪烁,然后恢复正常。

I have a JPanel window with a few things drawn on it. I am trying to figure out how I can flash the screen, like one simple flash, to get a users attention? I found a previous question on this but it did not work for what I am trying to do. I have a continuous loop that updates the screen based on a few variables. I would just like the screen to flash at a certain point and then go back to normal.

谢谢!

推荐答案

您可以使用 Trident 可以插补课程中的各种属性。这是使面板背景动画化的演示:

You can use Trident to interpolate various properties in your class. Here is a demo that animates panel's background:

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

编辑:对汽车的评论隐藏弹出窗口

comments to auto hide a popup

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

You can use timer to hide a popup, for example:

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

这篇关于Java-如何刷新JPanel窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆