Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Main {
    public static void main(String[] args) throws Exception {
        int cp = 0;
        StyledDocument doc;
        JTextPane jta = new JTextPane();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        doc = jta.getStyledDocument();
        JScrollPane jsp = new JScrollPane(jta);
        jsp.setPreferredSize(new Dimension(400, 400));
        String[] fnt = ge.getAvailableFontFamilyNames();
        MutableAttributeSet mas = jta.getInputAttributes();
        for (int i = 0; i < fnt.length; i++) {
            StyleConstants.setBold(mas, false);
            StyleConstants.setItalic(mas, false);
            StyleConstants.setFontFamily(mas, fnt[i]);
            StyleConstants.setFontSize(mas, 16);

            doc.insertString(cp, fnt[i] + "\n", mas);

            StyleConstants.setBold(mas, true);
            doc.insertString(cp, fnt[i] + "bold \n", mas);
            StyleConstants.setItalic(mas, true);

            doc.insertString(cp, fnt[i] + "bold and italic\n", mas);
            StyleConstants.setBold(mas, false);
            doc.insertString(cp, fnt[i] + "italic\n", mas);
        }
        JFrame frm = new JFrame();
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setLayout(new BorderLayout());
        frm.add(jsp, BorderLayout.CENTER);
        frm.setLocation(100, 100);
        frm.pack();
        frm.setVisible(true);
    }
}