2013-10-11 140 views
1

我想知道如何動態地將組件添加到JDialog。我知道在SO here上有類似的問題,但正如你所看到的,我將他的解決方案作爲我的代碼的一部分。動態地將組件添加到按鈕單擊的JDialog上

所以這個想法是,點擊按鈕,我需要在對話框中添加一個組件。該示例代碼如下:

import java.awt.Container; 
import java.awt.Dialog; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
public class Test extends JFrame { 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

public static void main(String args[]) { 
    Test test = new Test(); 
    test.createDialog(); 
} 

public void createDialog() { 
    DynamicDialog dialog = new DynamicDialog(this); 
    dialog.setSize(300, 300); 
    dialog.setVisible(true); 
} 
} 

class DynamicDialog extends JDialog { 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

public DynamicDialog(final JFrame owner) { 
    super(owner, "Dialog Title", Dialog.DEFAULT_MODALITY_TYPE); 
    final JPanel mainPanel = new JPanel(); 
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); 

    final JPanel panel = new JPanel(); 
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); 
    panel.add(Box.createRigidArea(new Dimension(3, 10))); 
    panel.add(createLabel("Click on add")); 
    panel.add(Box.createRigidArea(new Dimension(23, 10))); 
    panel.add(createLabel("To add another line of text")); 
    panel.add(Box.createHorizontalGlue()); 
    mainPanel.add(panel); 
    mainPanel.add(Box.createRigidArea(new Dimension(3, 10))); 

    JPanel buttonPanel = new JPanel(); 
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); 
    buttonPanel.add(Box.createHorizontalGlue()); 

    JButton button = new JButton(); 
    button.setText("Add another line"); 
    buttonPanel.add(button); 
    mainPanel.add(buttonPanel); 
    mainPanel.add(Box.createRigidArea(new Dimension(3, 10))); 

    button.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      Container contentPane = owner.getContentPane(); 
      JPanel _panel = new JPanel(); 
      _panel.setLayout(new BoxLayout(_panel, BoxLayout.X_AXIS)); 
      _panel.add(Box.createHorizontalGlue()); 
      _panel.add(createLabel("Added!")); 
      contentPane.add(_panel); 

      contentPane.validate(); 
      contentPane.repaint(); 
      owner.pack(); 
     } 
    }); 

    pack(); 
    setLocationRelativeTo(owner); 
    this.add(mainPanel); 
} 

JLabel createLabel(String name) { 
    JLabel label = new JLabel(name); 
    return label; 
} 
} 

回答

3

如果你把它添加到主面板,將工作,你將它添加到它似乎它不顯示任何地方幀的內容窗格中。

button.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent event) { 

     JPanel _panel = new JPanel(); 
     _panel.setLayout(new BoxLayout(_panel, BoxLayout.X_AXIS)); 
     _panel.add(Box.createHorizontalGlue()); 
     _panel.add(createLabel("Added!")); 
     mainPanel.add(_panel); 

     mainPanel.validate(); 
     mainPanel.repaint(); 
     owner.pack(); 
    } 
}) 
+0

非常感謝,真的很有幫助! –

+0

雖然如果您必須使用'owner.pack()',那麼不需要顯式調用child.validate/revalidate和child.repaint,因爲這可以通過調用來隱式完成'包'對所有者:-) +1 :-) –

相關問題