문제

How would I go about setting the default font for all JLabel instances. Instead of setting the font for each JLabel independently.

도움이 되었습니까?

해결책

Use UIManager to define JLabel's default font:

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);
   }
}

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

다른 팁

Is this what you are looking for?

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);
   }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top