The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
As of the Java Platform, Standard Edition 6 (Java SE 6) Update 10 release, you can add translucent and shaped windows to your Swing applications. 从Java Platform,Standard Edition 6(Java SE 6)Update 10发行版开始,您可以向Swing应用程序添加半透明和形状的窗口。This page covers the following topics:本页涵盖以下主题:
This functionality, which is part of the public AWT package in the JDK 7 release, takes three forms, as follows:此功能是JDK 7版本中公共AWT包的一部分,有三种形式,如下所示:
Click the Launch button to run the TranslucentWindowDemo example using Java™ Web Start. 单击Launch按钮,使用Java™Web启动运行TranslucentWindowDemo示例。This example requires JDK 7 or later. 此示例需要JDK 7或更高版本。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引。
Click the Launch button to run the GradientTranslucentWindowDemo example using Java™ Web Start. 单击“启动”按钮,使用Java™Web启动运行GradientTranslucentWindowDemo示例。This example requires JDK 7 or later. 此示例需要JDK 7或更高版本。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引。
Shape
object that you can define. Shape
对象创建窗口。Click the Launch button to run the ShapedWindowDemo example using Java™ Web Start. 单击Launch按钮,使用Java™启动运行ShapedWindowDemo示例。This example requires JDK 7 or later. 此示例需要JDK 7或更高版本。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引。
UnsupportedOperationException
exception is thrown when code attempts to invoke the setShape
or setOpacity
methods on a platform that does not support these capabilities. setShape
或setOpacity
方法时,将引发UnsupportedOperationException
异常。GraphicsDevice
class provides the isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency)
method that you can use for this purpose. GraphicsDevice
类提供了可用于此目的的isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency)
方法。GraphicsDevice.WindowTranslucency
, to this method:GraphicsDevice.WindowTranslucency
中定义的三个枚举值之一传递给此方法:
TRANSLUCENT
– PERPIXEL_TRANSLUCENT
– PERPIXEL_TRANSPARENT
– GraphicsConfiguration
class also provides the isTranslucencyCapable
method to determine if PERPIXEL_TRANSLUCENT
translucency is supported by the given GraphicsConfiguration
object. GraphicsConfiguration
类还提供isTranslucencyCapable
方法,以确定给定的GraphicsConfiguration
象是否支持PERPIXEL_TRANSLUCENT
半透明。import static java.awt.GraphicsDevice.WindowTranslucency.*; // Determine what the default GraphicsDevice can support. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); boolean isUniformTranslucencySupported = gd.isWindowTranslucencySupported(TRANSLUCENT); boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT); boolean isShapedWindowSupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT);
IllegalComponentStateException
exception to be thrown. IllegalComponentStateException
异常。setOpacity(float)
method in the Window
class. window
类中的setOpacity(float)
方法,可以创建一个窗口,其中每个像素都具有相同的半透明。float
argument passed to this method represents the translucency of the window and should be a value between 0 and 1, inclusive. float
参数表示窗口的半透明性,应该是介于0和1之间的值,包括0和1。getOpacity
method. getOpacity
方法。The
example creates a window that is 55 percent opaque (45 percent translucent). TranslucentWindowDemo.java
示例创建了一个55%不透明(45%半透明)的窗口。TranslucentWindowDemo.java
If the underlying platform does not support translucent windows, the example exits. 如果底层平台不支持半透明窗口,则退出示例。The code relating to opacity is shown in bold.与不透明度相关的代码以粗体显示。
import java.awt.*; import javax.swing.*; import static java.awt.GraphicsDevice.WindowTranslucency.*; public class TranslucentWindowDemo extends JFrame { public TranslucentWindowDemo() { super("TranslucentWindow"); setLayout(new GridBagLayout()); setSize(300,200); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add a sample button. add(new JButton("I am a Button")); } public static void main(String[] args) { // Determine if the GraphicsDevice supports translucency. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); //If translucent windows aren't supported, exit. if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) { System.err.println( "Translucency is not supported"); System.exit(0); } JFrame.setDefaultLookAndFeelDecorated(true); // Create the GUI on the event-dispatching thread SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TranslucentWindowDemo tw = new TranslucentWindowDemo(); // Set the window to 55% opaque (45% translucent). tw.setOpacity(0.55f); // Display the window. tw.setVisible(true); } }); } }
Note that the button is also affected by the uniform translucency. 请注意,按钮也受均匀半透明的影响。Setting the opacity affects the whole window, including any components that the window contains.设置不透明度会影响整个窗口,包括窗口包含的任何组件。
GradientPaint
class. GradientPaint
类。Invoking 在窗口上调用setBackground(new Color(0,0,0,0))
on the window causes the software to use the alpha values to render per-pixel translucency. setBackground(new Color(0,0,0,0))
会导致软件使用alpha值渲染每像素半透明。In fact, invoking 事实上,调用setBackground(new Color(0,0,0,alpha)
, where alpha
is less than 255, installs per-pixel transparency. setBackground(new Color(0,0,0,alpha)
,其中alpha
小于255
,将安装每像素透明度。So, if you invoke 因此,如果调用setBackground(new Color(0,0,0,128))
and do nothing else, the window is rendered with 50 percent translucency for each background pixel. setBackground(new Color(0,0,0,128))
而不执行其他操作,则窗口将以每个背景像素50%的半透明度渲染。However, if you are creating your own range of alpha values, you most likely will want an alpha value of 0.但是,如果您正在创建自己的alpha值范围,则很可能希望alpha的值为0。
While not prohibited by the public API, you will generally want to enable per-pixel translucency on undecorated windows. 虽然公共API不禁止,但您通常希望在未装饰的窗口上启用每像素半透明。In most cases, using per-pixel translucency on decorated windows does not make sense. 在大多数情况下,在装饰窗上使用每像素半透明是没有意义的。Doing so can disable the decorations, or cause other platform-dependent side effects.这样做可能会禁用装饰,或导致其他依赖于平台的副作用。
To determine if a window is using per-pixel translucency, you can use the 要确定窗口是否使用逐像素半透明,可以使用isOpaque
method.isOpaque
方法。
An example follows. First, here are the steps required to implement the example:下面是一个例子。首先,以下是实施示例所需的步骤:
setBackground(new Color(0,0,0,0))
on the window.setBackground(new Color(0,0,0,0))
。JPanel
instance that overrides the paintComponent
method.paintComponent
方法的JPanel
实例。paintComponent
method, create a GradientPaint
instance.paintComponent
方法中,创建GradientPaint
实例。GradientPaint
class smoothly interpolates the alpha values from the top to the bottom of the rectangle.GradientPaint
类从矩形的顶部到底部平滑插值alpha值。GradientPaint
instance as the panel's paint method.GradientPaint
实例设置为面板的绘制方法。Here is the code for the 下面是GradientTranslucentWindowDemo.java
example. GradientTranslucentWindowDemo.java
示例的代码。If the underlying platform does not support per-pixel translucency, this example exits. 如果底层平台不支持每像素半透明,则退出此示例。The code specifically relating to creating the gradient window is shown in bold.与创建渐变窗口相关的代码以粗体显示。
import java.awt.*; import javax.swing.*; import static java.awt.GraphicsDevice.WindowTranslucency.*; public class GradientTranslucentWindowDemo extends JFrame { public GradientTranslucentWindowDemo() { super("GradientTranslucentWindow"); setBackground(new Color(0,0,0,0)); setSize(new Dimension(300,200)); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics g) { if (g instanceof Graphics2D) { final int R = 240; final int G = 240; final int B = 240; Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), 0.0f, getHeight(), new Color(R, G, B, 255), true); Graphics2D g2d = (Graphics2D)g; g2d.setPaint(p); g2d.fillRect(0, 0, getWidth(), getHeight()); } } }; setContentPane(panel); setLayout(new GridBagLayout()); add(new JButton("I am a Button")); } public static void main(String[] args) { // Determine what the GraphicsDevice can support. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT); //If translucent windows aren't supported, exit. if (!isPerPixelTranslucencySupported) { System.out.println( "Per-pixel translucency is not supported"); System.exit(0); } JFrame.setDefaultLookAndFeelDecorated(true); // Create the GUI on the event-dispatching thread SwingUtilities.invokeLater(new Runnable() { @Override public void run() { GradientTranslucentWindowDemo gtw = new GradientTranslucentWindowDemo(); // Display the window. gtw.setVisible(true); } }); } }
Note that the button is not affected by the per-pixel translucency. 请注意,该按钮不受每像素半透明的影响。Setting the per-pixel translucency affects the background pixels only. 设置每像素半透明仅影响背景像素。If you want a window that has a uniformly translucent effect on the background pixels only, you can invoke 如果希望窗口仅对背景像素具有均匀的半透明效果,可以调用setBackground(new Color(0,0,0,alpha))
where alpha
specifies your desired translucency.setBackground(new Color(0,0,0,alpha))
,其中alpha
指定所需的半透明。
setShape(Shape)
method in the Window
class. window
类中的setShape(Shape)
方法来创建一个有形状的窗口。Shape
argument that is passed to the method determines how the window is clipped. Shape
参数决定了窗口的剪裁方式。The best practice for setting the window's shape is to invoke 设置窗口形状的最佳实践是在组件事件侦听器的setShape
in the componentResized
method of the component event listener. componentResized
方法中调用setShape
。This practice will ensure that the shape is correctly calculated for the actual size of the window. The following example uses this approach.这种做法将确保根据窗户的实际尺寸正确计算形状。以下示例使用此方法。
The ShapedWindowDemo.java
example creates an oval-shaped window with 70 percent opacity. ShapedWindowDemo.java
示例创建了一个不透明度为70%的椭圆形窗口。If the underlying platform does not support shaped windows, the example exits. 如果底层平台不支持成型窗口,则退出示例。If the underlying platform does not support translucency, the example uses a standard opaque window. 如果底层平台不支持半透明,则示例使用标准不透明窗口。You could modify this example to create a shaped window that also uses per-pixel translucency.您可以修改此示例以创建一个也使用逐像素半透明的成形窗口。
The code relating to shaping the window is shown in bold.与窗口形状相关的代码以粗体显示。
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.Ellipse2D; import static java.awt.GraphicsDevice.WindowTranslucency.*; public class ShapedWindowDemo extends JFrame { public ShapedWindowDemo() { super("ShapedWindow"); setLayout(new GridBagLayout()); // It is best practice to set the window's shape in // the componentResized method. Then, if the window // changes size, the shape will be correctly recalculated. addComponentListener(new ComponentAdapter() { // Give the window an elliptical shape. // If the window is resized, the shape is recalculated here. @Override public void componentResized(ComponentEvent e) { setShape(new Ellipse2D.Double(0,0,getWidth(),getHeight())); } }); setUndecorated(true); setSize(300,200); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new JButton("I am a Button")); } public static void main(String[] args) { // Determine what the GraphicsDevice can support. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = ge.getDefaultScreenDevice(); final boolean isTranslucencySupported = gd.isWindowTranslucencySupported(TRANSLUCENT); //If shaped windows aren't supported, exit. if (!gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT)) { System.err.println("Shaped windows are not supported"); System.exit(0); } //If translucent windows aren't supported, //create an opaque window. if (!isTranslucencySupported) { System.out.println( "Translucency is not supported, creating an opaque window"); } // Create the GUI on the event-dispatching thread SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ShapedWindowDemo sw = new ShapedWindowDemo(); // Set the window to 70% translucency, if supported. if (isTranslucencySupported) { sw.setOpacity(0.7f); } // Display the window. sw.setVisible(true); } }); } }
com.sun.awt.AWTUtilities
class. com.sun.awt.AWTUtilities
类中实现的。Method in Java SE 6 Update 10 | JDK 7 Equivalent |
---|---|
AWTUtilities.isTranslucencySupported(Translucency) |
GraphicsDevice.isWindowTranslucencySupported(WindowTranslucency) |
AWTUtilities.isTranslucencyCapable(GraphicsConfiguration) |
GraphicsConfiguration.isTranslucencyCapable() |
AWTUtilities.setWindowOpacity(Window, float) |
Window.setOpacity(float) |
AWTUtilities.setWindowShape(Window, Shape) |
Window.setShape(Shape) |
AWTUtilities.setWindowOpaque(boolean) |
Window.setBackground(Color) new Color(0,0,0,alpha) to this method, where alpha is less than 255, installs per-pixel translucency.new Color(0,0,0,alpha) 传递给此方法,其中alpha 小于255,将安装每像素半透明。 |