在开放课程之间进行交流

时间:2015-01-12 00:47:46

标签: java swing user-interface awt

我有三节课。一个是完成所有艰苦工作但没有显示任何东西的工人阶级。另外两个是GUI类,其中一个调用另一个。调用第二个GUI类的那个工作者类打开了。

第一个GUI使用此方法调用第二个:

protected void openAdd() {

        AddPet add = new AddPet(ADD_PROMPT, FIELDS, DATE_PROMPT);
        add.setVisible(true);
    }

第二个GUI类用于从工作者类中使用的用户获取信息,但是,因为我已经在第一个GUI中打开了工作者类,所以我不想再次打开它,我想使用第一个GUI中的一些信息。

我需要做的是将第二个GUI中的信息传递回第一个GUI,以便它可以使用它并将其传递给开放的工作者类。

我该怎么做?

编辑: 我认为最好的选择是从第二个GUI调用第一个GUI中的方法,但我不知道这是否可能。

1 个答案:

答案 0 :(得分:1)

第二个想法,看起来你的第二个窗口基本上被用作第一个窗口的对话框,并且你正在使用它来输入用户数据而没有其他东西。如果是这样,那么请确保第二个窗口不是JFrame,而是一个模态JDialog。如果是这样,那么当它打开时它将阻止用户与第一个窗口的交互,并且从中提取信息将很容易,因为当用户完成时,你知道完全,因为程序在您设置第二个窗口可见的代码之后,将立即在第一个GUI中恢复流程。

如,

// in this example, AddPet is a modal JDialog, not a JFrame
protected void openAdd() {
    // I'm passing *this* into the constructor, so it can be used ...
    //    ... in the JDialog super constructor
    AddPet add = new AddPet(this, ADD_PROMPT, FIELDS, DATE_PROMPT);
    add.setVisible(true);

    // code starts here immediately after the AddPet modal dialog is no longer visible
    // so you can extract information from the class easy:

    String petName = add.getPetName(); // I know you don't use these exact methods
    String petBreed = add.getPetBreed(); // but they're just a "for instance" type of code
    // ... etc

}

...