微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用frame.setBackground0,255,0,0不适用于Linux

所以我一直通过设置背景alpha为0来使JFrame背景透明,但是如果我在linux下运行该程序,JFrame背景是白色的,为什么呢?

好吧,我发现它有一些与渲染graphics的框架,这种方法是旧的,我不需要它在程序了,但我仍然想知道为什么会发生这种情况(它只是在Linux的作品find在Windows中)

这里是一个可运行的例子

import java.awt.*; import java.awt.Color; import javax.swing.*; class Main extends JFrame{ private void init() { this.setPreferredSize(new Dimension(1420,820)); this.setUndecorated(true); this.setResizable(false); this.setDefaultCloSEOperation(this.EXIT_ON_CLOSE); this.setBackground(new Color(0,255,0)); this.requestFocus(); this.setVisible(true); this.validate(); this.pack(); Color color = UIManager.getColor("activeCaptionBorder"); this.getRootPane().setBorder(BorderFactory.createLineBorder(color,1)); paintInfo(); } private void paintInfo() { Graphics g = this.getGraphics(); g.setColor(new Color(222,222,4)); g.setFont(new Font("Arial Black",Font.BOLD,15)); g.setColor(Color.BLACK); g.drawString("test String ",this.getWidth()/2,this.getHeight()/2); g.dispose(); } public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); new Main().init(); } }

WOW64 SetlayeredWindowAttributes LWA_ALPHA

使用fState = ILS_ALPHA的ImageList_DrawIndirect是否可以在Windows XP上运行?

WPF无边界窗口的DropShadow

如何绘制儿童控制的透明区域?

如何使用Windows API在透明窗口上绘制animation?

用Qt显示半透明/不规则窗口

为最大化的WPF自定义窗口删除DropShadow

(C#)Windows窗体 – 透明的背景,同时捕获鼠标事件?

ListList上的ImageList透明度?

Emacs在Gnome Shell上的透明背景

我们从…开始

private void paintInfo() { Graphics g = this.getGraphics(); g.setColor(new Color(222,this.getHeight()/2); g.dispose(); }

不是如何在Swing完成绘画,并且处理一个你没有创建的Graphics上下文会导致奇怪的事情…

相反,创建你的框架,设置它的透明度,然后添加一个组件,它做你想要的实际绘画,例如…

import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.GridBagLayout; import java.awt.LinearGradientPaint; import java.awt.Point; import java.awt.Rectangle; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; /** * * @author swhitehead */ public class JavaApplication233 { /** * @param args the command line arguments */ public static void main(String[] args) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); //If translucent windows aren't supported,exit. if (!gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)) { System.err.println("Per-pixel translucency is not supported"); System.exit(0); } else { new JavaApplication233(); } } public JavaApplication233() { 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.setUndecorated(true); frame.setBackground(new Color(0,0)); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeto(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { setopaque(false); setLayout(new GridBagLayout()); add(new JLabel("I'm not wearing anything")); // Color color = UIManager.getColor("activeCaptionBorder"); setBorder(BorderFactory.createLineBorder(Color.BLACK,1)); } @Override public Dimension getPreferredSize() { return new Dimension(200,200); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); LinearGradientPaint lgp = new LinearGradientPaint( new Point(0,0),new Point(0,getHeight()),new float[]{0f,1f},new Color[]{applyAlpha(Color.RED),applyAlpha(Color.YELLOW)} ); g2d.setPaint(lgp); g2d.fill(new Rectangle(0,getWidth(),getHeight())); g2d.dispose(); } protected Color applyAlpha(Color color) { return new Color(color.getRed(),color.getGreen(),color.getBlue(),64); } } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐