如何改变秋千标签的差距

时间:2012-04-14 21:59:07

标签: java swing jlabel

我创建了一些标签:

leftLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
leftLabel.setFont(new Font(FONT, Font.PLAIN, 280));
leftLabel.setBorder(BorderFactory.createTitledBorder("aaa"));
leftLabel.setText("0");

看起来像这样:enter image description here

正如你所看到的那样,上下差距很大。我怎么能减少呢?

2 个答案:

答案 0 :(得分:10)

你需要调整边框插图,

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

public final class TitledBorderDemo {
    private static void createAndShowGUI(){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(new TitledLabel(String.valueOf(0)));
        frame.pack();
        frame.setVisible(true);
    }

    private static class TitledLabel extends JLabel{
        private static final long serialVersionUID = 1L;
        private static final String TITLE = "aaa";

        TitledLabel(String text){
            super(text);
            setAlignmentX(Component.CENTER_ALIGNMENT);
            setFont(new Font("Arial", Font.PLAIN, 280));
            setBorder(new TitledBorder(TITLE){
                private static final long serialVersionUID = 1L;

                @Override
                public Insets getBorderInsets(Component c, Insets insets){
                    // arbitrary insets for top and bottom.
                    return new Insets(insets.top - 45, insets.left, insets.bottom - 55, insets.right);
            }});

        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }
}

enter image description here

希望这能让你开始朝着正确的方向前进!

答案 1 :(得分:2)

问题可能在新创建的边框的属性中。这些边界有插图。这是我能想到的影响差距的唯一方法。尝试在边框上调用一个方法,将这些插入更改为[1,1,1,1]