2016-08-30 3 views
0

パネルに水平方向と垂直方向にラベルを動的に追加したい。私はこのコードを試しました。パネルにボタンを動的に追加するにはどうすればいいですか?

GridBagConstraints labelConstraints = new GridBagConstraints(); 

     // Add buttons 
     for(int i = 0; i < indexer; i++) 
     { 
      // Label constraints 
      labelConstraints.gridx = 1; 
      labelConstraints.gridy = i; 

      // Add them to panel     
      panel.add(listOfLabels.get(i), labelConstraints); 
     } 

     // Align components top-to-bottom 
     GridBagConstraints c = new GridBagConstraints(); 
     c.gridx = 0; 
     c.gridy = indexer; 
     c.weighty = 1; 
     panel.add(new JLabel(), c); 


     indexer++; 
    } 

が、私は、この画像のようになる必要があります。

view image

私はJLabelのサイズと変更ラベルの数を変更する必要はありません。

+1

はたぶん、あなたは、人々があなたを助けることが、何を得るのですか。 – Harald

答えて

1

私はあなたが以下のコードの希望はあなたの問題を解決することができ、正しくgridxとgridyを設定しなかったことを考える:動的にここにラベルを追加することによって、意図されたものを

int rows = 5; 
    int columns = 5; 
    JLabel label; 
    pane.setLayout(new GridBagLayout()); 
    GridBagConstraints constraints= new GridBagConstraints(); 

    for(int rowIndex = 0; rowIndex < rows; rowIndex++){ 
     for(int columnIndex = 0 ; columnIndex < columns; columnIndex++){ 
      label = new JLabel(String.format("table row:%d, column:%d ", rowIndex+1,columnIndex+1)); 
      constraints.fill = GridBagConstraints.HORIZONTAL; 
      constraints.gridx = columnIndex; 
      constraints.gridy = rowIndex; 
      pane.add(label, constraints); 
     } 
    } 
2

わかりません。

私の場合、コンポーネントを動的に追加すると、実行時に ランタイムに追加されます。次の例は、 MigLayoutマネージャでこれを示しています。初めに

package com.zetcode; 

import java.awt.event.ActionEvent; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.SwingUtilities; 

import net.miginfocom.swing.MigLayout; 

/* 
Demonstrating dynamic addition of labels 
with MigLayout manager. 

Author: Jan Bodnar 
Website: zetcode.com 
*/ 
public class MigLayoutDynamicLabelsEx extends JFrame { 

    private int counter = 0; 

    public MigLayoutDynamicLabelsEx() { 

     initUI(); 
    } 

    private void initUI() { 

     MigLayout layout = new MigLayout("wrap 4"); 
     setLayout(layout); 

     JButton addBtn = new JButton("Add"); 
     addBtn.addActionListener((ActionEvent e) -> { 
      JLabel lbl = createLabel(); 
      add(lbl, "w 100lp, h 30lp"); 
      pack(); 
     }); 

     add(addBtn); 
     add(createLabel(), "w 100lp, h 30lp"); 
     add(createLabel(), "w 100lp, h 30lp"); 
     add(createLabel(), "w 100lp, h 30lp"); 

     pack(); 

     setTitle("MigLayout dynamic example"); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    private JLabel createLabel() { 

     counter++; 
     String text = "#table no." + counter; 

     JLabel lbl = new JLabel(text); 
     lbl.setBorder(BorderFactory.createEtchedBorder()); 

     return lbl; 
    } 

    public static void main(String[] args) { 

     SwingUtilities.invokeLater(() -> { 
      MigLayoutDynamicLabelsEx ex = new MigLayoutDynamicLabelsEx(); 
      ex.setVisible(true); 
     }); 
    } 
} 

、ボタンと3つのラベルがあります。ボタンの をクリックすると、レイアウトに新しいラベルが追加されます。 wrap 4制約 は、1行につき4列を作成します。ウィンドウはpack()メソッドで再編成されます。

スクリーンショット:あなたが追加した場合

Screenshot of the example

関連する問題