I am using java to draw some text, but it is hard for me to calculate the string s width. for example: zheng中国... How long will this string occupy?
如何计算体积?
原标题:How to calculate the font s width?
最佳回答
就单一体而言,你可以获取特定线体的测量值,并使用计算体积。 例如:
String message = new String("Hello, StackOverflow!");
Font defaultFont = new Font("Helvetica", Font.PLAIN, 12);
FontMetrics fontMetrics = new FontMetrics(defaultFont);
//...
int width = fontMetrics.stringWidth(message);
如果你有更为复杂的文字布局要求,例如在特定的宽度内分发一段案文,则你可创建rel=“noreferer”>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);
问题回答
在这里,可以简单地显示,在测试“强食”时,如何使用“光纤”系统:
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);
}
}
审视这一伟大的介绍,特别是“衡量尺度”。 它解释了现有规模及其用途:。
http://www.oracle.com/technetwork/java/index-137037.html#Q_What_is_the_difference_between”rel=“nofollow noreferer” 合乎逻辑的、视觉的和像样的区别是什么?
a. 在以下类别中采用先天办法:
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();
}
}
你们可以从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();
相关问题
热门标签
- winforms
- combobox
- fogbugz
- java
- date
- internationalization
- asp.net
- iis
- url-rewriting
- urlrewriter
- c#
- enums
- ocaml
- haxe
- algorithm
- string
- viewstate
- .net
- c++
- c
- symbol-table
- mysql
- database
- postgresql
- licensing
- migration
- vb.net
- vb6
- declaration
- vb6-migration
- python
- psycopg2
- backup
- vmware
- virtualization
- gnu-screen
- authentication
- desktop
- excel
- xll
- cultureinfo
- regioninfo
- oracle
- client
- session
- download
- html
- virtual
- constructor
- scenarios
- perl
- full-text-search
- javascript
- ajax
- testing
- oop
- inheritance
- vim
- encapsulation
- information-hiding