如何使JFrame激活(聚焦)?

问题描述 投票:1回答:2

当显示对话框窗口时,如果其他窗口处于活动状态,则它不会聚焦(即,您需要通过鼠标指针对其进行聚焦以便能够处理它)。如何关注显示的对话框?

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class GuiTest {

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

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("frame");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setAlwaysOnTop(true);

        JOptionPane.showMessageDialog(frame, "test info", "test header",
                JOptionPane.INFORMATION_MESSAGE);

        //frame.toFront();
        //frame.requestFocus();

        frame.dispose(); // When a frame is disposed, the exit action will be
                            // called.

    }

}
java swing
2个回答
0
投票

JOptionPane.showMessageDialog停止EDT(事件调度线程),直到对话框关闭。

您可以改为使用另一个JFrame而不是JOptionPane

package util;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GuiTest {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GuiTest window = new GuiTest();
                    window.frame.setVisible(true);

                    Dialog d = new Dialog();
                    d.show();

                    window.frame.requestFocus();

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * Create the application.
     */
    public GuiTest() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

class Dialog extends JFrame {
    private JFrame frame;

    public void show() {

        if (frame == null) {
            frame = new JFrame();
            frame.setTitle("Dialog");

            frame.setBounds(100, 100, 450, 300);
            frame.add(new JTextField("Hello"));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        }

        frame.setVisible(true);
    }

}

0
投票

尝试运行它,然后在任何其他窗口激活

一旦窗口失去焦点,您需要:

  1. 单击窗口以获得焦点
  2. 使用桌面管理器访问该应用程序。在Windows中,这是通过使用Alt + Tab循环打开应用程序来完成的。

问题是只有一个JFrame被添加到桌面管理器中,因此如果您希望能够使用Alt + Tab,则需要在显示选项窗格之前使框架可见。

当然,JFrame现在将在屏幕上可见,因此您可以使用带负值的setLocation(...)方法来隐藏可见桌面中的帧。

© www.soinside.com 2019 - 2024. All rights reserved.