栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

java使用图像自定义形状框架

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java使用图像自定义形状框架

要创建透明窗口,您需要将框架背景色的
Alpha设置为0。这可能是我一段
时间以来看到的最不直观的调用,就好像您对任何其他Swing组件执行此操作一样,您将完全
搞砸绘制过程。

您不想更改窗口的不透明度,因为它会影响
整个窗口,并且其内容均等。

例如…

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

frame.setBackground(new Color(0, 0, 0, 0));
您不必使用JWindow,但这意味着我不需要
自己修饰它…

您还需要确保添加到窗口的任何内容都是
透明的(opaque = false),以便它不会“隐藏”其下面的
内容……

import java.awt.Color;import java.awt.Dimension;import java.awt.EventQueue;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GridBagLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.image.BufferedImage;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JButton;import javax.swing.JPanel;import javax.swing.JWindow;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.border.CompoundBorder;import javax.swing.border.EmptyBorder;import javax.swing.border.LineBorder;public class LeafWindow {    public static void main(String[] args) {        new LeafWindow();    }    public LeafWindow() {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {     }     JWindow frame = new JWindow();     frame.setBackground(new Color(0, 0, 0, 0));     frame.setContentPane(new LeafPane());     frame.pack();     frame.setLocationRelativeTo(null);     frame.setVisible(true);     frame.setAlwaysonTop(true); }        });    }    public class LeafPane extends JPanel {        private BufferedImage leaf;        public LeafPane() { setBorder(new CompoundBorder(      new LineBorder(Color.RED),      new EmptyBorder(0, 0, 250, 0))); try {     leaf = ImageIO.read(getClass().getResource("/Leaf.png")); } catch (IOException ex) {     ex.printStackTrace(); } setOpaque(false); setLayout(new GridBagLayout()); JButton button = new JButton("Close"); button.addActionListener(new ActionListener() {     @Override     public void actionPerformed(ActionEvent e) {         System.exit(0);     } }); add(button);        }        @Override        public Dimension getPreferredSize() { return leaf == null ? new Dimension(200, 200) : new Dimension(leaf.getWidth(), leaf.getHeight());        }        @Override        protected void paintComponent(Graphics g) { super.paintComponent(g); if (leaf != null) {     Graphics2D g2d = (Graphics2D) g.create();     g2d.drawImage(leaf, 0, 0, this);     g2d.dispose(); }        }    }}

本示例故意为内容添加线边框,因为您可以看到原始的窗口边界。它还使用aEmptyBorder强制将其
JButton拖到图形上,但这只是一个示例……



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/455492.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号