开发者

Swing: checkbox spacing as compared to labels

How do you remove/reduce the spacing between checkboxes with MigLayout?

Swing: checkbox spacing as compared to labels

import java.util.Arrays;
import javax.swing.JCheckBox开发者_高级运维;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class CheckboxSpacing {
    public static void main(String[] args) {
        JFrame frame = new JFrame("checkbox spacing");
        JPanel panel = new JPanel();
        frame.setContentPane(panel);
        panel.setLayout(new MigLayout());
        for (String verb : Arrays.asList("see","hear","speak"))
        {
            JCheckBox cb = new JCheckBox(verb+" no evil");
            panel.add(cb, "wrap");
        }

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

NOTE: I'm wondering maybe if the MigLayout part of this question is a red herring. If I change my program to the following, I get these dialogs:

Swing: checkbox spacing as compared to labels

Swing: checkbox spacing as compared to labels

You will note the difference in spacing between labels and checkboxes. How can I get rid of the excess spacing for checkboxes?

import java.awt.Component;
import java.util.Arrays;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class CheckboxSpacing {
    enum WhichGUI { LABEL {
        @Override
        public Component createComponent(String text) {
            return new JLabel(text);
        }
    }, CHECKBOX {
        @Override
        public Component createComponent(String text) {
            return new JCheckBox(text);
        }
    };
        abstract public Component createComponent(String text); 
    }
    public static void main(String[] args) {
        doit(WhichGUI.LABEL);
        doit(WhichGUI.CHECKBOX);
    }
    private static void doit(WhichGUI which) {
        JFrame frame = new JFrame("spacing: "+which);
        JPanel panel = new JPanel();
        frame.setContentPane(panel);
        panel.setLayout(new MigLayout(
                  "",  //layout
                  "[]", //column
                  "0"  //row
            ));
        for (String verb : Arrays.asList("see","hear","speak"))
        {
            Component c = which.createComponent(
                      verb+" no evil (Jabberwocky! Çgpq)");
            panel.add(c, "wrap 0");
        }

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


The difference is in the preferred size of each component.

String text = "Hear no evil";
JLabel label = new JLabel(text);
System.out.println( label.getPreferredSize() );
System.out.println( label.getInsets() );

JCheckBox checkBox = new JCheckBox(text);
System.out.println( checkBox.getPreferredSize() );
System.out.println( checkBox.getInsets() );

A check box has non-zero insets. This would be from the border. So you should be able to either:

  1. set the border of the check box to null
  2. add an EmptyBorder to the label


There are a number of ways to accomplish this and they are well specified in this document.

You can specify the spacing either between the grid or the components. You probably want to do something like this:

     new MigLayout(
              "",  //layout
              "[]", //column
              "5"  //row
        );

And that should set the gaps. You will have to play with the numbers until you get what you want.

A second option (and I think you have more control this way is to do this:

  panel.add(cb, "wrap 0");

Where the 0 specifies the gap between rows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜