如何在事件发生后重新调整框架大小?

3

我创建了一个表格,用于选择数据库服务,如SQL Server和Oracle及其版本。然后通过单击“连接”按钮连接到它……但在建立连接之前,需要设置一些参数以便放置在URL中……此代码是为“连接”按钮编写的。

jButton2 = new JButton();
getContentPane().add(jButton2);
jButton2.setText("Connect");
jButton2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            LinkedFrame inst = new LinkedFrame();
            inst.setLocationRelativeTo(rootPane);
            inst.setVisible(true);
//Question:  Should I add any method here to do what I want? , and what method should I add?
            }
                    });
        }

以下是扩展自JFrame的 LinkedFrame 代码:

    private class DatabaseSelectionHandler implements ActionListener{
    public void actionPerformed(ActionEvent evt){
        database=jTextField1.getText();
        username=jTextField2.getText();
        pass=new String(jPasswordField1.getPassword());
        if(database.isEmpty() || username.isEmpty() || pass.isEmpty())
            JOptionPane.showMessageDialog(null, "Please fill all fields", "Error", JOptionPane.ERROR_MESSAGE);
        else
        {   setVisible(false);
            if (service.equalsIgnoreCase("sqlserver"))
                Connector.MSSQLConnection(service);//Single tone connectioto SQL Server
            else
                Connector.ORACLEConnection(service);//Single tone connection to Oracle
//Question:  Should I add any method here to do what I want? , and what method should I add?
        }
    }       
}

LinkedFrame 是一个新的表单,用于收集必要信息,包括数据库名称、用户名和密码。这些信息应该传递给 Connector 类的 MSSQLconnect 或 OracleConnect 方法。当您单击按钮时,将创建此表单,并在填写字段并按下 Enter 后消失...(请参阅上面的代码)
现在我有一些问题:
我想在填写空白并按下 ENTER 并且连接成功后立即调整我的主框架大小以进行查询。
1. 我应该使用 JFrame 的哪个方法? 2. 该方法应放置在哪里(在主框架的按钮事件处理程序中还是在 Linkedframe 的事件处理程序中或其他任何建议的位置)?
非常感谢您的帮助。

已翻译:它已经修订了...请再次检查。 - msc87
1个回答

3

没有更多的代码,我们可能很难为您提供完整的答案,但我会尽力而为。

使用静态连接器是可以的,只要您永远不需要同时使用多个连接。 没有实际问题。 但是,如果您确实需要,则需要将Connector传递给LinkedFrame,可以作为构造函数的一部分或属性,但这是一个设计选择。

对于LinkedFrame,我建议使用设置为modal的JDialog。 这将阻止用户输入,直到关闭对话框。 这也意味着您可以显示对话框,并且您的代码将被阻塞,直到对话框关闭。 这为您的代码提供了一个“陷阱”。

一旦用户提供了您从LinkedFrame中想要的信息并关闭了对话框,您就可以提取所需的详细信息(如果有的话),并相应地调整主窗口的大小。

更新

public void actionPerformed(ActionEvent evt) {

    LinkedFrame linkedFrame = new LinkedFrame(); // create the dialog, set as modal
    linkedFrame.setVisible(true); // code will block here till you close the dialog

    setSize(width, height); // supply the width & height you want
}

我将LinkedFrame更改为jDialog,并在其构造函数中将modal设置为true。现在正如您所说的那样,当创建LinkedFrame时(第一个代码块)我的代码应该被阻止。我认为setSize方法应该在LinkedFrame的处理程序中调用,但我不知道怎么在那里达到这个方法?!!! - msc87
我想 linkedFrame.setSize(width, height) 应该可以正常工作,其中 linkedFrame 是您对话框的一个实例。 - MadProgrammer
我想为我的主框架设置大小,而不是LinkedFrame...我该如何从LinkedFrame类中访问到这个方法? - msc87
啊,抱歉,你真的不应该这样做。一旦对话框返回(在调用setVisible之后),你应该在主框架内进行更改。 - MadProgrammer
不好意思,我并不是很擅长Java...linkedFrame在maiFrame前面打开,我希望在对话框关闭后立即调整主窗口的大小。请给我一个示例代码。 - msc87

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接