2013-01-11 17 views
8

Używam metody drawString() do rysowania ciągów przy użyciu grafiki, ale chcę wycentrować tekst w prostokącie. Jak mam to zrobić?Tekst w środku Java w prostokącie

+1

Twoje tytuł członkowskich, używasz 'JTextField', ale nie swoimi pytanie. Więc co jest prawdą? – Mordechai

+0

Twój tytuł to: * Tekst środkowy Java w polu tekstowym *, ale twój post mówi: * Używam metody 'drawString()' do rysowania napisów w Grafice, ale chcę wycentrować mój tekst w prostokącie. Jak mam to zrobić? * Jak to się dzieje? –

+0

Aby poprawić moje pytanie: Mam gdzieś w ramce JFrame .... Chcę rysować ciąg wyśrodkowany w tym prostokącie. Jak mam to zrobić? –

Odpowiedz

27

Używałem tego do centrum tekst na JPanel

 Graphics2D g2d = (Graphics2D) g; 
     FontMetrics fm = g2d.getFontMetrics(); 
     Rectangle2D r = fm.getStringBounds(stringTime, g2d); 
     int x = (this.getWidth() - (int) r.getWidth())/2; 
     int y = (this.getHeight() - (int) r.getHeight())/2 + fm.getAscent(); 
     g.drawString(stringTime, x, y); 
+2

Centrowanie tekstu w pionie jest nieco bardziej zaangażowane, z powodu tekstu wejście i sposób renderowania tekstu. Z pamięci powinno być bardziej podobne do y = ((getHeight() - fm.getHeight())/2) + fm.getAscent(); ... Spróbuj narysować linię wzdłuż linii środkowej komponentów i porównaj obie metody, jeśli moja pamięć dobrze mi służy, wierzę, że to będzie lepiej wyśrodkowane ... +1 – MadProgrammer

+0

Masz rację. Poprawiłem odpowiedź. –

+0

Możesz także chcieć przeczytać [to] (http://stackoverflow.com/questions/1055851/how-do-you-draw-a-string-centered-vertically-in-java) – MadProgrammer

15

Centrowanie tekstu ma dużo „Opcje”. Czy koncentrujesz się całkowicie lub na podstawie linii bazowej ??

enter image description here

Osobiście wolę absolutną pozycję środkową, ale to zależy od tego, co robisz ...

public class CenterText { 

    public static void main(String[] args) { 
     new CenterText(); 
    } 

    public CenterText() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    public class TestPane extends JPanel { 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      int height = getHeight(); 
      String text = "This is a test xyx"; 

      g.setColor(Color.RED); 
      g.drawLine(0, height/2, getWidth(), height/2); 

      FontMetrics fm = g.getFontMetrics(); 
      int totalWidth = (fm.stringWidth(text) * 2) + 4; 

      // Baseline 
      int x = (getWidth() - totalWidth)/2; 
      int y = (getHeight() - fm.getHeight())/2; 
      g.setColor(Color.BLACK); 

      g.drawString(text, x, y + ((fm.getDescent() + fm.getAscent())/2)); 

      // Absolute... 
      x += fm.stringWidth(text) + 2; 
      y = ((getHeight() - fm.getHeight())/2) + fm.getAscent(); 
      g.drawString(text, x, y); 

     } 

    } 

} 
+0

+1 dla kontrastu. – trashgod