使JLabel大小/间隙为JCheckBox [英] Making JLabel size/gap as JCheckBox

查看:94
本文介绍了使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", "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");

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

执行时,可以看到JLabel包含与JCheckBox相比,垂直间隙较小。因此,电子邮件地址(JLabel)不会显示在显示名称(JCheckBox)的同一行中。我该如何解决这个问题?

When you execute it, you can see that JLabel contains less vertical gap as compared to JCheckBox. As a result, "Email Addresses" (JLabel) are not displayed in the same line where "Names" (JCheckBox) are displayed. How can I fix this?

推荐答案

GridLayout 有一个很好的功能,允许您使用 0 来表示任意数量的行或列(但不能同时表示两者)。此外,

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


  • 指定地图参数: Map< String,String>

Set implements Iterable ,允许使用 Map.Entry 进行for-each循环。

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

应在仅的Swing GUI对象rel =nofollow noreferrer>事件派遣线程

Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

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

这篇关于使JLabel大小/间隙为JCheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆