2013-01-05 111 views
5

我試圖創建一個單獨的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()); 
     } 
    } 
} 
+1

(無關,但考慮到使用Java命名變量的約定,比如:'FontStyle'會'fontStyle'。) –

+1

爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

+1 @AndrewThompson和DaveNewton評論說,這不是一個[SSCCE](http://sscce.org),因爲我不知道你是如何使用他的課程,但看到我的答案在下面尋求幫助。 –

回答

3

添加到我的評論

@Override 
protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 

     Font font = new Font("Serif", Font.BOLD, 15); 
     g.setFont(font); 
     g.setColor(Color.YELLOW); 
     g.drawString(string, 0, 0); 
} 

上面的代碼注意到@Override註釋,所以我相信我重寫正確的方法。並且getX()getY()已被替換爲0,0,因爲getXgetY指的是組件的位置,但是當我們調用drawString時,我們向它提供了在容器內繪製的參數的paramteres(並且它必須在邊界/大小f當然集裝箱

2)繪製的圖形對象時,你應該重寫getPreferredSize並返回Dimension S的適合你的部件圖紙/內容或其他視覺上不會有任何東西可見的部件尺寸將是0,0:

private class Drawing extends JComponent { 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(200, 200);//you would infact caluclate text size using FontMetrics#getStringWidth(String s) 
    } 
} 

而只是建議使用一些RenderHintsGraphics2D爲好看的文字:)在這裏看到更多:

1

有很多在這個CustomFont類,沒有真正使任何意義。首先,沒有任何事情可以用Font來實現,另一個事實是你沒有真正使用任何編碼的東西。以下內容似乎更符合您所尋找的內容。

public class Drawing extends JComponent { 
    String text; 
    Font myFont; 
    Color myTextColor; 

    public Drawing(String textArg, Font f, Color textColor) { 
     myFont = f; 
     myTextColor = textColor; 
     text = textArg; 
    } 

    public void paintComponent(Graphics g) { 
     g.setFont(myFont); 
     g.setColor(myTextColor); 
     g.drawString(text, 0, getHeight()/2); 
    } 
} 

要使用,如下圖所示...

public class Test { 
    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Font font = new Font("Serif", Font.BOLD, 15); 
     String text = "blah blah blah"; 
     Color textColor = Color.YELLOW; 
     Drawing d = new Drawing(text, font, textColor); 

     frame.add(d); 
     frame.setSize(100,100); 
     frame.setVisible(true); 
    } 
} 

繪圖利用了Font的,字體不使用一個Drawing的。在Font內定義Drawing沒有意義。通過調用super.XXX實現paintComponent(..)方法,你應該和它應該是在重寫的方法,否則可能會出現異常的第一個電話

1)你不兌現油漆鏈:

+0

請你能詳細說說你在說什麼.. – Alfred

+0

那我該如何解決這個問題? – Alfred

+0

對於第一部分,請嘗試draw.setSize(int w,int h)並查看是否有幫助。您還可以通過將draw.getSize()打印到控制檯來檢查它是否具有非零大小。 對於第二部分,您不想在g.drawString()中使用getX()和getY()。 drawString已經相對於組件的位置繪製。您需要介於0和getWidth()之間的x值以及介於0和getHeight()之間的y值。 – Mike