2012-05-17 19 views
0

我似乎有其他人的相反問題。 我的JDialog默認最小化和最大化按鈕。 按下最大化按鈕時,對話框會最大化 - 但內容不會。它只是保持相同的大小,集中在一個巨大的對話框中。 只需抓住邊緣並重新調整對話框的大小就會發生同樣的情況。當最大化時,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 ... 
} 
+2

發佈一些代碼,以便我們可以看到發生了什麼。這通常與使用正確的LayoutManager並正確配置相關。 –

+3

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

2
  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,方法或構造函數中的最後一行代碼行

相關問題