在 Java Swing 中创建渐变半透明窗口


借助 JDK 7,我们可以非常轻松地使用 swing 创建基于渐变的半透明窗口。以下是制作基于渐变的半透明窗口所需步骤。

 首先使 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);

示例

请看以下具有基于渐变的半透明效果的窗口示例。

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

输出

更新于:19-6-2020

2K+ 浏览次数

开启你的 职业生涯

完成课程即可获得认证

开始
广告
© . All rights reserved.