【问题标题】:JDialog not doing a re-layout when maximized最大化时JDialog不进行重新布局
【发布时间】:2012-05-17 22:05:15
【问题描述】:

我似乎和其他人有相反的问题。 默认情况下,我的 JDialog 同时具有最小化和最大化按钮。 当按下最大化按钮时,对话框会最大化——但内容不会。它只是保持相同的大小,以一个巨大的对话框为中心。 当您抓住边缘并重新调整对话框大小时,也会发生同样的情况。

我尝试添加一个 WindowStateListener - 但它永远不会被调用。 我添加了一个 WindowListener - 它只在 Open/Close/Activate/Deactivate 时调用。

因此,我要么需要能够使对话框内容随对话框重新调整大小,要么删除最大化按钮。 (我想去掉最小化按钮。)

我确实在创建对话框后执行了一个 pack(),因为对话框中的控件是从数据块动态创建的,所以我没有可使用的初始大小。

好的,下面是代码。所有生成的 UI 面板也在 GridBagLayouts 中。

public class FastAccessDialog extends JDialog implements BeanActionListener {

private static final long   serialVersionUID = 1L;
private static final Cursor waitCursor       = new Cursor(Cursor.WAIT_CURSOR);

private Cursor              oldCursor;
private JPanel              cmdOutput;
private JScrollPane         cmdOutputScroll;

public FastAccessDialog(Frame owner, ObjectName bean, String methodName) throws InstanceNotFoundException, IntrospectionException,
        ReflectionException, IOException {
    super(owner);
    setResizable(true);
    setModal(false);
    setTitle(BeanUtil.cleanUpCamelCase(methodName));

    boolean enabled = (UIHintUtil.isEnabled(bean) == EnableState.ENABLED);

    // Find the BeanOperationInfo for that method.
    MBeanInfo info = JMXConnectionSingleton.getInstance().getMBeanInfo(bean);
    MBeanOperationInfo[] operations = info.getOperations();
    JComponent comp = null;

    for (MBeanOperationInfo opInfo : operations) {
        if (opInfo.getName().equals(methodName)) {
            comp = OperationsManager.getInstance().createControls(bean, opInfo, this, true, enabled);
            break;
        }
    }

    if (comp == null) {
        throw new IllegalArgumentException("Unknown method name: " + methodName);
    }

    Container cont = getContentPane();
    cont.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.WEST;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(4, 4, 4, 4);
    cont.add(comp, gbc);
    cont.validate();

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            pack();
        }
    });
    return;
}

... other methods invoked when an operation is performed ...
... none of which are invoked before having the re-size problem ...
}

【问题讨论】:

  • 发布一些代码,以便我们可以看到发生了什么。这通常与使用正确的 LayoutManager 并正确配置有关。
  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing layout-manager jdialog maximize


【解决方案1】:
  1. JPanel.(re)validate();

  2. JPanel.repaint();

  3. JDialog.pack();

  4. SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JDialog.setVisible(true);
        }
    });
    
  5. 不要扩展Top-Level Containers

  6. 不要使用ContentPane,没有理由...来自Java5

  7. JDialog.pack();JDialog.setVisible(true) 的情况下没有其他内容;是返回 JDialog 实例的 void、方法或构造函数中的最后一行代码

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    相关资源
    最近更新 更多