2012-04-14 9 views
5

puedo crear alguna etiqueta:Cómo cambiar brecha en la etiqueta de oscilación

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

que se parecen a esto: enter image description here

Como se puede ver hay gran brecha hacia arriba y abajo. ¿Cómo puedo reducirlo?

+0

¿Qué sucede cuando no eliges un tamaño de letra que puedo leer desde una milla de distancia? – MarioDS

+0

Necesito cambiar el tamaño de fuente – hudi

+0

setAlignmentY()? – Randy

Respuesta

10

Es necesario ajustar las inserciones de frontera,

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

Esperemos que esto le ayudará a comenzar en la dirección correcta!

+0

guau buen ejemplo. Esto funciona como un encanto ahora, necesito crear un borde vacío – hudi

+2

leftLabel.setBorder (nuevo EmptyBorder (nuevos Insets (-45, 0, -45, 0))); thx – hudi

+1

+1 para [sscce] (http://sscce.org/). Ver también este relacionado [ejemplo] (http://stackoverflow.com/a/4151403/230513). – trashgod

2

el problema probablemente esté dentro de las propiedades del borde recién creado. Estas fronteras tienen inserciones. Eso es lo único que se me ocurre para influir en las lagunas. Intente llamar a un método en el borde que cambie estas inserciones al [1,1,1,1].

+0

el problema no está en el borde Añado borde para mostrar el espacio en JLabel – hudi

+1

@hudi La próxima vez veré más en profundidad en mi bola de cristal. – MarioDS

Cuestiones relacionadas