如何使JPanel的背景渐变 [英] How to make the background gradient of a JPanel

查看:218
本文介绍了如何使JPanel的背景渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何制作另一个JPanel中的背景渐变.互联网上有很多文章,但所有文章都演示了如何覆盖JPanel的paintComponent()而不是如何对其内部的jPanel进行处理.
我使用Netbeans IDE.我创建了一个新的JPanel类,并且可以覆盖其paintComponent().我上面还有另一个jpanel(拖放到父级JPanel上).我要使其背景渐变.

I want to know how to make background gradient which is in another JPanel. Many articles found in internet,but all of them had demostrated how to overide the paintComponent() of the JPanel not how to do for a jPanel which is inside it.
I use Netbeans IDE. I created a new JPanel class and could overide its paintComponent(). I have another jpanel on it (dragged & dropped on to the parent JPanel). I want to make its background gradient.

这是我为父母尝试的方式.有效.如何为子级jpanel设置此名称?

Here is how I tried for parent. It worked. How can I overide this for child jpanel ?

public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        Color color1 = getBackground();
        Color color2 = color1.darker();
        int w = getWidth();
        int h = getHeight(); 
        GradientPaint gp = new GradientPaint(
                0, 0, color1,
                0, h, color2);

        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
        }

推荐答案

如果小心调用super.paintComponent(g),则可以将渐变直接添加到面板中,如下所示.

If you are careful to invoke super.paintComponent(g), you can add the gradient directly to the panel as shown below.

出于可用性考虑,我拒绝尝试使各个组件透明的诱惑.另请注意,不透明度由外观控制&感觉.

For usability, I would resist the temptation to try making the individual components transparent. Note also that opacity is controlled by the Look & Feel.

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
* @see http://stackoverflow.com/q/12220853/230513
*/
public class GradientPanel extends JPanel {

    private static final int N = 32;

    public GradientPanel() {
        this.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
        this.add(new JLabel("Test:", JLabel.CENTER));
        this.add(new JTextField("This is a test."));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        Color color1 = getBackground();
        Color color2 = color1.darker();
        int w = getWidth();
        int h = getHeight();
        GradientPaint gp = new GradientPaint(
            0, 0, color1, 0, h, color2);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
    }

    private void display() {
        JFrame f = new JFrame("GradientPanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

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

            @Override
            public void run() {
                new GradientPanel().display();
            }
        });
    }
}

这篇关于如何使JPanel的背景渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆