本文介绍了让JWindow保持焦点,当孩子聚焦,包括另一个窗口作为孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 JWindow ,它有几个 JTextInput 字段。这个 JWindow 父节点是 JFrame 。现在的情况 - 如果文本字段被点击 - 他们得到的焦点,但是 JWindow 也保持焦点。那很好。但是,如果我创建另一个 JWindow (第一个 JWindow 的子元素),并将其设置为可见, c $ c> JWindow 会丢失,并且 JWindow 的焦点会被重点关注。这不好。有没有办法保持焦点在父 JWindow ,并使子 JWindow 获得像<$ JTextInput 字段?

I have created JWindow, which has several JTextInput fields. This JWindow parent is JFrame. Now the situation - if text fields are clicked - they get the focus BUT JWindow also stays focused. That is good. But if I create another JWindow (child of first JWindow), and set it visible, then focus on first JWindow is lost, and the child JWindow is focused. This is bad. Is there some way to retain focus on the parent JWindow, and to make the child JWindow get the focus like JTextInput fields?

推荐答案



for example

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

public class WindowTest {

    private JFrame frame;
    private boolean bol = true;

    public WindowTest() {
        frame = new JFrame("Window Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);
        JWindow win = new JWindow(frame);
        win.setLayout(new GridLayout(0, 1));
        JTextField text = new JTextField("Show Window");
        text.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                if (!bol) {
                    JWindow win = new JWindow();
                    win.setLayout(new GridLayout(0, 1));
                    win.add(new JTextField("JTextField"));
                    win.add(new JTextField("JTextField"));
                    win.add(new JLabel("<html> Concurency Issues in Swing<br>"
                            + " never to use Thread.sleep(int) <br>"
                            + " durring EDT, simple to freeze GUI </html>"));
                    win.pack();
                    win.setLocation(350, 150);
                    win.setVisible(true);
                    bol = true;
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });
        win.add(text);
        win.add(new JTextField("JTextField"));
        win.add(new JTextField("JTextField"));
        win.pack();
        win.setLocation(250, 150);
        win.setVisible(true);

        bol = false;
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new WindowTest();
            }
        });
    }
}

这篇关于让JWindow保持焦点,当孩子聚焦,包括另一个窗口作为孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 15:46