Home > Mobile >  How to set a gradient background to the form. Intellij
How to set a gradient background to the form. Intellij

Time:04-29

I am a beginner on the Intellij Idea GUI Form. And I don't know how to set a gradient background to the form. Can someone help me? Thanks.

My layout

This is my current layout. Instead of blue, I would like to use a gradient background, how can I do that?

CodePudding user response:

Make the background of JFrame transparent first.

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

Create a gradient paint, and fill the panel.

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());
    }
}

Assign the panel as a content pane to the frame.

frame.setContentPane(panel);

Example

See the example below of a window with gradient-based translucency.

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);
}
}
  • Related