2009-10-06 94 views
11

我正在使用java來繪製一些文本,但我很難計算字符串的寬度。 例如: zheng中國... 這個字符串佔用多長時間?如何計算字體寬度?

+0

可能的複製[在Java中計算字符串的顯示寬度](http://stackoverflow.com/questions/258486/calculate-the-display-width-of-a-string-in-java) – Zich 2017-04-09 13:11:41

回答

33

對於單個字符串,您可以獲取給定圖形字體的度量標準,並使用它來計算字符串大小。例如:

String  message = new String("Hello, StackOverflow!"); 
Font  defaultFont = new Font("Helvetica", Font.PLAIN, 12); 
FontMetrics fontMetrics = new FontMetrics(defaultFont); 
//... 
int width = fontMetrics.stringWidth(message); 

如果具有更復雜的文本佈局要求,如給定寬度內流動的一段文字,則可以創建一個java.awt.font.TextLayout對象,如本實施例中(從文檔):

Graphics2D g = ...; 
Point2D loc = ...; 
Font font = Font.getFont("Helvetica-bold-italic"); 
FontRenderContext frc = g.getFontRenderContext(); 
TextLayout layout = new TextLayout("This is a string", font, frc); 
layout.draw(g, (float)loc.getX(), (float)loc.getY()); 

Rectangle2D bounds = layout.getBounds(); 
bounds.setRect(bounds.getX()+loc.getX(), 
       bounds.getY()+loc.getY(), 
       bounds.getWidth(), 
       bounds.getHeight()); 
g.draw(bounds); 
+17

Can not instan指定FontMetrics類型。 – 2011-10-19 08:47:07

+3

使用:FontMetrics fontMetrics = g.getFontMetrics(); – 2011-10-20 17:37:17

+0

java.awt.FontMetrics是抽象的;不能實例化 FontMetrics metrics = new FontMetrics(font); 我在其他地方發現你必須在(字體)和分號之前放置一對空的大括號。我不知道爲什麼。 FontMetrics指標=新的FontMetrics(字體){}; – 2013-08-26 16:40:58

3

這裏是一個簡單的應用程序,可以顯示你測試字符串的寬度時,如何使用的FontMetrics:

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class GUITest { 
    JFrame frame; 
    public static void main(String[] args){ 
     new GUITest(); 
    } 
    public GUITest() { 
     frame = new JFrame("test"); 
     frame.setSize(300,300); 
     addStuffToFrame(); 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run() { 
       frame.setVisible(true); 
      } 
     }); 
    }   

    private void addStuffToFrame() {  
     JPanel panel = new JPanel(new GridLayout(3,1)); 
     final JLabel label = new JLabel(); 
     final JTextField tf = new JTextField(); 
     JButton b = new JButton("calc sting width"); 
     b.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       FontMetrics fm = label.getFontMetrics(label.getFont()); 
       String text = tf.getText(); 
       int textWidth = fm.stringWidth(text); 
       label.setText("text width for \""+text+"\": " +textWidth); 
      } 
     }); 
     panel.add(label); 
     panel.add(tf); 
     panel.add(b); 
     frame.setContentPane(panel); 
    } 


} 
0

使用方法的getWidth更多的一些信息:

import java.awt.*; 
import java.awt.geom.*; 
import java.awt.font.*; 

class StringMetrics { 

    Font font; 
    FontRenderContext context; 

    public StringMetrics(Graphics2D g2) { 

    font = g2.getFont(); 
    context = g2.getFontRenderContext(); 
    } 

    Rectangle2D getBounds(String message) { 

    return font.getStringBounds(message, context); 
    } 

    double getWidth(String message) { 

    Rectangle2D bounds = getBounds(message); 
    return bounds.getWidth(); 
    } 

    double getHeight(String message) { 

    Rectangle2D bounds = getBounds(message); 
    return bounds.getHeight(); 
    } 

} 
0

您可以從Font.getStringBounds找到它()的

String string = "Hello World"; 
// Passing or initializing an instance of Font. 
Font font = ...; 
int width = (int) font.getStringBounds(string, new FontRenderContext(font.getTransform(), false, false)).getBounds().getWidth(); 
+2

請解釋你的解決方案 – Jens 2015-04-02 05:48:39