自定义UI绘画问题

时间:2010-10-06 20:49:45

标签: java swing paint

我有一个JInternalFrame,我正在应用自定义UI。 UI绘制组件,但是当我向JInternalFrame添加JPanel时,它不显示。我认为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);


    }
}

1 个答案:

答案 0 :(得分:2)

问题不在于你在绘制任何东西,而是InternalFrameUI绝对不做任何事情(如果确实如此,你还需要调用super.paint(g, c);)。通常,组件的绘制由诸如BasicInternalFrameUI的子类完成。看起来您正在尝试绘制自定义标题栏,BasicInternalFrameUI委托给BasicInternalFrameTitleBar的任务。