无法将项目添加到自定义JPanel

时间:2010-10-01 01:28:15

标签: java jpanel

我的问题是我创建了一个绘制渐变作为背景的JPanel。但是当我去添加组件时(比如JButton)它什么也没做......

请帮忙!

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class CocoaTrashBar extends JPanel {
    String titleText = "";
    Color topLineColor = new Color(128, 128, 128);
    Color bottomLineColor = new Color(69, 69, 69);
    Color gradientTop = new Color(116, 116, 116);
    Color gradientBottom = new Color(81, 81, 81);


    public CocoaTrashBar() {
        setDefaults();
    }


    public CocoaTrashBar(String title) {
        setDefaults();
        this.titleText = title;
    }


    public void setTitle(String title) {
        this.titleText = title;
        this.repaint();
    }


    private void setDefaults() {
        super.setOpaque(true);
        this.setPreferredSize(new Dimension(0, 24));
        installWindowFocusListener(new WindowFocusListener() {
            public void windowGainedFocus(WindowEvent e) {
                topLineColor = new Color(128, 128, 128);
                bottomLineColor = new Color(69, 69, 69);
                gradientTop = new Color(116, 116, 116);
                gradientBottom = new Color(81, 81, 81);
                repaintComponent();
            }
            public void windowLostFocus(WindowEvent e) {
                topLineColor = new Color(171, 171, 171);
                bottomLineColor = new Color(103, 103, 103);
                gradientTop = new Color(156, 156, 156);
                gradientBottom = new Color(121, 121, 121);
                repaintComponent();
            }
        }, this);
    }


    private static void installWindowFocusListener(
            WindowFocusListener focusListener, JComponent component) {
        component.addPropertyChangeListener("Frame.active",
                createFrameFocusPropertyChangeListener(focusListener, component));
    }


    private static PropertyChangeListener createFrameFocusPropertyChangeListener(
            final WindowFocusListener focusListener, final JComponent component) {
        return new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                Window window = SwingUtilities.getWindowAncestor(component);
                boolean hasFocus = (Boolean) component.getClientProperty("Frame.active");
                if (hasFocus) {
                    focusListener.windowGainedFocus(
                            new WindowEvent(window, WindowEvent.WINDOW_GAINED_FOCUS));
                } else {
                    focusListener.windowLostFocus(
                            new WindowEvent(window, WindowEvent.WINDOW_LOST_FOCUS));
                }
            }
        };
    }


    private void repaintComponent() {
        this.repaint();
    }


    @Override
    public void paint(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d =(Graphics2D)g;
        // Draw first line
        g2d.setPaint(topLineColor);
        g2d.drawLine(0, 0, this.getWidth(), 0);
        // Draw last line
        g2d.setPaint(bottomLineColor);
        g2d.drawLine(0, 23, this.getWidth(), 23);
        // Draw gradient
        GradientPaint gradient = new GradientPaint(
                this.getX(),
                this.getY()+1,
                gradientTop,
                this.getX(),
                this.getHeight()-1,
                gradientBottom);
        g2d.setPaint(gradient);
        g2d.fillRect(this.getX(), this.getY()+1, this.getWidth(), this.getHeight()-2);
        if(titleText != null) {
            g2d.setFont(new Font("", Font.BOLD, 13));
            g2d.setColor(Color.WHITE);
            g2d.drawString(titleText, 10, 16);
        }
        g.dispose();
    }

}

2 个答案:

答案 0 :(得分:3)

问题在于您的paint方法覆盖调用paintComponent

用这个替换你的绘画方法:

protected void paintComponent(Graphics g)
{
    if (isOpaque())
    {
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
    }
    Graphics2D g2d = (Graphics2D) g;
    // Draw first line
    g2d.setPaint(topLineColor);
    g2d.drawLine(0, 0, this.getWidth(), 0);
    // Draw last line
    g2d.setPaint(bottomLineColor);
    g2d.drawLine(0, 23, this.getWidth(), 23);
    // Draw gradient
    GradientPaint gradient = new GradientPaint(this.getX(), this.getY() + 1, gradientTop, this.getX(), this.getHeight() - 1, gradientBottom);
    g2d.setPaint(gradient);
    g2d.fillRect(this.getX(), this.getY() + 1, this.getWidth(), this.getHeight() - 2);
    if (titleText != null)
    {
        g2d.setFont(new Font("", Font.BOLD, 13));
        g2d.setColor(Color.WHITE);
        g2d.drawString(titleText, 10, 16);
    }
}

答案 1 :(得分:0)

在方法paint(Graphics g)中,您的第一行是:

super.paintComponent(g);

这将绘制您的按钮。该方法的其余部分将继续绘制该按钮:

g2d.fillRect(this.getX(), this.getY()+1, this.getWidth(), this.getHeight()-2);

因此,你看不到任何按钮。让第一行成为最后一行,你应该做得很好。