Dam*_*mir 1 java graphics swing
如何计算px中字符串的宽度?我有面板,我尝试(JPAnel上的这个参考)
FontMetrics gp = this.getGraphics().getFontMetrics();
int x=gp.stringWidth("test");
Run Code Online (Sandbox Code Playgroud)
但是getGraphics总是返回null,因为面板还不可见.如何解决这个问题呢 ?
您应该在实例存在的其中一个paint()或多个paintComponent()方法中执行此操作Graphics.
或者你可以创建BufferedImage和调用getGraphics().
| 归档时间: |
|
| 查看次数: |
515 次 |
| 最近记录: |