2012-11-12 1 views
1

S'il vous plaît jeter un oeil sur le code suivantFaire taille JLabel/gap comme JCheckBox

import java.awt.GridLayout; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Map; 
import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 

public class TestSend extends JFrame 
    { 
     private Box names, emails; 
     private JButton ok; 
     private Map mMap; 
     private JLabel nameLabel, emailLabel; 
     private JPanel mainPanel; 
     private JScrollPane scroll; 


     public TestSend() 
     { 
      names = new Box(BoxLayout.Y_AXIS); 
      emails = new Box(BoxLayout.Y_AXIS); 

      nameLabel = new JLabel("Names"); 
      emailLabel = new JLabel("Email"); 

      mainPanel = new JPanel(); 
      mainPanel.setLayout(new GridLayout(2,2)); 

      scroll = new JScrollPane(mainPanel); 

      mainPanel.add(nameLabel); 
      mainPanel.add(emailLabel); 
      mainPanel.add(names); 
      mainPanel.add(emails); 

      mMap = new HashMap(); 

      mMap.put("yohan", "[email protected]"); 
      mMap.put("Gihan", "[email protected]"); 
      mMap.put("Sumi", "[email protected]"); 
      mMap.put("mac", "[email protected]"); 
      mMap.put("Jay", "[email protected]"); 
      mMap.put("Rom", "[email protected]"); 
      mMap.put("shane", "[email protected]"); 
      mMap.put("Mafe", "[email protected]"); 
      mMap.put("willi", "[email protected]"); 

      Iterator iter = mMap.entrySet().iterator(); 



      while(iter.hasNext()) 
      { 
       Map.Entry mEntry = (Map.Entry)iter.next(); 

       JCheckBox cBox = new JCheckBox((String)mEntry.getKey()); 

       names.add(cBox); 

       if((String)mEntry.getValue() != null && ((String)mEntry.getValue()).length() != 0 && !((String)mEntry.getValue()).equals("")) 
       { 
        JLabel lLabel = new JLabel((String)mEntry.getValue()); 
        // lLabel.setPreferredSize(new Dimension(cBox.getPreferredSize().width,cBox.getPreferredSize().height)); 
        emails.add(lLabel); 
        emails.add(new JPanel()); 

       } 
       else 
       { 
        JLabel lLabel = new JLabel(); 
        //lLabel.setPreferredSize(new Dimension(cBox.getPreferredSize().width,cBox.getPreferredSize().height)); 
        emails.add(lLabel); 
        emails.add(new JPanel()); 
       } 

      } 


      this.add(scroll); 
      this.pack(); 
      this.setVisible(true); 

     } 

     public static void main(String[]args) 
     { 
      new TestSend(); 
     } 
    } 

Lorsque vous exécutez, vous pouvez voir que JLabel contient moins écart vertical par rapport à JCheckBox. Par conséquent, "Email Addresses" (JLabel) ne sont pas affichés sur la même ligne où "Names" (JCheckBox) sont affichés. Comment puis-je réparer cela?

+1

1) Cela ressemble à des informations sous forme de tableau. Utilisez un 'JTable'. 2) Pour une meilleure assistance plus tôt, publiez un [SSCCE] (http://sscce.org/) (par exemple, aucune importation d'API de 3ème partie pour un problème de mise en page). –

+0

@AndrewThompson: Importations supprimées :) –

+1

..Ce code est-il compilé? Je parie que non. –

Répondre

2

GridLayout a une belle fonctionnalité qui vous permet d'utiliser 0 pour indiquer n'importe quel nombre de lignes ou de colonnes (mais pas les deux à la fois). En outre,

  • Précisez votre Map paramètres: Map<String, String>.

  • SetSet implémente Iterable, ce qui permet une boucle for-each avec Map.Entry.

  • Les objets GUI Swing doivent être construits et manipulés seulement sur le event dispatch thread.

image

import java.awt.EventQueue; 
import java.awt.GridLayout; 
import java.util.HashMap; 
import java.util.Map; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 

public class TestSend extends JFrame { 

    private JPanel names = new JPanel(new GridLayout(0, 1)); 
    private Map<String, String> mMap = new HashMap<String, String>(); 

    public TestSend() { 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 

     JPanel top = new JPanel(new GridLayout(1, 0)); 
     top.add(new JLabel("Names")); 
     top.add(new JLabel("Email")); 
     names.add(top); 

     mMap.put("yohan", "[email protected]"); 
     mMap.put("Gihan", "[email protected]"); 
     mMap.put("Sumi", "[email protected]"); 
     mMap.put("mac", "[email protected]"); 
     mMap.put("Jay", "[email protected]"); 
     mMap.put("Rom", "[email protected]"); 
     mMap.put("shane", "[email protected]"); 
     mMap.put("Mafe", "[email protected]"); 
     mMap.put("willi", "[email protected]"); 
     for (Map.Entry<String, String> e : mMap.entrySet()) { 
      names.add(createPanel(e.getKey(), e.getValue())); 
     } 

     this.add(new JScrollPane(names)); 
     this.pack(); 
     this.setVisible(true); 
    } 

    private static JPanel createPanel(String s1, String s2) { 
     JPanel panel = new JPanel(new GridLayout(1, 0)); 
     panel.add(new JCheckBox(s1)); 
     panel.add(new JLabel(s2)); 
     return panel; 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new TestSend(); 
      } 
     }); 
    } 
} 
+1

intesesting. Je vais vérifier ça. –

+1

Je vous encourage à poursuivre le 'JTable' [alternative] (http://stackoverflow.com/q/13345504/230513), aussi. C'est bon d'avoir des choix! – trashgod

+0

génial .. C'est une excellente création. Merci :) –

1

Vous avez un GridLayout avec quatre grandes cellules, et vous ajoutez des cases à cocher et des courriels à différentes cellules séparément. Ils ne se connaissent pas et sont alignés uniquement sur leur taille, ce qui est différent.

Pour aligner chaque case à cocher sur chaque e-mail, ajoutez-les à la mise en page en fonction de leur relation. Le plus simple est de faire un GridLayout non 2x2 mais (N + 1) x2, puis d'ajouter chaque case à cocher et chaque email dans une cellule séparée. Vous pouvez également utiliser GridBagLayout, il ne nécessite pas de connaître le nombre de cellules à l'avance.

+0

Merci pour la réponse. Je l'apprécie vraiment :) –

Questions connexes