2012-01-13 16 views
1

我試圖按照標題說的去做。將大量不同的擺動物體添加到面板/框架的高效方法。 (Java)

我有一個有效的方法通過在陣列中存儲它們,將它們添加使用for循環,像這樣幾個發佈同揮杆對象的一個​​框架:

JLabel[] contrllabels= new JLabel[8]; 
contrllabels[0] = new JLabel("SCF Type: "); 
contrllabels[1] = new JLabel("Units: "); 
contrllabels[2] = new JLabel("Spherical Harmonics: "); 
contrllabels[3] = new JLabel("Molecular Charge: "); 
contrllabels[4] = new JLabel("PP: "); 
contrllabels[5] = new JLabel("DFT Type: "); 
contrllabels[6] = new JLabel("Max Iterations: "); 
contrllabels[7] = new JLabel("Mult: "); 


for(int i = 0;i<contrllabels.length;i++){ 
    c.gridy = i; 
    frame.add(contrllabels[i],c); 
} 

但是,如果有什麼幾種不同類型的搖擺物體?我有幾個組合框和文本框,我想以類似的方式添加到框架中。我使用gridbaglayout,所以如果我不使用for循環,最終會產生大量不必要的代碼,因爲每次我想要添加不同的對象時都會給出約束新的值。

有沒有這樣的事情作爲指向這些不同的對象,然後我可以迭代添加到框架的引用數組?像

JTextField tf = new JTextField(5); 
JComboBox cb = new JComboBox("example"); 

Swing[] array = {tf,cb} 

for(int i = 0;i<array.length;i++){ 
    c.gridy = i; 
    frame.add(array[i],c); 
} 

我知道這樣的數組不存在,但有沒有實現這樣的事情的某種方式?這將大大減少我的代碼中的行數,並減少混淆。

謝謝

+0

like'JComponent'? – Tedil 2012-01-13 15:46:41

+0

至少,你可以有對象數組。我建議你使用ArrayList。您可能會發現自己需要在將來移除一個物體,並且將會轉移所有元素......浪費大量時間! ArrayList由一個數組支持,稍微額外的開銷在我們現在運行的機器上毫無意義......甚至是手機!你應該考慮閱讀java.util.ArrayList代碼來看看它是如何運作的,你會更好地感受這一切。 – vinnybad 2012-01-13 16:58:06

回答

7

你可以使用一個公用的超類型的數組或集合,如JComponent的數組或ArrayList中。我很好奇,如果你正在使用GridBagConstraints的並行數組來處理每個正在添加的組件 - 呃。我經常使用組件數組,但通常如果它們像組件(如JLabel/JTextField對或一組JRadioButtons組件)。另外,爲了我的錢,我儘量避免使用GridBagLayout,而是使用更容易編碼的佈局來嵌套容器。

例如這個小GUI用的FlowLayout,BoxLayout的,BorderLayout的和網格佈局的組合製成:

enter image description here

保持該整個GUI使用的BorderLayout,該JTextArea中在中央的大的JPanel放置BorderLayout.CENTER,頂部的Provider JLabel和JTextField位於整體BorderLayout.NORTH的FlowLayout JPanel中,底部的按鈕位於使用GridLayout(1,0,5,0)的JPanel中,該JPanel保存在另一個JPanel使用放置在GUI BorderLayout.SOUTH中的FlowLayout,右側的東西使用JPanel在BoxLayout中。

例如:

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.Window; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.UIManager; 
import javax.swing.UIManager.LookAndFeelInfo; 
import javax.swing.UnsupportedLookAndFeelException; 

@SuppressWarnings("serial") 
public class GetLetterTextGui extends JPanel { 
    private static final int TA_ROWS = 20; 
    private static final int TA_COLS = 35; 
    private static final int PROVIDER_FIELD_COLS = 10; 
    private static final String GUI_TITLE = "Get Letter Text"; 
    private JList letterList; 
    private JTextArea textarea = new JTextArea(TA_ROWS, TA_COLS); 
    private JTextField providerField = new JTextField(PROVIDER_FIELD_COLS); 
    private JCheckBox addValedictionChkBox = new JCheckBox("Add Valediction", true); 

    public GetLetterTextGui() { 
     letterList = new JList(new String[]{"Fe", "Fi", "Fo", "Fum"}); 

     providerField.setText("John Smith, MD"); 

     textarea.setWrapStyleWord(true); 
     textarea.setLineWrap(true); 

     JPanel northPanel = new JPanel(); 
     northPanel.add(new JLabel("Provider:")); 
     northPanel.add(providerField); 

     JPanel southPanel = new JPanel(); 
     JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0)); 
     btnPanel.add(new JButton("Copy to Clipboard")); 
     btnPanel.add(new JButton("Clear")); 
     btnPanel.add(new JButton(new ExitAction())); 
     southPanel.add(btnPanel); 

     JPanel eastPanel = new JPanel(); 
     eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.PAGE_AXIS)); 
     eastPanel.add(new JScrollPane(letterList)); 
     eastPanel.add(new JPanel() {{add(addValedictionChkBox);}}); 

     int eb = 0; 
     setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb)); 
     setLayout(new BorderLayout(eb, eb)); 
     add(northPanel, BorderLayout.PAGE_START); 
     add(eastPanel, BorderLayout.LINE_END); 
     add(new JScrollPane(textarea), BorderLayout.CENTER); 
     add(southPanel, BorderLayout.PAGE_END); 
    } 


    private class ExitAction extends AbstractAction { 
     private final Object MNEMONIC = new Integer(KeyEvent.VK_X); 

     public ExitAction() { 
     super("Exit"); 
     putValue(MNEMONIC_KEY, MNEMONIC); 
     } 

     @Override 
     public void actionPerformed(ActionEvent evt) { 
     Window win = SwingUtilities.getWindowAncestor(GetLetterTextGui.this); 
     win.dispose(); 
     } 
    } 

    private static void createAndShowGui() { 
     GetLetterTextGui mainPanel = new GetLetterTextGui(); 

     JFrame frame = new JFrame(GUI_TITLE); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     try { 
     for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 

     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 

     } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
     } catch (InstantiationException e) { 
     e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
     } catch (UnsupportedLookAndFeelException e) { 
     e.printStackTrace(); 
     } 
    } 
} 
+0

您使用哪種佈局?爲了告訴你實情,我發現很多這些佈局讓我感到非常沮喪。 謝謝 – user1147964 2012-01-13 15:51:42

+0

@Hovercraft Full Of Eels'如果你使用的平行陣列'成像hmmm +1 – mKorbel 2012-01-13 15:52:29

+0

你的解決方案工作氣墊船。非常感謝你的幫助。我會嘗試不同的佈局組合 – user1147964 2012-01-13 15:58:48

4

所有的GUI組件都JComponent的對象。 ArrayList<JComponent>可以持有所有人的參考。試試看:

JFrame frame = new JFrame("test"); 
ArrayList<JComponent> cps = new ArrayList<JComponent>(); 
cps.add(new JLabel("Hello")); 
cps.add(new JPanel()); 
cps.add(new JButton("OK")); 

for (JComponent widget : cps) { 
    frame.add(widget); 
} 
相關問題