super.addNotify(); int maxWidth = 0; FontMetrics fm = getToolkit().getFontMetrics(getFont()); for (int i=0;i<text.length;i++) { maxWidth = Math.max (maxWidth, fm.stringWidth(text[i])); } Insets inset = insets(); dim = new Dimension (maxWidth + inset.left + inset.right, text.length*fm.getHeight() + inset.top + inset.bottom); resize (dim);
if (args.length == 0) { System.err.println ("Usage: java Center <some text>"); return; } text = args; Center f = new Center(); f.show();
g.translate(insets().left, insets().top); FontMetrics fm = g.getFontMetrics(); for (int i=0;i<text.length;i++) { int x,y; x = (size().width - fm.stringWidth(text[i]))/2; y = (i+1)*fm.getHeight()-1; g.drawString (text[i], x, y); }