java 7中是否可以有一个包含标题栏的半透明窗口?

2022-01-24 00:00:00 java swing jframe java-7 translucency

与这个问题有关:是Java教程半透明窗口示例那些玩jdk7的人有麻烦吗?

对于 jdk1.6.0_26,我似乎能够将半透明应用于 JFrame,但对于 jre7 则不然:

NativeException: java.awt.IllegalComponentStateException: 框架被装饰

ex(jruby 脚本 java,但 jdk1.6 不适用于 jdk7):

<上一页>需要'java'类 MouseDrawdef self.gojava_import 'javax.swing.JFrame'java_import 'com.sun.awt.AWTUtilities'f = JFrame.newAWTUtilities.set_window_opacity(f, 0.5)f.set_size 200,200f.show结尾结尾MouseDraw.go

所以我的问题是jdk7 中是否可以有一个半透明的标题栏"(我希望我创建的透明窗口可以拖动/调整大小)

解决方案

Java 7 引入 Window.setOpacity() 来做 Java 6 中非官方类 AWTUtilities 所做的事情.

不幸的是,它被证明不能使用装饰过的窗户:

<块引用>

要设置不透明度值小于1.0f,必须满足以下条件:

  • TRANSLUCENT 底层系统必须支持半透明
  • 窗口必须未装饰(请参阅 Frame.setUndecorated(boolean)Dialog.setUndecorated(boolean))
  • 窗口不得处于全屏模式(请参阅 GraphicsDevice.setFullScreenWindow(Window))

(强调我的)

Related to this question: Is The Java Tutorials Translucent Window example giving trouble to those playing with jdk7?

with jdk1.6.0_26 I seem to be able to apply translucency to a JFrame, but not so with jre7:

NativeException: java.awt.IllegalComponentStateException: The frame is decorated

ex (jruby scripting java, works jdk1.6 not with jdk7 though):


require 'java'

class MouseDraw

  def self.go
    java_import 'javax.swing.JFrame'
    java_import 'com.sun.awt.AWTUtilities'

    f = JFrame.new
    AWTUtilities.set_window_opacity(f, 0.5)
    f.set_size 200,200
    f.show
  end

end
MouseDraw.go

So my question is "is it possible to have a translucent title bar in jdk7" (I would like a transparent window I'm creating to be draggable/resizable)

解决方案

Java 7 introduced Window.setOpacity() to do what the unofficial class AWTUtilities did in Java 6.

Unfortunately it's documented not to work with decorated windows:

The following conditions must be met in order to set the opacity value less than 1.0f:

  • The TRANSLUCENT translucency must be supported by the underlying system
  • The window must be undecorated (see Frame.setUndecorated(boolean) and Dialog.setUndecorated(boolean))
  • The window must not be in full-screen mode (see GraphicsDevice.setFullScreenWindow(Window))

(Emphasis mine)

相关文章