Making JLabel size/gap as JCheckBox

后端 未结 2 658
难免孤独
难免孤独 2021-01-25 07:04

Please have a look at the following code

import java.awt.GridLayout;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swin         


        
相关标签:
2条回答
  • 2021-01-25 07:26

    You have a GridLayout with four large cells, and you're adding checkboxes and emails to different cells separately. They don't know of each other and are aligned based only on their size, which is different.

    To align each checkbox to each email, add them to the layout with regard to their relationship. The simplest way is to make a GridLayout not 2x2 but (N+1)x2, and then add each checkbox and each email to a separate cell. You can also use GridBagLayout, it doesn't require to know the number of cells in advance.

    0 讨论(0)
  • 2021-01-25 07:35

    GridLayout has a nice feature that lets you use 0 to signify any number of rows or columns (but not both at once). Also,

    • Specify your Map parameters: Map<String, String>.

    • Set implements Iterable, which permits a for-each loop with Map.Entry.

    • Swing GUI objects should be constructed and manipulated only on the 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", "yy@yahoo.com");
            mMap.put("Gihan", "gihan@yahoo.com");
            mMap.put("Sumi", "sumi@yahoo.com");
            mMap.put("mac", "mac@yahoo.com");
            mMap.put("Jay", "jay@yahoo.com");
            mMap.put("Rom", "rom@yahoo.com");
            mMap.put("shane", "shane@yahoo.com");
            mMap.put("Mafe", "mafe@yahoo.com");
            mMap.put("willi", "willi@yahoo.com");
            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();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题