把焦点放在不是专注窗口的小孩的面板上? [英] give focus to a panel that is not a child of the focused Window?

查看:109
本文介绍了把焦点放在不是专注窗口的小孩的面板上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java来创建一个内部工具。这两个包含两个 JPanels ,它们可以在两个独立的屏幕上运行。我希望能够点击 JButton 这是 JFrame A 的一部分,它将发送一个键盘动作到 JFrame B

不幸的是我似乎无法做到这一点,因为 JFrame B 没有焦点,我可以'由于 JFrame B 不是的子项,所以请使用 Focus 方法中的任何一个。 Window JFrame A is)。

那么我怎样才能给 JFrame B 焦点,尽管它不是焦点窗口的子项或发送键盘事件 Frame B 它会回应而没有焦点吗?

解决方案

很难管理两个 JFrames a>,最好只创建一个 JFrame 和其他顶级容器 JDialogs ,创建一个/两个 JDialog(s)并通过删除所有JComponents来重复使用



基本上是这样的:

$ p $ EventBueue.invokeLater(new Runnable(){

@Override
public void run(){
someComponent.grabFocus();
someComponent.requestFocus(); //或inWindow取决于Swing或Awt
}
});

但是两个JFrame之间的东西有点复杂,只是基本的和未完成的例子.good.sun.forums.com)

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

public class PMDialog extends JDialog {

private static final long serialVersionUID = 1L;
private boolean modal = false;
private WindowAdapter parentWindowListener;
私人窗口所有者;
private JFrame blockedFrame = new JFrame(Blocked Frame);
private JFrame noBlockedFrame = new JFrame(No Blocked Frame);

public PMDialog(){
noBlockedFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
noBlockedFrame.getContentPane()。add(new JButton(new TestAction(Test button)){

private static final long serialVersionUID = 1L;

@Override
public void actionPerformed(ActionEvent evt){
System.out.println(Non blocked button push);
/ * if(blockedFrame.isVisible()){
noBlockedFrame .setVisible(false);
} else {
blockedFrame.setVisible(true);
} * /
noBlockedFrame.setVisible(true);
blockedFrame.setVisible true);
}
}));
noBlockedFrame.setSize(200,200);
noBlockedFrame.setVisible(true);
blockedFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
blockedFrame.getContentPane()。add(new JButton(new TestAction(Test Button){

private static final long serialVersionUID = 1L;

@Override
public void actionPerformed(ActionEvent evt){
final PMDialog pmd = new PMDialog(blockedFrame,Partial Modal Dialog,true);
pmd.setSize(200,100); $ b $添加新的JButton(新的AbstractAction(测试按钮){

private static final long serialVersionUID = 1L;


$ b $
$ @Override
public void actionPerformed(ActionEvent evt){
System.out.println(Blocked button push);
pmd.setVisible(false);
blockedFrame.setVisible(false);
noBlockedFrame.setVisible(t后悔);
}
}));
pmd.setDefaultCloseOperation(PMDialog.DISPOSE_ON_CLOSE);
pmd.setVisible(true);
System.out.println(从对话框返回);
}
}));
blockedFrame.setSize(200,200);
blockedFrame.setLocation(300,0);
blockedFrame.setVisible(false);

$ b $ public PMDialog(JDialog parent,String title,boolean isModal){
super(parent,title,false);
initDialog(parent,title,isModal);

$ b $ public PMDialog(JFrame parent,String title,boolean isModal){
super(parent,title,false);
initDialog(parent,title,isModal);


private void initDialog(Window parent,String title,boolean isModal){
owner = parent;
modal = isModal;
parentWindowListener = new WindowAdapter(){

@Override $ b $ public void windowActivated(WindowEvent e){
if(isVisible()){
System。通过out.println( Dialog.getFocusBack());
getFocusBack();
}
}
};

$ b $ private void getFocusBack(){
Toolkit.getDefaultToolkit()。beep();
super.setVisible(false);
super.pack();
super.setLocationRelativeTo(owner);
super.setVisible(true);
super.toFront();


@Override
public void dispose(){
owner.setEnabled(true);
owner.setFocusableWindowState(true);
super.dispose();

$ b @Override
@SuppressWarnings(deprecation)
public void hide(){
owner.setEnabled(true);
owner.setFocusableWindowState(true);
super.hide();


@Override
public void setVisible(boolean visible){
boolean blockParent =(visible&& modal);
owner.setEnabled(!blockParent);
owner.setFocusableWindowState(!blockParent);
super.setVisible(visible);
if(blockParent){
System.out.println(Adding listener to parent ...);
owner.addWindowListener(parentWindowListener);
尝试{
if(SwingUtilities.isEventDispatchThread()){
System.out.println(EventDispatchThread);
EventQueue theQueue = getToolkit()。getSystemEventQueue(); $(b)b(isVisible()){
AWTEvent event = theQueue.getNextEvent();
Object src = event.getSource();
if(eventEvent eventEvent){
((ActiveEvent)event).dispatch();
} else if(src instanceof Component){
((Component)src).dispatchEvent(event);


} else {
System.out.println(OUTSIDE EventDispatchThread);
synchronized(getTreeLock()){
while(isVisible()){
try {
getTreeLock()。wait();
} catch(InterruptedException e){
break;



} catch(Exception ex){
ex.printStackTrace();
System.out.println(Error from EDT ...:+ ex);

} else {
System.out.println(从父级删除侦听器...);
owner.removeWindowListener(parentWindowListener);
owner.setEnabled(true);
owner.setFocusableWindowState(true);
}
}

@Override
public void setModal(boolean modal){
this.modal = modal;


public static void main(String args []){
PMDialog pMDialog = new PMDialog();




$ b编辑:如何将焦点添加到 JDialog 是否有优秀的Woodoo由camickr Dialog Focus ,但是 AncestorListener 不是我的一杯Java,也不是我的抽象

I'm working in Java to create an internal tool. The two consists of two JPanels which are anticipated to run on two separate screens. I want to be able to click a JButton which is part of JFrame A which will send a keyboard action to JFrame B.

Unfortunately I can't seem to do this because JFrame B doesn't have the focus and I can't use any of the request Focus methods because JFrame B is not a child of the Focus Window (JFrame A is).

So how can I either give JFrame B focus despite it not being a child of the Focus Window or send Keyboard Events to JFrame B that it will respond to without having focus?

解决方案

is so hard to manage Focus between two JFrames, better would be create only one JFrame and other Top-level Containers would be JDialogs, create one/two JDialog(s) and reuse that by removing all JComponents

basicaly it would be :

EventQueue.invokeLater(new Runnable() {

   @Override
     public void run() {
         someComponent.grabFocus();
         someComponent.requestFocus();//or inWindow depends if Swing or Awt
     }
});

but between two JFrames is things little bit complicated, just basic and uncompleted example (based on code from old.good.sun.forums.com)

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

public class PMDialog extends JDialog {

    private static final long serialVersionUID = 1L;
    private boolean modal = false;
    private WindowAdapter parentWindowListener;
    private Window owner;
    private JFrame blockedFrame = new JFrame("Blocked Frame");
    private JFrame noBlockedFrame = new JFrame("No Blocked Frame");

    public PMDialog() {
        noBlockedFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        noBlockedFrame.getContentPane().add(new JButton(new AbstractAction("Test button") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent evt) {
                System.out.println("Non blocked button pushed");
                /*if (blockedFrame.isVisible()) {
                noBlockedFrame.setVisible(false);
                } else {
                blockedFrame.setVisible(true);
                }*/
                noBlockedFrame.setVisible(true);
                blockedFrame.setVisible(true);
            }
        }));
        noBlockedFrame.setSize(200, 200);
        noBlockedFrame.setVisible(true);
        blockedFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        blockedFrame.getContentPane().add(new JButton(new AbstractAction("Test Button") {

            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent evt) {
                final PMDialog pmd = new PMDialog(blockedFrame, "Partial Modal Dialog", true);
                pmd.setSize(200, 100);
                pmd.setLocationRelativeTo(blockedFrame);
                pmd.getContentPane().add(new JButton(new AbstractAction("Test button") {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        System.out.println("Blocked button pushed");
                        pmd.setVisible(false);
                        blockedFrame.setVisible(false);
                        noBlockedFrame.setVisible(true);
                    }
                }));
                pmd.setDefaultCloseOperation(PMDialog.DISPOSE_ON_CLOSE);
                pmd.setVisible(true);
                System.out.println("Returned from Dialog");
            }
        }));
        blockedFrame.setSize(200, 200);
        blockedFrame.setLocation(300, 0);
        blockedFrame.setVisible(false);
    }

    public PMDialog(JDialog parent, String title, boolean isModal) {
        super(parent, title, false);
        initDialog(parent, title, isModal);
    }

    public PMDialog(JFrame parent, String title, boolean isModal) {
        super(parent, title, false);
        initDialog(parent, title, isModal);
    }

    private void initDialog(Window parent, String title, boolean isModal) {
        owner = parent;
        modal = isModal;
        parentWindowListener = new WindowAdapter() {

            @Override
            public void windowActivated(WindowEvent e) {
                if (isVisible()) {
                    System.out.println("Dialog.getFocusBack()");
                    getFocusBack();
                }
            }
        };
    }

    private void getFocusBack() {
        Toolkit.getDefaultToolkit().beep();
        super.setVisible(false);
        super.pack();
        super.setLocationRelativeTo(owner);
        super.setVisible(true);
        super.toFront();
    }

    @Override
    public void dispose() {
        owner.setEnabled(true);
        owner.setFocusableWindowState(true);
        super.dispose();
    }

    @Override
    @SuppressWarnings("deprecation")
    public void hide() {
        owner.setEnabled(true);
        owner.setFocusableWindowState(true);
        super.hide();
    }

    @Override
    public void setVisible(boolean visible) {
        boolean blockParent = (visible && modal);
        owner.setEnabled(!blockParent);
        owner.setFocusableWindowState(!blockParent);
        super.setVisible(visible);
        if (blockParent) {
            System.out.println("Adding listener to parent ...");
            owner.addWindowListener(parentWindowListener);
            try {
                if (SwingUtilities.isEventDispatchThread()) {
                    System.out.println("EventDispatchThread");
                    EventQueue theQueue = getToolkit().getSystemEventQueue();
                    while (isVisible()) {
                        AWTEvent event = theQueue.getNextEvent();
                        Object src = event.getSource();
                        if (event instanceof ActiveEvent) {
                            ((ActiveEvent) event).dispatch();
                        } else if (src instanceof Component) {
                            ((Component) src).dispatchEvent(event);
                        }
                    }
                } else {
                    System.out.println("OUTSIDE EventDispatchThread");
                    synchronized (getTreeLock()) {
                        while (isVisible()) {
                            try {
                                getTreeLock().wait();
                            } catch (InterruptedException e) {
                                break;
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println("Error from EDT ... : " + ex);
            }
        } else {
            System.out.println("Removing listener from parent ...");
            owner.removeWindowListener(parentWindowListener);
            owner.setEnabled(true);
            owner.setFocusableWindowState(true);
        }
    }

    @Override
    public void setModal(boolean modal) {
        this.modal = modal;
    }

    public static void main(String args[]) {
        PMDialog pMDialog = new PMDialog();
    }
}

EDIT: for How to adds Focus to the JDialog is there excelent Woodoo by camickr Dialog Focus but AncestorListener isn't my cup of Java and too much abstract for me

这篇关于把焦点放在不是专注窗口的小孩的面板上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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