Example usage for javax.swing JLabel getFont

List of usage examples for javax.swing JLabel getFont

Introduction

In this page you can find the example usage for javax.swing JLabel getFont.

Prototype

@Transient
public Font getFont() 

Source Link

Document

Gets the font of this component.

Usage

From source file:Main.java

public static void main(String[] args) {
    int BTN_COUNT = 3;
    int VERT_GAP = 10;
    int EB_GAP = 5;
    float TITLE_SIZE = 36f;
    String TITLE_TEXT = "This is my Title";

    JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
    titleLabel.setFont(titleLabel.getFont().deriveFont(TITLE_SIZE));
    JPanel titlePanel = new JPanel();
    titlePanel.add(titleLabel);/*from   w w  w  .j  a v  a2  s .  c o  m*/

    JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
    for (int i = 0; i < BTN_COUNT; i++) {
        JButton btn = new JButton("Button " + (i + 1));
        buttonPanel.add(btn);
    }

    JTextArea textArea = new JTextArea(20, 30);

    JPanel mainPanel = new JPanel();
    mainPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
    mainPanel.add(titlePanel);
    mainPanel.add(Box.createVerticalStrut(VERT_GAP));
    mainPanel.add(buttonPanel);
    mainPanel.add(Box.createVerticalStrut(VERT_GAP));
    mainPanel.add(new JScrollPane(textArea));

    JFrame frame = new JFrame();
    frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(300, 300);/* www. j a v a2s  . co m*/

    JPanel panel = new JPanel(new GridLayout(3, 1));
    JLabel label = new JLabel();
    JTextField tf = new JTextField();
    JButton b = new JButton("calc sting width");
    b.addActionListener(e -> {
        FontMetrics fm = label.getFontMetrics(label.getFont());
        String text = tf.getText();
        int textWidth = fm.stringWidth(text);
        label.setText("text width for \"" + text + "\": " + textWidth);
    });
    panel.add(label);
    panel.add(tf);
    panel.add(b);
    frame.setContentPane(panel);
    frame.setVisible(true);
}

From source file:Main.java

public static void boldLabel(JLabel label) {
    label.setFont(label.getFont().deriveFont(Font.BOLD));
}

From source file:Main.java

public static void plainLabel(JLabel label) {
    label.setFont(label.getFont().deriveFont(Font.PLAIN));
}

From source file:Main.java

public static void italicLabel(JLabel label) {
    label.setFont(label.getFont().deriveFont(Font.ITALIC));
}

From source file:Main.java

public static void italicBoldLabel(JLabel label) {
    label.setFont(label.getFont().deriveFont(Font.BOLD + Font.ITALIC));
}

From source file:Main.java

private static JPanel createLogin() {
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    JLabel label = new JLabel("login panel.");
    label.setFont(label.getFont().deriveFont(Font.ITALIC, 24f));
    label.setAlignmentX(0.5f);//w  w  w.j a  va 2 s. c  o  m
    label.setBorder(new EmptyBorder(0, 20, 0, 20));
    p.add(Box.createVerticalStrut(36));
    p.add(label);
    p.add(Box.createVerticalStrut(144));
    return p;
}

From source file:Main.java

public static void changeFont(JLabel label, float sizeMultipler) {
    float size = label.getFont().getSize() * sizeMultipler;
    label.setFont(label.getFont().deriveFont(size));
}

From source file:Main.java

public static void changeFontSize(JLabel label, int newSize) {
    label.setFont(label.getFont().deriveFont((float) newSize));
}

From source file:Main.java

public static JLabel modifyLabelFont(JLabel label, int style, int delta) {
    Font font = label.getFont();
    label.setFont(font.deriveFont(style, font.getSize() + delta));
    label.setForeground(new Color(140, 140, 140));
    return label;
}