Tuesday, January 13, 2009

Change Font in Java

This is an example code how to change a font in Java application.

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.awt.Font;

public class Teks extends JFrame {
JLabel lbl1 = new JLabel(”Helvetica biasa”);
JLabel lbl2 = new JLabel(”SansSerif bold”);
JLabel lbl3 = new JLabel(”Monospaced italic”);
JLabel lbl4 = new JLabel(”Serif biasa”);

public Teks()
{
setTitle(”Font”);
setApplicationLayout();
setSize(175,200);
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String args[])
{
Teks teks = new Teks();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void setApplicationLayout()
{
setLayout(new FlowLayout());
lbl1.setFont(new Font (”Helvetica”, Font.PLAIN, 16));
lbl2.setFont(new Font (”SansSerif”, Font.BOLD, 16));
lbl3.setFont(new Font (”Monospaced”, Font.ITALIC, 16));
lbl4.setFont(new Font (”Serif”, Font.PLAIN, 24));
add(lbl1);
add(lbl2);
add(lbl3);
add(lbl4);
}
}

Best regards,

Deny Sutani

No comments:

Post a Comment