Text & Fonts
Home ] Up ] JFrame Internal Structure ] Graphics Context ] Displaying Text ] [ Text & Fonts ] Mixing Fonts ] Finding & Displaying Fonts ]

 

 

Now, let's say you want to display the text in a different way -- perhaps using a particular font:

package swingExamples;

import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

class BoldTextDisplayPanel extends JPanel
{
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    Font font = new Font("Serif", Font.BOLD, 16);
    g.setFont(font);
    g.drawString("Hello from a Java Swing application!", 20, 80);
  }
}

class BoldTextDisplayFrame extends JFrame
{
  public BoldTextDisplayFrame()
  {
    setTitle("Font Display Frame");
    setSize(300, 200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contentPane = getContentPane();
    contentPane.add( new BoldTextDisplayPanel() );
  }
}

public class BoldTextDisplay
{
  public static void main(String[] args)
  {
    BoldTextDisplayFrame frame = new BoldTextDisplayFrame();
    frame.setVisible(true);
  }
}

which produces:

 

This page was last modified on 02 October, 2007