2016-08-30 2 views
0

Ich möchte Label dynamisch in Panel mit horizontal und vertikal hinzufügen. Ich habe diesen Code ausprobiert.Wie man Knopf im Panel dynamisch hinzufügt?

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

aber ich brauche wie dieses Bild ergeben:

view image

Ich will nicht eine Größe von JLabel und Änderungskennzeichen Zahl ändern.

+1

Vielleicht was, wenn Sie hinzufügen, bekommen Sie können die Menschen Ihnen helfen. – Harald

Antwort

1

Ich denke, dass Sie nicht die gridx und gridy richtig eingestellt hat, die folgenden Codes Hoffnung Ihr Problem lösen können:

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

nicht sicher, was durch Hinzufügen eines Etiketts hier dynamisch gemeint war.

Für mich dynamische Hinzufügen einer Komponente es bei Laufzeit hinzufügt. Das folgende Beispiel zeigt dies mit dem MigLayout Manager:

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

Zu Beginn gibt es Taste und drei Etiketten. Wenn Sie auf die Schaltfläche klicken, wird dem Layout eine neue Beschriftung hinzugefügt. Die wrap 4 Einschränkung erstellt 4 Spalten pro Zeile. Das Fenster wird mit der Methode pack() reorganisiert.

Screenshot:

Screenshot of the example

Verwandte Themen