我创建了一些标签:
leftLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
leftLabel.setFont(new Font(FONT, Font.PLAIN, 280));
leftLabel.setBorder(BorderFactory.createTitledBorder("aaa"));
leftLabel.setText("0");
看起来像这样:
正如你所看到的,上下有很大的差距。我怎样才能减少它?
您需要调整边框插图,
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();
}
});
}
}
希望这能让您朝着正确的方向开始!
问题可能出在新创建的边框的属性内。这些边框有插图。这是我能想到的唯一能影响差距的事情。尝试在边框上调用一个方法,将这些插图更改为[1,1,1,1]
.