【问题标题】:Resizing a Container from a ActionListerner从 ActionListener 调整容器大小
【发布时间】:2013-10-22 15:58:57
【问题描述】:

我不确定这是否可能,但我试图做的是动态调整容器的大小。所以当用户点击登录时,容器会调整大小以显示新的按钮和面板。

目前我的代码看起来像这样

容器代码

        c = getContentPane();
    c.add(panel); //panel contains login panel, user panel and system panel
    setDefaultCloseOperation(EXIT_ON_CLOSE);        
    setVisible(true);
    setSize(400, 200); //width, height
    setLocation(600,0);
    setResizable(false);
    validate();

动作监听器

        loginButton.setText("Login");
    loginButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            c.setSize(400,630);
            c.validate();
            userScrollPane.setVisible(true);
            sysScrollPane.setVisible(true);
            resultsButtonPanel.setVisible(true);
            loginButton.setEnabled(false);
            resetButton.setEnabled(true);
            resultsButton.setEnabled(true);
            tfUser.setEditable(false);
            tfPass.setEditable(false);
            mb.setVisible(true);
        }
    });

【问题讨论】:

  • 尝试删除c.validate()并在末尾添加c.invalidate()
  • @DavidConrad 欢呼,不过没用 :(

标签: java swing containers


【解决方案1】:

//面板包含登录面板、用户面板和系统面板

面板只能用于一个目的。如果您有多个功能,则创建多个面板。

然后您可以使用Card Layout 根据需要在 GUI 中交换面板。

【讨论】:

  • 我不需要更换面板。成功登录后,用户应该可以看到所有面板。 @camickr
【解决方案2】:

您可以使用 setPreferedSize 然后打包到您的 Actionlistener 以动态调整面板的大小

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2017-06-06
    • 2012-11-11
    • 1970-01-01
    相关资源
    最近更新 更多