2012-11-12 34 views
1

請看看下面的代碼製作JLabel的大小/差距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(); 
     } 
    } 

當你執行它,你可以看到比那JCheckBox的JLabel的含有較少的垂直間距。因此,「電子郵件地址」(JLabel)不會顯示在顯示「名稱」(JCheckBox)的同一行中。我怎樣才能解決這個問題?

+1

1)這看起來像表格信息。使用'JTable'。 2)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)(例如,對於佈局問題,沒有第三方API導入)。 –

+0

@AndrewThompson:進口刪除:) –

+1

..代碼編譯?我敢打賭它沒有。 –

回答

2

GridLayout有一個很好的功能,可讓您使用0來表示任意數量的行或列(但不是同時存在)。另外,

  • 指定您的Map參數:Map<String, String>

  • Set implements Iterable,其允許與Map.Entry的for-each循環。

  • Swing GUI對象應該在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。我會檢查這一點。 –

+1

我鼓勵你也去追求'JTable' [備選](http://stackoverflow.com/q/13345504/230513)。很高興有選擇! – trashgod

+0

真棒..這是一個偉大的創作。謝謝 :) –

1

你有一個有四個大單元格的GridLayout,並且你將複選框和電子郵件分別添加到不同的單元格。他們彼此不知道,只根據他們的大小進行對比,這是不同的。

要將每個複選框與每個電子郵件對齊,請將它們添加到關於其關係的佈局中。最簡單的方法是使GridLayout不是2x2,而是(N + 1)x2,然後將每個複選框和每封電子郵件添加到單獨的單元格。你也可以使用GridBagLayout,它不需要事先知道單元的數量。

+0

感謝您的回覆。我真的很感激它:) –