Pregunta

¿Cómo hago para establecer la fuente predeterminada para todas las instancias JLabel. En lugar de establecer el tipo de letra para cada JLabel forma independiente.

¿Fue útil?

Solución

UIManager para definir por defecto de JLabel fuente:

import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;

public class LabelFont {

   public static void main(String[] args) {
      Font oldLabelFont = UIManager.getFont("Label.font");
      UIManager.put("Label.font", oldLabelFont.deriveFont(Font.PLAIN));

      JFrame f = new JFrame("LabelFont Test");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.getContentPane().setLayout(new FlowLayout());

      JLabel df = new JLabel("Default JLabel font");
      f.getContentPane().add(df);

      JLabel ef = new JLabel("Font explicitly set");
      ef.setFont(oldLabelFont);
      f.getContentPane().add(ef);

      f.pack();
      f.setVisible(true);
   }
}

Vía: http: // coding.derkeiler.com/Archive/Java/comp.lang.java.help/2005-04/msg00395.html

Otros consejos

¿Es esto lo que busca?

import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;

public class LabelFont {

   public static void main(String[] args) {
      Font oldLabelFont = UIManager.getFont("Label.font");
      UIManager.put("Label.font", oldLabelFont.deriveFont(Font.PLAIN));

      JFrame f = new JFrame("LabelFont Test");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.getContentPane().setLayout(new FlowLayout());

      JLabel df = new JLabel("Default JLabel font");
      f.getContentPane().add(df);

      JLabel ef = new JLabel("Font explicitly set");
      ef.setFont(oldLabelFont);
      f.getContentPane().add(ef);

      f.pack();
      f.setVisible(true);
   }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top