2013-01-11 12 views
8

私はdrawString()メソッドを使用してGraphicsを使用して文字列を描画しますが、テキストを四角形の中央に配置したいと思います。私はどうしたらいいですか?矩形内のJava中心のテキスト

+1

あなたのタイトルの状態を私は絶対的な中心位置を好むが、それはあなたが何をしているかに依存して、あなたの 'JTextField'を使用しますが、していません質問。だから本当ですか? – Mordechai

+0

あなたのタイトルは次のとおりです:*テキストフィールド*のJava中心のテキストですが、あなたの投稿はこう言っています:* Graphicsに文字列を描画するのに 'drawString()'メソッドを使用します。どのようにすればいいですか?*これはどのように対応しますか –

+0

私の質問を修正するには: 私はJFrameのどこかに長方形を持っています...私はその長方形の中心に文字列を描きたいと思います。それはどうしたらいいですか? –

答えて

27

私は、テキストは、「オプション」をたくさん持っているセンタリングの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

のテキストを縦にセンタリングするのは、テキストテキストがレンダリングされる方法を示します。メモリからは、y =((getHeight() - fm.getHeight())/ 2)+ fm.getAscent();のようになります。 ...コンポーネントの中心線を横切って線を引いて、2つの方法を比較してください。私の記憶が私によく役立っているなら、これはより適切に中心に置かれていると思います... +1 – MadProgrammer

+0

あなたは正しいです。私は答えを決めた。 –

+0

あなたはまた、[this]を読んでいることが好きかもしれません(http://stackoverflow.com/questions/1055851/how-do-you-draw-a-string-centered-vertically-in-java) – MadProgrammer

15

上のテキストを中心に、これを使用しました。あなたは絶対に、またはベースラインに基づいてセンターですか?個人的に

enter image description here

、...

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のコントラスト。 – trashgod

関連する問題