我試圖創建一個單獨的CustomFont
類,其中我可以使用不同的文本屬性。於是我創建了一個新類Font
,並在裏面創建了一個私人類Drawing,它擴展了JComponent
。我改變paintComponent
方法中的字體和文字的顏色和其他特徵。更改Java中文本的顏色
問題是paintComponent
方法沒有被調用。我確信我犯了一些錯誤。
下面是代碼:
import javax.swing.JComponent;
public class CustomFont extends Font {
private String string;
private int FontStyle;
public CustomFont(String text, int style) {
super("Serif", style, 15);
FontStyle = style;
string = text;
Drawing draw = new Drawing();
draw.repaint();
}
private class Drawing extends JComponent {
public void paintComponent(Graphics g) {
Font font = new Font("Serif", Font.BOLD, 15);
g.setFont(font);
g.setColor(Color.YELLOW);
g.drawString(string, getX(), getY());
}
}
}
(無關,但考慮到使用Java命名變量的約定,比如:'FontStyle'會'fontStyle'。) –
爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 –
+1 @AndrewThompson和DaveNewton評論說,這不是一個[SSCCE](http://sscce.org),因爲我不知道你是如何使用他的課程,但看到我的答案在下面尋求幫助。 –