是否有可能根据其字体获取字符串的宽度?
例如,如果字体大小是 40,字符串是“hi”,你可以做 2*40。
这方面的一个例子是
startX = 260; startWidth = "Start".length()*getFont().getSize();
startY = getHeight()-startWidth-20;
startHeight = getFont().getSize();
It really depends on the type of font, whether it's monospaced or proportional. Most font now are proportional so you would not simply be able to calculate num_of_chars * n.
The gui interface you are using should give you the width of the container once it has been populated with the given text.