加標和字體爲String我想提請對我的JFrame有標和字體的字符串,我試圖用構造一個AttributedString但我似乎並不想爲工作無論原因。它要麼一次只顯示字體或只是標而不是兩個。圖形JFrame上
private class DrawFormulas extends JComponent
{
public void paint(Graphics g)
{
Graphics2D G2D = (Graphics2D)g;
G2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setFont(F);
AttributedString Trig = new AttributedString("a2 + b2 = c2");
Trig.addAttribute(TextAttribute.FONT, F);
Trig.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 1, 2);
Trig.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 6, 7);
Trig.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 11, 12);
Trig.addAttribute(TextAttribute.SIZE, F.getSize());
G2D.drawString(Trig.getIterator(), 170, 75);
}
}
如果有人能告訴我爲什麼這不工作或有這樣做的任何幫助的一個更好的辦法,將不勝感激。謝謝
哪個是你的字體? – Frakcool
它是在方法外定義的F Font F = new Font(「SANS_SERIF」,Font.PLAIN,60); – LoganMacMonkey
1+爲一個很好的代碼片段,但是請確保下次發佈有效的[mcve],因此我們不需要問什麼字體(或代碼中的任何數據)。我的回答顯示了一個MCVE的例子 – Frakcool