Is it possible to have a translucent windows in java 7 including a title bar?

元气小坏坏 提交于 2019-12-01 01:12:16

问题


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)


回答1:


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)




回答2:


Fascinatingly, if you add

JFrame.setDefaultLookAndFeelDecorated(true);

Then it allows you to have a draggable, with title bar JFrame (it just uses the ugly java look and feel).

ref: Pass mouse events to applications behind from a Java UI

Now that is weird.

Also note that you can "fake" the old behavior using per pixel transparency, see http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html

so you can have the old behavior in both java 6 and 7, but you'd have to special case the code around it and do it one way for one, one way for the other, possibly...



来源:https://stackoverflow.com/questions/7353799/is-it-possible-to-have-a-translucent-windows-in-java-7-including-a-title-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!