Home > Java > javaTutorial > 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
Release: 2024-10-28 16:29:01
Original
643 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template