2012-06-18 73 views
3

我是新來的Java Swing我想開發的POS應用程序,它是這樣的形象: enter image description here將組件添加到JScrollPane的動態

開發這個應用程序,我使用Eclipse,我已經創建的JPanel這是由所示數字例如(1,2)在圖像中。在3號我添加了包含JPanel的JscrollPan,現在我想在這裏做點什麼,當我點擊2號面板中的一個按鈕時,應該動態地在面板3中添加新的按鈕,那時我只想在每一行顯示3個按鈕滾動應該只在需要時垂直激活。但我無法這樣做,因爲當我寫scrollPanel.setPreferredSize(new Dimension(2,3));垂直滾動不起作用。我的代碼是在這裏:

public class WelcomeUI extends JFrame { 
    private JPanel contentPane; 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        WelcomeUI frame = new WelcomeUI(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
    public WelcomeUI() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(Toolkit.getDefaultToolkit().getScreenSize()); 
     setUndecorated(true);  
     contentPane = new JPanel(); 
     contentPane.setBackground(Color.LIGHT_GRAY); 
     contentPane.setBorder(new EmptyBorder(0, 0, 0, 0)); 
     contentPane.setLayout(new BorderLayout(0, 0)); 
     setContentPane(contentPane);   
     header = new JPanel(); 
     header.setForeground(Color.BLUE); 
     FlowLayout fl_header = (FlowLayout) header.getLayout(); 
     fl_header.setAlignment(FlowLayout.RIGHT); 
     fl_header.setVgap(40); 
     fl_header.setHgap(0); 
     header.setBackground(Color.BLUE); 
     contentPane.add(header, BorderLayout.NORTH); 

     footer = new JPanel(); 
     FlowLayout flowLayout = (FlowLayout) footer.getLayout(); 
     flowLayout.setVgap(10); 
     footer.setBackground(Color.BLUE); 
     contentPane.add(footer, BorderLayout.SOUTH); 

     panel_2 = new JPanel(); 
     panel_2.setBackground(Color.BLUE); 
     contentPane.add(panel_2, BorderLayout.WEST); 

     JButton btnSelectRestaurant = new JButton("Select Restaurant"); 
     btnSelectRestaurant.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       createDynamicButton(e); 
      } 
     });  
     JButton btnNewButton = new JButton("Select Steward");  
     JButton btnNewButton_1 = new JButton("Select Table");  
     JButton btnNewButton_2 = new JButton("Misc keys"); 
     GroupLayout gl_panel_2 = new GroupLayout(panel_2); 
     gl_panel_2.setHorizontalGroup(
      gl_panel_2.createParallelGroup(Alignment.LEADING) 
       .addGroup(gl_panel_2.createSequentialGroup() 
        .addGap(19) 
        .addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING) 
         .addComponent(btnNewButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE) 
         .addComponent(btnSelectRestaurant, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
         .addComponent(btnNewButton_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE) 
         .addComponent(btnNewButton_2, Alignment.TRAILING, 0, 0, Short.MAX_VALUE)) 
        .addContainerGap()) 
     ); 
     gl_panel_2.setVerticalGroup(
      gl_panel_2.createParallelGroup(Alignment.LEADING) 
       .addGroup(gl_panel_2.createSequentialGroup() 
        .addGap(26) 
        .addComponent(btnSelectRestaurant, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE) 
        .addPreferredGap(ComponentPlacement.UNRELATED) 
        .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE) 
        .addPreferredGap(ComponentPlacement.UNRELATED) 
        .addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE) 
        .addPreferredGap(ComponentPlacement.UNRELATED) 
        .addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE) 
        .addContainerGap(438, Short.MAX_VALUE)) 
     ); 
     gl_panel_2.setAutoCreateContainerGaps(true); 
     gl_panel_2.setAutoCreateGaps(true); 
     panel_2.setLayout(gl_panel_2); 

     panel_3 = new JPanel(); 
     FlowLayout flowLayout_2 = (FlowLayout) panel_3.getLayout(); 
     flowLayout_2.setHgap(250); 
     contentPane.add(panel_3, BorderLayout.EAST); 

     scrollPane = new JScrollPane();  
     contentPane.add(scrollPane, BorderLayout.CENTER);  
     scrollPanel = new JPanel();  
     scrollPanel.setPreferredSize(new Dimension(2, 3)); 
     scrollPane.setViewportView(scrollPanel);   
    } 
    protected void createDynamicButton(ActionEvent e) {  
     JButton dynButton = new JButton("My Button"); 
     dynButton.setPreferredSize(new Dimension(300, 200));   
     scrollPanel.add(dynButton);   
     scrollPanel.validate(); 
     scrollPanel.revalidate();  
    } 
    private JPanel header; 
    private JPanel footer; 
    private JPanel panel_2; 
    private JPanel panel_3; 
    private JPanel scrollPanel; 
    private JScrollPane scrollPane; 
} 

所以,請告訴我我做錯了什麼。提前致謝。

+0

請嘗試使您的問題更加簡潔 – dagnelies

回答

2

聚焦面板3上,考慮下列因素:

  1. 給所述面板GridLayout(0, 3),它指定在三列的任意數量的行。
  2. 執行Scrollable接口,並讓getPreferredScrollableViewportSize()返回一個Dimension,它是按鈕高度的倍數。
  3. 隨着每個按鈕的添加,給它一個合適的Action
  4. 當添加一個按鈕,revalidate()面板和repaint()它。
+0

@mKorbel:糟糕,我應該說'revalidate()',說明[here](http://stackoverflow.com/a/4130167/230513);感謝審查。 – trashgod

+0

這個無效是一些沒有明確理由的歷史方法,[我試過,我測試過](http://stackoverflow.com/a/10985931/714968) – mKorbel

+0

啊,'revalidate()'_calls_'invalidate()'支持Swing推遲自動佈局。 – trashgod

1

我得到了答案,實際上問題是,我已經爲面板設置了最大尺寸,並將所有動態生成的按鈕放置在單行,添加到最大尺寸沒有遇到,但功能無法與其他溝通,因此我們需要動態更改大小,並通過修改FlowLayout interface來實現。

您可以使用修改的佈局而不是FlowLayout。