将焦点从新的jFrame转移到先前的jFrame [英] Transferring focus from new jFrame to previous jFrame

查看:75
本文介绍了将焦点从新的jFrame转移到先前的jFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个JFrames jFrame1jFrame2,在jFrame1中有一个文本字段和一个按钮,同时单击按钮jFrame2会出现.在jFrame2中还有一个文本字段和一个按钮.我将在jFrame2的文本字段中键入一个名称,然后通过单击其中的按钮将文本字段值显示在jFrame1的文本字段上.但是我没有把焦点转移到jFrame1上,我尝试了代码,

I have two JFrames jFrame1 and jFrame2,in jFrame1 there's a textfield and a button,while clicking on button jFrame2 will appear. In jFrame2 there's also a textfield and a button.I will type a name in textfield of jFrame2 and by clicking the button in it that textfield value should appear on textfield of jFrame1. But I am not getting the focus transferred to jFrame1,i tried the code,

在jFrame1

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        jFrame2 abc=new jFrame2();
        abc.setVisible(true);
    }   


public void inserting(String name){
   jTextField1.requestFocusInWindow();
   jTextField1.setText(name);

 }

在jFrame2中,

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        jFrame1 abc1=new jFrame1();
       // abc1.transferFocus();  //not working

        abc1.inserting(jTextField1.getText());
        this.dispose();
    } 

我正在获得方法inserting()的价值,但未在文本字段中设置它.如果我再次为jFrame1提供setVisible(true),它可以工作,但是我不想那样做.还有其他解决方法吗?

I am getting value to the method inserting(),but it's not getting set into the textfield. If I again give setVisible(true) for jFrame1 it works,but I dont want to do i in that way. Is there any other way to resolve this?

推荐答案

要将焦点吸引到该字段,您应该使用requestFocusInWindow,但是我认为这不会使有问题的窗口重新成为焦点.

To bring focus to the field, you should use requestFocusInWindow, however I don't think that will bring the window in question back into focus.

您可以使用WindowListener监视可以响应的更改.

You could use a WindowListener to monitor changes that you could respond.

例如,在jFrame1actionPerformed处理程序中,您可以

For example, in jFrame1's actionPerformed handler you could

Frame02 frame2 = new Frame02();
frame2.addWindowListener(new WindowAdapter() {

    @Override
    public void windowClosed(WindowEvent we) {

        Frame02 frame2 = (Frame02) we.getWindow();
        jTextField1.setText(frame2.getText());

        toFront();
        jTextField1.requestFocusInWindow();

    }

});

frame2.setVisible(true);
frame2.toFront();
frame2.requestFocus();

jFrame1正在从jFrame2请求文本,原因是jFrame2不了解jFrame1,因此没有对其的引用.

jFrame1 is requesting the text from jFrame2 cause jFrame2 doesn't know about jFrame1, there's no reference to it.

jFrame2中,您需要添加WindowListener来处理文本字段的焦点请求

In jFrame2 you would need to add a WindowListener to handle the request for focus of the text field

addWindowListener(new WindowAdapter() {
    public void windowOpened(WindowEvent we) {
        jTextField1.requestFocus();
    }
});

这篇关于将焦点从新的jFrame转移到先前的jFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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