0

在这个例子中如何使 JPanel 透明?渐变背景不可见:

package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

public class PaintJPanelOnJWindow extends JWindow {

    public PaintJPanelOnJWindow() {

        JPanel panel = new JPanel();

        panel.setPreferredSize(new Dimension(350, 120));
        panel.setMinimumSize(new Dimension(350, 120));
        panel.setMaximumSize(new Dimension(350, 120));

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setOpaque(false);

        JLabel someText = new JLabel("I'm not transparent and my JPanel too :(");
        someText.setOpaque(false);
        panel.add(someText);
        add(panel);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D) g.create();
        try {
            int w = getWidth(), h = getHeight();
            g2d.setPaint(new GradientPaint(0, 0, Color.RED, 0, h, Color.WHITE));
            g2d.fillRect(0, 0, w, h);
        } finally {
            g2d.dispose();
        }
        super.paint(g);
    }
}
4

2 回答 2

2

来自JavaDocs

create()创建一个新的 Graphics 对象,该对象是此 Graphics 对象的副本。

这意味着Graphics对象g2d不是指JWindow Graphics对象,而是指对象的副本JWindow Graphics

你需要改变

Graphics2D g2d = (Graphics2D) g.create(); //creates a copy, wrong object

Graphics2D g2d = (Graphics2D) g;          //refers to the right Graphics object

更新

但是,这不是正确的方法。您应该重写JPanel'spaintComponent方法,而不是破坏窗口的绘制过程。首先,paint()从您的类中删除方法覆盖。然后,初始化JPanel如下:

JPanel panel = new JPanel() {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        int w = getWidth(), h = getHeight();
        g2d.setPaint(new GradientPaint(0, 0, Color.RED, 0, h, Color.WHITE));
        g2d.fillRect(0, 0, w, h);
    }
};
于 2013-08-15T22:17:21.790 回答
2

眼前的问题是

super.paint(g);

在方法中的自定义绘画代码之后被调用,paint这将导致任何先前的绘画丢失。调用panel.setOpaque(false)paint方法中的操作没有影响。调用setOpaque问题中的任何组件是不必要的 - 默认情况下,当正确实现自定义绘画时会显示背景。

这应该通过覆盖paintComponent方法来完成。这意味着创建一个新JPanel的并将自定义绘画功能放置在那里,而不是放在顶级容器中,例如JWindow.

例子:

在此处输入图像描述

public class PaintJPanelApp {


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new JFrame("Gradient App");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setUndecorated(true);
                frame.setLocationRelativeTo(null);

                JLabel someText = new JLabel("I AM transparent and my JPanel too :)");
                GradientPanel gradientPanel = new GradientPanel();
                gradientPanel.add(someText);
                frame.add(gradientPanel);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    static class GradientPanel extends JPanel {


        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int w = getWidth(); 
            int h = getHeight();
            Graphics2D g2d = (Graphics2D) g;
            g2d.setPaint(new GradientPaint(0, 0, Color.RED, 0, h, Color.WHITE));
            g2d.fillRect(0, 0, w, h);
        }
    }
}
于 2013-08-15T22:32:58.687 回答