【问题标题】:how to resize the frame after an event happened?事件发生后如何调整框架的大小?
【发布时间】:2012-07-26 00:25:21
【问题描述】:

我创建了一个表单来选择数据库服务,例如 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?
            }
                    });
        }

这是 LinkedFrame 代码(从 JFrame 扩展):

    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 是一种用于收集所需信息的新表单,包括数据库名称、用户名和密码。这些信息应传递给连接器类的 MSSQLconnect 或 OracleConnect 方法。在此表单中,当您单击按钮时创建此表单,并在您填写所有字段并按 Enter 时消失...(参见上面的代码)

现在我有一些问题:

我想在填写空白并加热 ENTER 以及是否建立连接以进行查询后立即调整我的主框架(不是链接框架)的大小。

  1. 我应该使用JFrame的什么方法?

  2. 方法应该放在哪里(在主框架的按钮事件处理程序中或在Linkedframe的事件处理程序中或任何建议的地方)?

非常感谢您的帮助。

【问题讨论】:

  • 已修改....请再检查一遍...

标签: java swing events resize frame


【解决方案1】:

如果没有更多代码,我们可能很难为您提供完整的答案,但我会试一试。

只要您一次不想超过一个连接,就可以使用静态连接器。真的没有问题。但是,如果您这样做了,则需要将 Connector 作为构造函数的一部分或作为属性传递给 LinkedFrame,但这是一种设计选择。

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

一旦用户从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 并在其构造函数中将模态设置为 true。现在正如你所说,当在按钮事件处理程序中创建 LinkedFrame(第一个代码块)时,我的代码应该被阻止......我认为应该在 LinkedFrame 的处理程序中调用 setSize 方法,但我不知道如何在那里达到这个方法?!!!!
  • 我想linkedFrame.setSize(width, height) 应该可以正常工作,linkedFrame 是你的对话框的实例。
  • 我想为我的主框架而不是 LinkedFrame 设置大小...如何从 LinkedFrame 类中访问此方法?
  • 啊,对不起,你不应该这样做。一旦对话框返回(在您调用 setVisible 之后),您应该在主框架内进行更改。
  • 对不起,我不是很擅长java...linkedFrame在maiFrame前面打开,我想在对话框关闭后立即调整主框架的大小。请给我一个示例代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
  • 2022-07-19
  • 2019-06-27
  • 1970-01-01
相关资源
最近更新 更多