Java Jpanels和Gradient背景

时间:2015-08-19 22:40:01

标签: java swing jframe jpanel gradient

我是初学者,试图创建一个GUI。我有4个基本类(见下文):

  • GuiApp.java - 使用main方法,调用MainFrame类
  • MainFrame.Java - 扩展JFrame - 添加MainPpanel
  • MainPanel.Java - 扩展JPanel - 添加面板
  • InnerPanel.Java - 扩展JPanel - 显示JLabel

我找到了以下代码来添加渐变背景:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame();
            TestPanel panel = new TestPanel();
            frame.add(panel);
            frame.setSize(200, 200);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });
}

当添加到InnerPanel.java时,这是有效的,但是当添加到MainPanel.Java时没有

没有错误,我只是得到默认的灰色,注意:InnerPanel.Java设置为透明。

那怎么解决这个问题呢?当然,我可以将渐变背景添加到主面板并在其上添加透明面板吗?

我的4个课程:

public class GuiApp {
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new MainFrame();
            }
        });
    }
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
class MainFrame extends JFrame {
    private MainPanel mainPanel;
    public MainFrame() {
        super("GuiApp");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setSize(300, 500);
        // setBackground(new Color(95, 158, 160));
        setLayout(new BorderLayout());

        mainPanel = new MainPanel();
        add(mainPanel, BorderLayout.CENTER);
    }
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
public class MainPanel extends JPanel {
    private InnerPanel panel;
    public MainPanel() {
        setLayout(new BorderLayout());
        panel = new InnerPanel();
        add(panel, BorderLayout.CENTER);
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        int w = getWidth();
        int h = getHeight();
        Color color1 = Color.RED;
        Color color2 = Color.GREEN;
        GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
    }
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
public class InnerPanel extends JPanel {
    public InnerPanel() {
        setBackground(new Color(0, 0, 0, 0));
        JLabel label = new JLabel("testing");
        add(label);
   }

}

1 个答案:

答案 0 :(得分:1)

所以,你的主要问题在于:

setBackground(new Color(0, 0, 0, 0));

Swing只知道如何绘制完全透明或完全不透明的组件,这是通过opaque属性进行控制。通过提供基于alpha的颜色,Swing不知道当由于某种原因更新时,它想要绘制出现在该组件下面的组件

相反,您应该使用setOpaque并传递false

InnerPanel

import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class InnerPanel extends JPanel {

    public InnerPanel() {
        setOpaque(false);
        // To prove the point
        setBackground(Color.RED);
        JLabel label = new JLabel("testing");
        add(label);
    }
}

MainPanel

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JPanel;

public class MainPanel extends JPanel {

    private InnerPanel panel;

    public MainPanel() {
        setLayout(new BorderLayout());
        panel = new InnerPanel();
        add(panel, BorderLayout.CENTER);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        int w = getWidth();
        int h = getHeight();
        Color color1 = Color.RED;
        Color color2 = Color.GREEN;
        GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
    }
}

GuiApp

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class GuiApp {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MainPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

哪,放在一起产生......

Gradient Fill