2016-08-30 6 views
0

가로 및 세로로 패널에 라벨을 동적으로 추가하고 싶습니다. 나는이 코드를 시도했다.패널에 버튼을 동적으로 추가하는 방법은 무엇입니까?

view image

내가 JLabel의 크기와 변화 레이블 수를 변경하지 않으 :

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++; 
    } 

하지만 나는이 이미지처럼 될 필요가있다.

+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

확실하지.

구성 요소를 동적으로 추가하면 런타임에 런타임에 추가됩니다. 버튼을 세 가지 라벨이 있습니다, 시작시

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); 
     }); 
    } 
} 

: 다음 예는 MigLayout 관리자와이 방법을 보여줍니다. 버튼의 을 클릭하면 레이아웃에 새 레이블이 추가됩니다. wrap 4 제약 조건 은 행당 4 개의 열을 만듭니다. 창은 pack() 방법으로 재구성됩니다.

스크린 샷 : 당신은 당신이 얻는 무엇을 추가 할 경우

Screenshot of the example