2010-10-06 75 views
1

我有一個JInternalFrame,我正在應用自定義UI。 UI繪製組件,但是當我將Janel添加到JInternalFrame時,它不顯示。我認爲UI在整個組件上都很痛苦,但是如何繪製UI然後繪製組件?自定義UI繪畫問題

但是,如果任何人有更好的方式做到這一點,請讓我知道!謝謝!

public class ClassInternalFrame extends JInternalFrame { 
    public static Color currentColor; 
    public static final Color CLASS_TYPE = new Color(148, 227, 251); 

    public ClassInternalFrame(String title, Color classType) { 
     super(title, true, true, false, true); 
     currentColor = classType; 
     super.setUI(new ClassFrameUI()); 

     Container pane = super.getContentPane(); 
     pane.setLayout(new BorderLayout()); 

     JPanel titlePanel = new JPanel(); 
     titlePanel.setPreferredSize(new Dimension(0, 20)); 
     pane.add(titlePanel, BorderLayout.NORTH); 

     titlePanel.setBorder(new MatteBorder(1, 1, 1, 1, Color.yellow)); 
    } 

} 

class ClassFrameUI extends InternalFrameUI { 
    private final static ClassFrameUI frmUI = new ClassFrameUI(); 

    public static ComponentUI createUI(JComponent c) { 
     return frmUI; 
    } 

    @Override 
    public void paint(Graphics g, JComponent c) 
    { 
     Graphics2D g2d = (Graphics2D) g; 

     g2d.setColor(Color.LIGHT_GRAY); 
     g2d.fillRect(0, 0, c.getWidth(), c.getHeight()); 

     g2d.setColor(ClassInternalFrame.currentColor); 
     g2d.fillRect(0, 0, c.getWidth(), 20); 

     g2d.setColor(Color.DARK_GRAY); 
     g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 1, 0 }, 0)); 
     g2d.drawRect(0, 0, c.getWidth()-1, c.getHeight()-1); 
     g2d.drawLine(0, 20, c.getWidth(), 20); 


    } 
} 

回答

2

的問題不在於你畫過任何東西,但InternalFrameUI絕對沒有(如果沒有,你還需要調用super.paint(g, c);)。正常情況下,組件的繪製由子類如BasicInternalFrameUI完成。看起來您正在嘗試繪製自定義標題欄,BasicInternalFrameUI代表BasicInternalFrameTitleBar