2012-11-10 190 views
1

我試圖添加一個JList到JPanel。具體來說,我有兩個JPanel,一個是正確的,一個是左邊的。正確的有兩個按鈕。在左邊我想要JList mySpriteOptions(見下面的代碼)。不幸的是,當我運行我的代碼時,這個JList永遠不會顯示出來。我不是專家(實際上我是一個巨大的新手),所以我不明白爲什麼會這樣。將JList添加到JPanel

這裏是我的代碼:

import java.awt.*; 
    import java.swing.* 


    public class SpriteEditorLauncher extends JFrame { 

    private JLabel mySearchBoxLabel; 
    private JList mySpriteOptions; 
    private JPanel myLeft; 
    private JPanel myRight; 
    private JScrollPane myScrollPane; 
    private JTextField mySearchBox; 
    private JButton myNewEditLauncher; 
    private JButton myEditLauncher; 

    private static final long serialVersionUID = 1L; 
    private static final String FONT_TYPE = "Times New Roman"; 
    private static final int FONT_SIZE = 12; 
    private static final int FONT_STYLE = 1; 
    private static final Font FONT = new Font(FONT_TYPE, FONT_STYLE, FONT_SIZE); 
    private static final int NUMBER_OF_ROWS = 1; 
    private static final int NUMBER_OF_COLUMNS = 2; 
    private static final int FRAME_WIDTH = 600; 
    private static final int FRAME_HEIGHT = 400; 

    public SpriteEditorLauncher() { 
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     initializeComponents(); 
     createLayout(); 
    } 

    private void initializeComponents() { 
     myLeft = new JPanel(); 
     myRight = new JPanel(); 
     myNewEditLauncher = new JButton(); 
     myEditLauncher = new JButton(); 
     myScrollPane = new JScrollPane(); 
     mySpriteOptions = new JList(); 
     mySearchBox = new JTextField(); 
     mySearchBoxLabel = new JLabel(); 

    } 

    private void setPanelBorder (JPanel toSetBorderFor) { 
     toSetBorderFor.setBorder(BorderFactory 
       .createTitledBorder(null, "Options", TitledBorder.DEFAULT_JUSTIFICATION, 
            TitledBorder.DEFAULT_POSITION, FONT)); 
    } 

    private void setButtonLabel (JButton button, Font font, String label) { 
     button.setFont(font); 
     button.setText(label); 
    } 

    private void setFrameLayout() { 
     GridLayout myLayout = new GridLayout(NUMBER_OF_ROWS, NUMBER_OF_COLUMNS); 
     setLayout (myLayout); 
     setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT)); 
     setResizable(false); 
    } 

    private void addPanelsToFrame() { 
     add(myLeft); 
     add(myRight); 
    } 

    private void addButtonsToPanel (JPanel panel) { 
     panel.add(myNewEditLauncher); 
     panel.add(myEditLauncher); 
    } 



    private void createLayout() { 

     setFrameLayout(); 
     setPanelBorder(myRight); 
     setButtonLabel(myNewEditLauncher, FONT, "New"); 
     setButtonLabel(myEditLauncher, FONT, "Edit"); 

     addPanelsToFrame(); 

     addButtonsToPanel(myRight); 



     mySpriteOptions.setModel(new AbstractListModel() { 

      private static final long serialVersionUID = 1L; 
      String[] strings = { "Item 1", "Item 2"}; 

      public int getSize() { 
       return strings.length; 
      } 

      public Object getElementAt (int i) { 
       return strings[i]; 
      } 
     }); 

     myLeft.add(mySpriteOptions); 

     myScrollPane.setViewportView(mySpriteOptions); 

     mySearchBox.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed (ActionEvent arg0) { 
       // TODO Auto-generated method stub 

      } 
     }); 

     mySearchBoxLabel.setFont(FONT); 
     mySearchBoxLabel.setLabelFor(mySearchBox); 
     mySearchBoxLabel.setText("Search:"); 

     pack(); 
     setVisible(true); 
    } 

} 

回答

4

你從不添加myScrollPaneJPanelmyLeft

myLeft.add(myScrollPane); 

雖然你已經添加mySpriteOptionsmyLeft,它也被設置爲myScrollPaneViewportView

的組件將永遠只能在最後容器上塗上它已經連接 —所以將只出現JScrollPane,但是這並沒有被添加。現在

,則不需要這樣的說法:

myLeft.add(mySpriteOptions); 
+0

非常感謝!這工作。 – covertbob

+0

不客氣@covertbob! – Reimeus

5

的默認佈局爲JPanelFlowLayout

FlowLayout喜歡使用它的組件的首選大小來佈置它們。

默認大小空JList可能是0x0,這意味着它永遠不會顯示。

嘗試用BorderLayoutmyRight = new JPanel(new BorderLayout());),並添加JList它,包裹在JScrollPane ...

private void addPanelsToFrame() { 
    add(myLeft); 
    add(new JScrollPane(myRight)); 
} 

創造合適的面板在

欲瞭解更多信息

+0

感謝MadProgrammer,我一定會看看這些鏈接。 – covertbob

+0

我使用OverlayLayout來處理滾動窗格中JPanel中的JList。 –