我似乎有其他人相反的问题。默认情况下,我的 JDialog 同时具有最小化和最大化按钮。当按下最大化按钮时,对话框会最大化——但内容不会。它只是保持相同的大小,以一个巨大的对话框为中心。当您抓住边缘并重新调整对话框大小时,也会发生同样的情况。
我试过添加一个 WindowStateListener - 但它永远不会被调用。我添加了一个 WindowListener - 它只在打开/关闭/激活/停用时调用。
所以,我要么需要能够让对话框内容随对话框重新调整大小,要么删除最大化按钮。(我想摆脱最小化按钮。)
在创建对话框后我确实做了一个 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 ...
}