开发者

Problem painting JPanel gradient background with a JLabel on it

I have the following code to paint a JPanel's background:

@Override
 public void paintComponent(Graphics g) {
    UIDefaults uid = UIManager.getDefaults();
    Graphics2D g2d = (Graphics2D)g;
    Dimension d = this.getSize();

    g2d.setPaint(new GradientPaint(0, 0, uid.getColor("ToolBar.light"), 0, d.height, uid.getColor("ToolBar.shadow"), true));
    g2d.fillRect(0, 0, d.width , d.height);

    g2d.setColor(uid.getColor("ToolBar.light"));
    g2d.drawLine(0, d.height-2, d.width, d.height-2);

    g2d.setColor(uid.getColor("ToolBar.highlight"));
    g2d.drawLine(0, d.height-1, d.width, d.height-1);
 }

I have a JLabel inside this JPanel (with opaque = false) but the JLabel's background is still some kind of gray. How can I paint a gradient on this JPanel background with a transparent background for the JLabel?

Full working example below:

DemoFrame.java:

import javax.swing.UIManager;

public class DemoFrame extends javax.swing.JFrame {

    public DemoFrame() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        panelHeader = new Extras.JPanelHeader();
        labelTitle = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panelHeader.setOpaque(false);
        panelHeader.setPreferredSize(new java.awt.Dimension(172, 29));
        panelHeader.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        labelTitle.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        labelTitle.setText("Criar Novo Utilizador");
        labelTitle.setOpaque(true);
        panelHeader.add(labelTitle);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelHeader, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelHeader, javax.swing.GroupLayout.PREFERRED_SIZE, javax开发者_运维知识库.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(83, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DemoFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel labelTitle;
    private Extras.JPanelHeader panelHeader;
    // End of variables declaration

}

JPanelHeader.java:

import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

public class JPanelHeader extends JPanel {

    @Override
     public void paintComponent(Graphics g) {
        UIDefaults uid = UIManager.getDefaults();
        Graphics2D g2d = (Graphics2D)g;
        Dimension d = this.getSize();

        g2d.setPaint(new GradientPaint(0, 0, uid.getColor("ToolBar.shadow"), 0, d.height, uid.getColor("ToolBar.background"), false));
        g2d.fillRect(0, 0, d.width , d.height);

        g2d.setColor(uid.getColor("ToolBar.light"));
        g2d.drawLine(0, d.height-2, d.width, d.height-2);

        g2d.setColor(uid.getColor("ToolBar.highlight"));
        g2d.drawLine(0, d.height-1, d.width, d.height-1);
     }

}


You must be using some wierd LAF. If that is the case then that should be posted in your question. In fact a SSCCE should be posted with every question so we can see exactly what you are doing.

Using an alpha value for backgrounds can cause problems as is demonstrated by Backgrounds With Transparency.

Here is a simple SSCCE that shows you don't need to do anything special to have the label paint properly:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FaceComponent2 extends JPanel
{
    protected void paintComponent(Graphics g)
    {
        UIDefaults uid = UIManager.getDefaults();
        Graphics2D g2d = (Graphics2D)g;
        Dimension d = this.getSize();

        g2d.setPaint(new GradientPaint(0, 0, uid.getColor("ToolBar.light"),
            0, d.height, uid.getColor("ToolBar.shadow"), true));
        g2d.fillRect(0, 0, d.width , d.height);

        g2d.setColor(uid.getColor("ToolBar.light"));
        g2d.drawLine(0, d.height-2, d.width, d.height-2);

        g2d.setColor(uid.getColor("ToolBar.highlight"));
        g2d.drawLine(0, d.height-1, d.width, d.height-1);
    }


    public static void main(String args[])
        throws Exception
    {
        JComponent face = new FaceComponent2();
        face.setLayout( new GridLayout(0, 1) );
        face.setPreferredSize( new Dimension(250, 250) );
        face.add( new JLabel("Line1") );
        face.add( new JLabel("Line2") );
        face.add( new JLabel("Line3") );

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add( face );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜