使用 JFrame 作为自定义输入框 [英] Use JFrame as a custom input box

查看:97
本文介绍了使用 JFrame 作为自定义输入框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于 java swing 的应用程序,其中有两个 JFrames:A,它是主窗口,B,在 A 上调用.

I am working on a java swing based application where i have two JFrames: A, which is the main window, and B, which is called on A.

我需要做的是,在 A 中,调用 B,从 B 获取用户输入并将该输入传递给 A 不知何故,然后处理它

What i need to do is to, in A, call B, get user input from B and pass that input to A somehow, to then proccess it

我尝试过的一切都失败了,据我所知 Java 不支持 async/await

Everything I've tried has failed, and as far as I know Java doesn't support async/await

这是我最好的尝试

    public void getInfoFromB()
    {
        FormB b = new FormB();

        while(b.isReady() == false){
            a.setVisible(false);
        }

        a.setVisible(true);
        ArrayList<String> b.getData();

        //...
    }

注意ab对象都继承了JFrameFormB的isReady是一个返回的方法Weathers 用户填写了所有需要的数据,getData 方法返回数据

Note that both a and b objects extends JFrame, FormB's isReady is a method that returns weathers the user has filled in all the data needed, and getData method returns said data

这种尝试似乎不起作用,因为一旦代码进入 while 循环,其他一切都会冻结,因为 java 似乎在处理并行操作

This attempt doesn't seem to work because once the code enters the while loop, everything else just freezes, as java seems to struggle with parallel actions

推荐答案

按照@TT. 的建议,可以通过引用将 A 传递给 B(显然所有对象都是通过引用传递的,因此不需要使用指针).从那里,我可以操作来自 A 和 B 的数据

As suggested by @TT., it is possible to pass A to B by reference (Apparently all objects are passed by reference, so there's no need to use pointers). From there, i could manipulate data from both A and B

A级

public void getDataFromB(ArrayList<String> data)
{
    this.dataFromB = data;
}

public void startFormB()
{
    this.setVisible(false);
    B formB = new B(this);
}

B级

//constructor
public B(formA a)
{
    this.mainForm = a;

    //...
}


public void setDataToAAndExit()
{
    this.mainForm.getDataFromB(this.dataToA);
    //re-show main window
    this.mainForm.setVisible(true);
    //close this window
    this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}

这篇关于使用 JFrame 作为自定义输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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