当前位置: 代码迷 >> J2SE >> Swing弹出窗口后怎么使当前窗口(包括windows和java生成的)不丢失焦点
  详细解决方案

Swing弹出窗口后怎么使当前窗口(包括windows和java生成的)不丢失焦点

热度:158   发布时间:2016-04-24 02:14:55.0
Swing弹出窗口后如何使当前窗口(包括windows和java生成的)不丢失焦点
Swing弹出窗口后如何使当前窗口(包括windows和java生成的)不丢失焦点
Java code
import java.awt.Window;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import javax.swing.JDialog;import javax.swing.JFrame;public class Test extends JDialog implements Runnable{    public Test(){            }    @Override    public void run() {        // TODO Auto-generated method stub        this.setTitle("测试");        this.setVisible(true);    }            public static void main(String[] args){        int i=0;        while (i<5) {            Thread thread = new Thread(new Test());            thread.start();            i++;        }    }}

执行后当前主窗口和系统窗口都会失去焦点,而像QQ的右下角弹出消息就不会丢失焦点,如何改成像QQ这样的呢?

------解决方案--------------------
Java code
import java.awt.Color;import java.awt.Dimension;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JFrame;import javax.swing.JWindow;import javax.swing.Timer;public class JRocks {    public static void main(final java.lang.String[] args) throws Exception {        java.awt.EventQueue.invokeLater(new java.lang.Runnable(){                @Override public void run(){                    final JFrame frame = new JFrame("JRocks");                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                    frame.setPreferredSize(new Dimension(600,400));                    frame.pack();                    frame.setVisible(true);                    final JWindow window = new JWindow();                    window.setPreferredSize(new Dimension(300,200));                    window.setBackground(new Color(128,128,128,1));                    window.pack();                    window.setLocation(800,50);                    final Timer timer = new Timer(5000,new ActionListener(){                            @Override public void actionPerformed(ActionEvent e){                                window.setVisible(!window.isShowing());                            }                        });                    frame.addWindowListener(new WindowAdapter(){                            @Override public void windowOpened(WindowEvent e){                                timer.start();                            }                            @Override public void windowClosing(WindowEvent e){                                timer.stop();                                window.dispose();                            }                        });                }            });    }}
  相关解决方案