2013-04-09 66 views
0

我想要創建動態按鈕(例如15),但我可以在屏幕上看到它們(450,450),我嘗試使用滾動窗口,但我沒有結果。這個想法很簡單創建X按鈕,並有可能性在屏幕上滾動垂直(按鈕在屏幕中心)ScrollPane的動態按鈕java

這是代碼,但我不知道是誰讓scrollpane工作得很好..總是有相同的結果(我不包括滾動窗格代碼)

public class Consulta2 extends JFrame { 

    private JPanel contentPane; 
    private JLabel label; 
    private JButton boton; 


    public Consulta2() { 




     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(450, 450); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 


     int x=50; 
     for (int i=0;i<10;i++){ 


     JButton boton = new JButton("oli"); 

      GroupLayout gl_contentPane1 = new GroupLayout(contentPane); 
      gl_contentPane1.setHorizontalGroup(
       gl_contentPane1.createParallelGroup(Alignment.LEADING) 
        .addGroup(gl_contentPane1.createSequentialGroup() 
         .addGap(163) 
         .addComponent(boton) 
         .addContainerGap(172, Short.MAX_VALUE)) 
      ); 
      gl_contentPane1.setVerticalGroup(
       gl_contentPane1.createParallelGroup(Alignment.LEADING) 
        .addGroup(gl_contentPane1.createSequentialGroup() 
         .addGap(50+x) 
         .addComponent(boton) 
         .addContainerGap(229, Short.MAX_VALUE)) 
      ); 
      contentPane.setLayout(gl_contentPane1); 

      x=x+50; 
     //contentPane.add(boton); 


     setVisible(true); 
     } 

     label = new JLabel("New label"); 
     GroupLayout gl_contentPane = new GroupLayout(contentPane); 
     gl_contentPane.setHorizontalGroup(
      gl_contentPane.createParallelGroup(Alignment.LEADING) 
       .addGroup(gl_contentPane.createSequentialGroup() 
        .addGap(183) 
        .addComponent(label)) 
     ); 
     gl_contentPane.setVerticalGroup(
      gl_contentPane.createParallelGroup(Alignment.LEADING) 
       .addGroup(gl_contentPane.createSequentialGroup() 
        .addGap(68) 
        .addComponent(label)) 
     ); 
     contentPane.setLayout(gl_contentPane); 

     Dimension tamFrame=this.getSize();//para obtener las dimensiones del frame 
     Dimension tamPantalla=Toolkit.getDefaultToolkit().getScreenSize();  //para obtener el tamanio de la pantalla 
     setLocation((tamPantalla.width-tamFrame.width)/2, (tamPantalla.height-tamFrame.height)/2); //para posicionar 
     setVisible(true); 



    } 


    public void mostrar() 
    { 
     setVisible(true); 
    } 


    public void setText(String string) { 
     //JLabel label = new JLabel(); 
     label.setText(string); 

    } 
} 

http://imageshack.us/photo/my-images/94/45985657.jpg/

+0

你能告訴我們一個屏幕截圖什麼錯誤,因爲我不知道你在說究竟是 – 2013-04-09 22:24:25

回答

1

我趕緊在的JFrame使這個。 它適合我。

// the panel 
    JPanel buttonPanel = new JPanel(); 
    buttonPanel.setLayout(new GridBagLayout()); 
    buttonPanel.setSize(new Dimension(400, 300)); // whatever 

    // the scrollpane 
    JScrollPane pane = new JScrollPane(); 
    pane.setSize(new Dimension(400, 300)); // whatever 

    // GridBagConstraint for button 
    GridBagConstraints constraint = new GridBagConstraints(); 
    constraint.anchor = GridBagConstraints.CENTER; 
    constraint.fill = GridBagConstraints.NONE; 
    constraint.gridx = 0; 
    constraint.gridy = GridBagConstraints.RELATIVE; 
    constraint.weightx = 1.0f; 
    constraint.weighty = 1.0f; 

    int sizeOfButtons = 50; 
    for(int i = 0; i < sizeOfButtons; i++) { 
     JButton button = new JButton(); 

     button.setText("Button #" + i); 

     // other attributes you will set 

     buttonPanel.add(button, constraint); 
    } 

    pane.setViewportView(buttonPanel); 
    this.rootPane.add(pane); // or other panel etc. 
    pane.updateUI(); 
+0

工作..我要改變這一點東西..但就是這個想法,我有...謝謝..你真的幫助我,我已經在一個圈子裏試圖解決這個問題..謝謝 – user2263811 2013-04-09 23:28:29