Home  >  Article  >  Java  >  How do I access and manipulate fonts, sizes, and styles in Java?

How do I access and manipulate fonts, sizes, and styles in Java?

Barbara Streisand
Barbara StreisandOriginal
2024-10-28 16:29:01484browse

How do I access and manipulate fonts, sizes, and styles in Java?

Getting Fonts, Sizes, Bold, and More in Java

Accessing predefined fonts, sizes, and colors in a Java program can be challenging. To resolve this issue, let's explore how to obtain these elements effectively.

GraphicsEnvironment

To retrieve the available fonts on the system, use the GraphicsEnvironment class. It provides the getAvailableFontFamilyNames() method, which returns a string array containing the names of all installed font families.

<code class="java">GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();</code>

Font Rendering

Once you have the font names, you can create a font object with the desired attributes. The Font constructor takes three parameters: the font name, the style, and the size. For example, to create an Arial font with a size of 12 and a bold style, use:

<code class="java">Font font = new Font("Arial", Font.BOLD, 12);</code>

Sizes and Styles

Unlike fonts, the sizes and styles can be set dynamically at runtime. They are defined as constants in the Font class, such as Font.BOLD, Font.ITALIC, and Font.PLAIN for styles, and Font.SIZE1, Font.SIZE2, and so on, for sizes.

<code class="java">font.setBold(true);
font.setSize(14);</code>

Example

The following snippet demonstrates a Java program that displays a font chooser allowing the user to select a font family, size, and color:

<code class="java">import java.awt.*;
import javax.swing.*;

public class FontDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            String[] fonts = ge.getAvailableFontFamilyNames();
            JComboBox fontChooser = new JComboBox(fonts);
            fontChooser.setRenderer(new FontCellRenderer());
            JOptionPane.showMessageDialog(null, fontChooser);
        });
    }
}

class FontCellRenderer extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(
            JList list,
            Object value,
            int index,
            boolean isSelected,
            boolean cellHasFocus) {
        JLabel label = (JLabel)super.getListCellRendererComponent(
                list,value,index,isSelected,cellHasFocus);
        Font font = new Font(value.toString(), Font.PLAIN, 20);
        label.setFont(font);
        return label;
    }
}</code>

JavaDoc

Refer to the JavaDoc for GraphicsEnvironment.getAvailableFontFamilyNames() for detailed information:

[GraphicsEnvironment.getAvailableFontFamilyNames()](https://docs.oracle.com/javase/7/docs/api/java/awt/GraphicsEnvironment.html#getAvailableFontFamilyNames())

The above is the detailed content of How do I access and manipulate fonts, sizes, and styles in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn