java – Flashing JFrame

我想创建一个具有两个特定功能的JFrame:

> JFrame不应该从最小化状态最大化时抓住焦点.
>当JFrame从最小化状态创建或最大化时,它应该在Windows栏中闪烁,直到用户将焦点赋予它. (就像在ICQ客户中一样).

有人知道如何实施第二项要求吗?

很少自我解释的例子:

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

public class JFrameTest {
    private static JFrame childFrame;
    public static Container getParentContentPane() {
        JPanel panel = new JPanel();
        JButton button = new JButton("Create\\Restore child frame");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                createOrRestoreChildFrame();
            }
        });
        panel.add(button);
        return panel;
    }

    private static void createOrRestoreChildFrame() {
        if (childFrame == null) {
            childFrame = new JFrame("Child Frame");
            childFrame.setLocation(200, 200);
            childFrame.add(new JLabel("Child Frame"));
            childFrame.pack();
            setChildFrameVisible();
        } else {
            setChildFrameVisible();
        }
    }

    private static void setChildFrameVisible() {
        childFrame.setFocusableWindowState(false);
        childFrame.setVisible(true);
        flashInWindowsBar(childFrame);
        childFrame.toFront();
        childFrame.setFocusableWindowState(true);
    }

    /**
     * Should Make child frame flash in Windows bar.
     * Currently, it does not work for me.
     * Could anybody help me to fix this please? ) 
     */
    private static void flashInWindowsBar(JFrame childFrame) {
        childFrame.setState(JFrame.ICONIFIED);
        childFrame.toFront();
    }

    private static void createAndShowGUI() {
        JFrame parentFrame = new JFrame("JFrame Demo");
        parentFrame.setLocation(100, 100);
        parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        parentFrame.setContentPane(getParentContentPane());
        parentFrame.pack();
        parentFrame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

谢谢!

最佳答案 以下代码完全按照您的描述为我工作:

    f.setState(JFrame.ICONIFIED);
    f.toFront();

f是一个JFrame.

点赞