Mega Code Archive
Center text based on font metrics
// This example is from the book _Java AWT Reference_ by John Zukowski.
// Written by John Zukowski. Copyright (c) 1997 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Center extends JPanel {
static String text[] = new String[]{"asdf","asdfasdf"};
private Dimension dim;
public void addNotify() {
super.addNotify();
int maxWidth = 0;
FontMetrics fm = getFontMetrics(getFont());
for (int i=0;i