如何为表单设置渐变背景。 Intellij

How to set a gradient background to the form. Intellij

我是 Intellij Idea GUI 表单的初学者。而且我不知道如何为表单设置渐变背景。有人能帮我吗?谢谢

My layout

这是我当前的布局。我想使用渐变背景而不是蓝色,我该怎么做?

先把JFrame的背景设为透明

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

创建渐变颜料,并填充面板。

JPanel panel = new javax.swing.JPanel() {
    protected void paintComponent(Graphics g) {
        Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), getWidth(), getHeight(), new Color(R, G, B, 255), true);

        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(p);
        g2d.fillRect(0, 0, getWidth(), getHeight());
    }
}

将面板作为内容窗格分配给框架。

frame.setContentPane(panel);

例子

请参阅下面的 window 和 gradient-based 半透明的示例。

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Paint;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;

public class Tester {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
   
        JFrame.setDefaultLookAndFeelDecorated(true);
    
        // Create the GUI on the event-dispatching thread
        SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            createWindow();                      
        }
    });
}

private static void createWindow() {          
    JFrame frame = new JFrame("Translucent Window");

    frame.setDefaultCloseOperation(
     JFrame.EXIT_ON_CLOSE
    );
  
    createUI(frame);
    frame.setVisible(true);          
}

private static void createUI(JFrame frame){
    frame.setLayout(new GridBagLayout());
    frame.setSize(200, 200);            
    frame.setLocationRelativeTo(null);
    frame.setBackground(new Color(0,0,0,0));

    JPanel panel = new javax.swing.JPanel() {
        protected void paintComponent(Graphics g) {
            if (g instanceof Graphics2D) {
               final int R = 100;
               final int G = 100;
               final int B = 100;                
              
              Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),
              getWidth(), getHeight(), new Color(R, G, B, 255), true);

              Graphics2D g2d = (Graphics2D)g;
              g2d.setPaint(p);
              g2d.fillRect(0, 0, getWidth(), getHeight());
            } else {
                super.paintComponent(g);
            }
        }
    };
  
    panel.setLayout(new GridBagLayout());
    panel.add(new JButton("Hello World"));
    frame.setContentPane(panel);
}
}