在JCheckBox中有一个左右对齐标签

时间:2012-12-20 08:53:55

标签: java swing jcheckbox

我将从我想要的东西开始,然后从那里开始:

+------------------------------------------+
|[] jCheckBox                       jLabel |
+------------------------------------------+

基本上我希望JCheckBox有2段文字,1条左对齐,1条右对齐。我希望整个框都可以点击,并且行为与JCheckBox完全相同(如果我按下鼠标,它会突出显示框等)。

现在我试过了:

  • 在文本框中使用HTML,其表格沿着<html><table width=100%><tr width=100%><td>Left Text</td><td align=right>Right Text</td></tr></table>行。这会产生[] Left TextRightText而没有任何视觉对齐。

  • JCheckBoxJLabel添加到面板中,但这需要我实现所有鼠标事件并在JCheckBox上调用正确的相应方法。我把它放在一边 - 但是我愿意重新考虑这是否是唯一的选择

  • 将JLabel添加到JCheckBox

    • 首先,这导致JCheckBox决定更改字体,因此必须“调整”
    • 其次,基线偏离了1或2个像素,这是通过实现将JLabel置于基线上的LayoutManager来解决的。
    • 第三,JLabelJCheckBox文字重叠,因此我要调整我的布局管理员,但JCheckBox没有从getPreferedSize()[1,1])。

所以,基本上我的问题是:我该如何实现呢?

3 个答案:

答案 0 :(得分:4)

  • 创建父JLabelJLabel尚未实现任何LayoutManager,然后设置正确LayoutManager,我认为GridLayout({{1} })

  • 添加两个with 2 segments of text, 1 left aligned and 1 right aligned并进行正确对齐(JLabels

  • JLabel.setHorizontalAlignment(javax.swing.SwingConstants.XXX);与父JCheckBoxJLabel

  • 放在一起
  • MultiIcon in the JLabel

  • 的方式相同

答案 1 :(得分:2)

这就是我设法使用JCheckBox作为父类来实现它的方法。这是缺少设置标签文本和颜色的方法。

package mock.dlect;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;

public class LeftRightCheck extends JCheckBox {

    private final static JCheckBox DEFAULT_CHECK_BOX = new JCheckBox();
    private final JLabel rightLabel;

    public LeftRightCheck(String leftText, String rightText, Icon icon,
            boolean selected) {
        super(leftText, icon, selected);
        rightLabel = new JLabel(rightText, TRAILING);
        // TODO figure out how to make the font work right on OSX
        this.setLayout(new BaseLineSingleElementLayoutManager(10));
        this.add(rightLabel);
    }

    @Override
    public final void setFont(Font font) {
        font = font.deriveFont(DEFAULT_CHECK_BOX.getFont().getSize2D());
        super.setFont(font);
        if (rightLabel != null) {
            rightLabel.setFont(font);
        }
    }

    private class BaseLineSingleElementLayoutManager implements LayoutManager {

        private int hgap;
        private Component component;

        public BaseLineSingleElementLayoutManager(int hgap) {
            this.hgap = hgap;
        }

        @Override
        public void addLayoutComponent(String name, Component comp) {
            // Do Nothing
        }

        @Override
        public void removeLayoutComponent(Component comp) {
            // Do Nothing
        }

        @Override
        public Dimension preferredLayoutSize(Container parent) {
            if (parent != null && parent instanceof JCheckBox) {
                JCheckBox cc = (JCheckBox) parent;
                // These 2 lines will add the size of the checkbox to the
                // preferred size
                Dimension parentSize = DEFAULT_CHECK_BOX.getPreferredSize();
                parentSize.width += cc.getFontMetrics(cc.getFont()).
                        stringWidth(cc.getText()) + hgap;
                if (checkComponent(cc)) {
                    Dimension childSize = this.component.getPreferredSize();
                    parentSize.height = Math.max(parentSize.height,
                            childSize.height);
                    parentSize.width += childSize.width;
                }
                return parentSize;
            }
            return new Dimension();
        }

        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return preferredLayoutSize(parent);
        }

        @Override
        public void layoutContainer(Container parent) {
            if (!checkComponent(parent)) {
                return;
            }
            this.component.setSize(parent.getSize());
            this.component.setLocation(0, getBaseline(parent)
                    - getBaseline(this.component));
        }

        private int getBaseline(Component c) {
            return c.getBaseline(c.getWidth(), c.getHeight());
        }

        protected boolean checkComponent(Container parent) {
            if (parent == null) {
                return false;
            } else if (parent.getComponentCount() >= 1
                    && parent.getComponent(0).isVisible()) {
                this.component = parent.getComponent(0);
                return true;
            } else {
                return false;
            }
        }
    }
}

答案 2 :(得分:1)

public class LeesCheckbox extends JPanel 
{
    private JCheckBox checkbox;

    public LeesCheckbox(String leftText, String rightText){
        super(new BorderLayout());
        checkbox = new JCheckBox(leftText);
        add(checkbox, BorderLayout.WEST);
        add(new JLabel(rightText), BorderLayout.EAST);
        setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                checkbox.setSelected(!checkbox.isSelected());
            }
        });
    }

    public JCheckBox getCheckbox() {
        return checkbox;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Box content = new Box(BoxLayout.Y_AXIS);
        content.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        LeesCheckbox box = new LeesCheckbox("Left text", "Right text");
        box.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
        content.add(box);
        content.add(Box.createVerticalGlue());

        frame.setContentPane(content);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}