2009-11-12 103 views
-1

我有JList的調整問題... 下面是我的代碼...我的要求是用一些固定大小添加一個JList到面板,當我最大化/最小化框架下的JList中應該增加/相應減少。請幫助我..如何實現這一目標?調整與JList的問題?

以下是設計:

嗨卡爾, 它可以在簡單的情況下,但不是在上述情況下。 所以我現在地名釋義上面的代碼..

我有一個名爲「TestListControl」與GB佈局框架。 用GB佈局添加selectDevicesPanel。 在selectDevicesPanel: 符合GB佈局 添加selectNamesInnerPanel在selectNamesInnerPanel: 添加selectNameAddPanel符合GB佈局 添加selectTeamPanel與流佈局 在selectNameAddPanel: chooseDeviceScrollPane clickToAddDeletePanel selectedNameScrollPane 在selectTeamPanel: selectTeamLabel selectTeamComboBox

public class TestListControl extends javax.swing.JFrame { 

    public TestListControl() { 
     initComponents(); 
    } 

    private void initComponents() { 
     java.awt.GridBagConstraints gridBagConstraints; 

     selectDevicesPanel = new javax.swing.JPanel(); 
     selectNamesInnerPanel = new javax.swing.JPanel(); 
     selectNameAddPanel = new javax.swing.JPanel(); 
     chooseDeviceScrollPane = new javax.swing.JScrollPane(); 
     chooseNamesList = new javax.swing.JList(); 
     clickToAddDeletePanel = new javax.swing.JPanel(); 
     clickToAddButton = new javax.swing.JButton(); 
     clickToRemoveButton = new javax.swing.JButton(); 
     selectedNameScrollPane = new javax.swing.JScrollPane(); 
     selectedNamesList = new javax.swing.JList(); 
     selectNameLabel = new javax.swing.JLabel(); 
     selectTeamPanel = new javax.swing.JPanel(); 
     selectTeamLabel = new javax.swing.JLabel(); 
     selectTeamComboBox = new javax.swing.JComboBox(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     selectDevicesPanel.setBorder(javax.swing.BorderFactory 
       .createTitledBorder("Member Selection")); 
     selectDevicesPanel.setLayout(new java.awt.GridBagLayout()); 

     selectNamesInnerPanel.setLayout(new java.awt.GridBagLayout()); 

     selectNameAddPanel.setLayout(new java.awt.GridBagLayout()); 

     chooseNamesList.setModel(new javax.swing.AbstractListModel() { 
      String[] strings = { "XXXX", "YYYY", "ZZZZ" }; 

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

      public Object getElementAt(int i) { 
       return strings[i]; 
      } 
     }); 
     chooseNamesList.setMaximumSize(new java.awt.Dimension(46, 88)); 
     chooseNamesList.setMinimumSize(new java.awt.Dimension(94, 98)); 
     chooseDeviceScrollPane.setViewportView(chooseNamesList); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 2; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.weightx = 1.0; 
     gridBagConstraints.weighty = 1.0; 
     gridBagConstraints.insets = new java.awt.Insets(0, 12, 7, 0); 
     selectNameAddPanel.add(chooseDeviceScrollPane, gridBagConstraints); 

     clickToAddDeletePanel.setLayout(new java.awt.GridBagLayout()); 

     clickToAddButton.setText(">>"); 
     clickToAddDeletePanel.add(clickToAddButton, 
       new java.awt.GridBagConstraints()); 

     clickToRemoveButton.setText("<<"); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 2; 
     clickToAddDeletePanel.add(clickToRemoveButton, gridBagConstraints); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 1; 
     gridBagConstraints.gridy = 2; 
     gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 5); 
     selectNameAddPanel.add(clickToAddDeletePanel, gridBagConstraints); 

     selectedNamesList.setModel(new javax.swing.AbstractListModel() { 
      String[] strings = { "XXXX", "YYYY" }; 

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

      public Object getElementAt(int i) { 
       return strings[i]; 
      } 
     }); 
     selectedNamesList.setMinimumSize(new java.awt.Dimension(94, 98)); 
     selectedNameScrollPane.setViewportView(selectedNamesList); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 2; 
     gridBagConstraints.gridy = 2; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 
     gridBagConstraints.weightx = 1.0; 
     gridBagConstraints.weighty = 1.0; 
     gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0); 
     selectNameAddPanel.add(selectedNameScrollPane, gridBagConstraints); 

     selectNameLabel.setText("Select Name"); 
     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 1; 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 
     gridBagConstraints.insets = new java.awt.Insets(0, 0, 8, 0); 
     selectNameAddPanel.add(selectNameLabel, gridBagConstraints); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.gridx = 0; 
     gridBagConstraints.gridy = 2; 
     gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 
     gridBagConstraints.weightx = 1.0; 
     gridBagConstraints.weighty = 1.0; 
     selectNamesInnerPanel.add(selectNameAddPanel, gridBagConstraints); 

     selectTeamLabel.setText("Select Team"); 
     selectTeamPanel.add(selectTeamLabel); 

     selectTeamComboBox.setModel(new javax.swing.DefaultComboBoxModel(
       new String[] { "TEAM X", "TEAM Y", "TEAM Z" })); 
     selectTeamComboBox 
       .addActionListener(new java.awt.event.ActionListener() { 
        public void actionPerformed(java.awt.event.ActionEvent evt) { 
         selectTeamComboBoxActionPerformed(evt); 
        } 
       }); 
     selectTeamPanel.add(selectTeamComboBox); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 
     selectNamesInnerPanel.add(selectTeamPanel, gridBagConstraints); 

     gridBagConstraints = new java.awt.GridBagConstraints(); 
     gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 
     selectDevicesPanel.add(selectNamesInnerPanel, gridBagConstraints); 

     org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
       getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(layout.createParallelGroup(
       org.jdesktop.layout.GroupLayout.LEADING).add(0, 400, 
       Short.MAX_VALUE).add(
       layout.createParallelGroup(
         org.jdesktop.layout.GroupLayout.LEADING).add(
         layout.createSequentialGroup().add(0, 107, 
           Short.MAX_VALUE).add(selectDevicesPanel, 
           org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 
           185, 
           org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
           .add(0, 108, Short.MAX_VALUE)))); 
     layout.setVerticalGroup(layout.createParallelGroup(
       org.jdesktop.layout.GroupLayout.LEADING).add(0, 300, 
       Short.MAX_VALUE).add(
       layout.createParallelGroup(
         org.jdesktop.layout.GroupLayout.LEADING).add(
         layout.createSequentialGroup().add(0, 39, 
           Short.MAX_VALUE).add(selectDevicesPanel, 
           org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 
           221, 
           org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
           .add(0, 40, Short.MAX_VALUE)))); 

     pack(); 
    } 

    private void selectTeamComboBoxActionPerformed(
      java.awt.event.ActionEvent evt) { 
     // TODO add your handling code here: 
    } 

    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new TestListControl().setVisible(true); 
      } 
     }); 
    } 

    private javax.swing.JScrollPane chooseDeviceScrollPane; 
    private javax.swing.JList chooseNamesList; 
    private javax.swing.JButton clickToAddButton; 
    private javax.swing.JPanel clickToAddDeletePanel; 
    private javax.swing.JButton clickToRemoveButton; 
    private javax.swing.JPanel selectDevicesPanel; 
    private javax.swing.JPanel selectNameAddPanel; 
    private javax.swing.JLabel selectNameLabel; 
    private javax.swing.JPanel selectNamesInnerPanel; 
    private javax.swing.JComboBox selectTeamComboBox; 
    private javax.swing.JLabel selectTeamLabel; 
    private javax.swing.JPanel selectTeamPanel; 
    private javax.swing.JScrollPane selectedNameScrollPane; 
    private javax.swing.JList selectedNamesList; 

} 
+0

哪個列表? chooseNamesList或selectedNamesList? – 2009-11-12 07:21:46

+0

這兩個列表框應該最大化/最小化當我最大/最小化框架 – Sidharth 2009-11-12 07:42:01

+1

這GroupLayout的東西不容易重現你的問題。將您的測試程序減少到實際需要查看問題的一小部分組件會很體貼。 – 2009-11-12 07:45:00

回答

0

傢伙我解決了這個問題自己.. thnks烏拉圭回合統籌..actually我確實給予的weightx和沉重的,其持有的兩個JList中的父面板,爲JEditor錯誤窗格......簡單,我們需要設置權重參數對於所有包含要調整任何組件的父容器..

+0

不好意思說怎麼樣? – Gnoupi 2009-11-12 10:39:45

+0

Gnoupi以上 – Sidharth 2009-11-12 11:09:02

+0

簡單。看,我們需要設置權重參數爲所有的父容器包含而這正是我給你的前4小時,你顯然對解決這個問題的suggeston要調整任何部件......你自己。 – camickr 2009-11-12 17:07:30

2

如果你這樣做,你的名單將不會變得更大:

chooseNamesList.setMaximumSize(new java.awt.Dimension(46, 88));

+0

我設置chooseNamesList.setMaximumSize(新java.awt.Dimension中(146,188)); 。但仍然我得到這個問題 – Sidharth 2009-11-12 07:58:37

+0

嗨卡爾..你有解決這個問題的辦法嗎? – Sidharth 2009-11-12 09:27:39

+0

正如我所提到的,由於具有異國情調的GroupLayout類,我有一個複製程序的問題。事實上,我很高興你能夠自己找到解決方案。 – 2009-11-12 11:58:43

0

如果設置一個原型單元格的值,當數據被改變JList中不會調整。

爲setPrototypeCellValue(Object)方法見The JList Javadoc page