简体   繁体   English

将焦点从新的jFrame转移到先前的jFrame

[英]Transferring focus from new jFrame to previous jFrame

I have two JFrames jFrame1 and jFrame2 ,in jFrame1 there's a textfield and a button,while clicking on button jFrame2 will appear. 我有两个jFrame1jFrame2 ,在jFrame1中有一个文本字段和一个按钮,单击按钮jFrame2会出现。 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. 在jFrame2中还有一个文本字段和一个按钮。我将在jFrame2的文本字段中键入一个名称,然后通过单击其中的按钮将文本字段值显示在jFrame1的文本字段上。 But I am not getting the focus transferred to jFrame1,i tried the code, 但是我没有把焦点转移到jFrame1上,我尝试了代码,

in jFrame1 在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);

 }

in jFrame2, 在jFrame2中,

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

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

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

I am getting value to the method inserting() ,but it's not getting set into the textfield. 我正在为方法inserting()增值,但未在文本字段中设置它。 If I again give setVisible(true) for jFrame1 it works,but I dont want to do i in that way. 如果我再次为jFrame1提供setVisible(true) ,它可以工作,但我不想那样做。 Is there any other way to resolve this? 还有其他解决方法吗?

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. 为了使焦点集中在该字段上,应该使用requestFocusInWindow ,但是我认为这不会使有问题的窗口重新成为焦点。

You could use a WindowListener to monitor changes that you could respond. 您可以使用WindowListener监视可以响应的更改。

For example, in jFrame1 's actionPerformed handler you could 例如,在jFrame1actionPerformed处理程序中,您可以

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 is requesting the text from jFrame2 cause jFrame2 doesn't know about jFrame1 , there's no reference to it. jFrame1请求从文本jFrame2原因jFrame2不知道jFrame1 ,有它没有提及。

In jFrame2 you would need to add a WindowListener to handle the request for focus of the text field jFrame2您需要添加一个WindowListener来处理文本字段焦点的请求

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

If you're using your second frame to just take a user's input, why not switch to using a JOptionPane.showInputDialog() ? 如果要使用第二帧只是接受用户输入,为什么不切换到使用JOptionPane.showInputDialog()呢? You can configure this to give you a TextField and a Button and returns a String? 您可以配置它以为您提供一个TextField和一个Button并返回一个String? Use this value to set the value of the JTextField in your first frame. 使用此值可以在第一帧中设置JTextField的值。

So your first method would be something like this: 因此,您的第一种方法将是这样的:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String inputString = JOptionPane.showInputDialog(this, "Enter Value: ");
    jTextField1.setText(inputString);
}  

I think this one would be a simpler solution instead of working with a couple of frames and switching focus in between them. 我认为这是一个简单的解决方案,而不是使用几个框架并在两个框架之间切换焦点。

This tutorial about "Getting the User's Input from a Dialog" might help you get a better picture about using Input Dialog. 本教程“从对话框获取用户输入”可能有助于您更好地了解使用输入对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM