Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 15th, 2012  |  syntax: None  |  size: 0.57 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Why is my label not centered along the y axis? (Stanford SEE CS106A)
  2. import acm.program.*;
  3. import acm.graphics.*;
  4.  
  5. public class CenterLabel extends GraphicsProgram {
  6.     public void run() {
  7.         GLabel text = new GLabel("HELLO!");
  8.         text.setFont("Arial-200");
  9.         double cy = (getHeight() / 2) + (text.getAscent() / 2);
  10.         text.setLocation(0, cy);
  11.         add(text);
  12.     }
  13. }
  14.        
  15. text.setBounds(20,20,20,20);
  16.        
  17. double cy = (getHeight() / 2) - (text.getAscent() / 2);
  18.        
  19. public static void main(String[] args) {
  20.     new /* name of your class */.start(args);
  21. }