2013-04-03 32 views
3

我有一個圖形用戶界面,看起來如下: GUI 當我按下按鈕addComponent有新JPanels加入到(1),添加滾動窗格的JPanels但滾動面板不滾動,會發生什麼是它添加新的JPanels和jPanels的大小正在減小。動態添加JPanels到JScrollPane中

因此,我的問題是面板插入到JScrollPane中,並且這些面板的大小正在減小,但沒有滾動條。

這裏是我的代碼,我有母豬遠:

public class ContractForm extends JFrame { 

    private JPanel contentPane; 
    private JTextField txtContractID; 
    private JScrollPane scrollPane; 
    private JButton btnAddComponent; 
    private JButton btnOk; 
    private JComboBox<Customer> comboBoxCustomer; 
    private JButton btnCancel; 
    private ArrayList<JPanel> rows; 
    private JPanel panel; 
    private Dimension size; 


    /** 
    * Create the frame. 
    */ 
    public ContractForm() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(250, 250, 654, 476); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

       rows = new ArrayList<JPanel>(); 

     JLabel lblContractid = new JLabel("ContractID"); 
     lblContractid.setBounds(50, 11, 78, 14); 
     contentPane.add(lblContractid); 

     txtContractID = new JTextField(); 
     txtContractID.setBounds(138, 8, 86, 20); 
     contentPane.add(txtContractID); 
     txtContractID.setColumns(10); 

     JLabel lblNewLabel = new JLabel("Customer"); 
     lblNewLabel.setBounds(276, 11, 69, 14); 
     contentPane.add(lblNewLabel); 

     comboBoxCustomer = new JComboBox<Customer>(); 
     comboBoxCustomer.setBounds(355, 8, 235, 20); 
     contentPane.add(comboBoxCustomer); 

     JLabel lblYear = new JLabel("Year:"); 
     lblYear.setBounds(50, 37, 46, 14); 
     contentPane.add(lblYear); 

     JSpinner spinner = new JSpinner(); 
     spinner.setBounds(138, 31, 86, 20); 
     contentPane.add(spinner); 

     scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     scrollPane.setBounds(0, 59, 628, 312); 
     contentPane.add(scrollPane); 

     panel = new JPanel(); 
     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
     size = panel.getSize(); 
     scrollPane.setViewportView(panel); 


     btnAddComponent = new JButton("addComponent"); 
     btnAddComponent.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       ArrayList<JobDescription> jobs = Database.getInstance().getJobDescription(); 
       JobDescriptionContractRow row = new JobDescriptionContractRow(jobs); 
       rows.add(row); 
       panel.add(row); 
       scrollPane.repaint(); 
       scrollPane.revalidate(); 

      } 
     }); 
     btnAddComponent.setBounds(355, 30, 114, 23); 
     contentPane.add(btnAddComponent); 

     btnOk = new JButton("Ok"); 
     btnOk.setBounds(185, 382, 89, 23); 
     contentPane.add(btnOk); 

     btnCancel = new JButton("Cancel"); 
     btnCancel.setBounds(298, 382, 89, 23); 
     contentPane.add(btnCancel); 




    } 
} 

public class JobDescriptionContractRow extends JPanel { 
    private JTextField textField; 
    private JButton btnRemove; 

    /** 
    * Create the panel. 
    */ 
    public JobDescriptionContractRow(ArrayList<JobDescription> jobs) { 
     setLayout(null); 

     JLabel lblJobdescription = new JLabel("Job description"); 
     lblJobdescription.setBounds(10, 14, 94, 14); 
     add(lblJobdescription); 

     JComboBox<JobDescription> comboBox = new JComboBox<JobDescription>(); 
     comboBox.setBounds(102, 11, 149, 20); 
     for(JobDescription job : jobs){ 
      comboBox.addItem(job); 
     } 
     comboBox.setSelectedItem(null); 
     add(comboBox); 

     textField = new JTextField(); 
     textField.setBounds(365, 11, 86, 20); 
     add(textField); 
     textField.setColumns(10); 

     JLabel lblNewLabel = new JLabel("Contract price"); 
     lblNewLabel.setBounds(277, 14, 78, 14); 
     add(lblNewLabel); 

     JButton btnRemove = new JButton("remove"); 
     btnRemove.setBounds(469, 10, 89, 23); 
     add(btnRemove); 


    } 
} 

回答

3

JPanel(您的JScrollPane視圖分量)使用FlowLayout默認,尊重部件的優選尺寸。您可以覆蓋您添加的JPanelsgetPreferredSize以正確調整它們的大小。如果不這樣做,JScrollPane會計算所有尺寸以適合ViewPortView

側注:避免使用絕對定位(null佈局)並使用layout manager

+0

非常感謝,現在尺寸保持不變,滾動條琶音 – Bjorn

0

可以強制JScrollPane的使用來調整。

JViewport v = new JViewport(); 
v.setSetViewSize(new Dimention(width,height)); 
scroll.setViewport(v); 
+0

'setViewport(n)'??不應該是'v'嗎? – BRHSM

+0

我確實相信。好趕上 –