[英]Making a JFrame containing a JPanel transparent
This is regarding a Java Swing application. 这是关于Java Swing应用程序的。 I have a
JFrame
whose contentPane
is set to a JLayer
. 我有一个
JFrame
其contentPane
设置为JLayer
。 The JLayer
has its border set to TitledBorder
. JLayer
的边界设置为TitledBorder
。 This is how the resulting GUI looks like. 这就是生成的GUI的样子。
I want the JFrame
to be completely transparent so that the resulting GUI has a JLayer
with a rounded rectangle as the border. 我希望
JFrame
完全透明,以便生成的GUI有一个带圆角矩形的JLayer
作为边框。 Only the rounded rectangle should be visible. 只能看到圆角矩形。 The light gray colour surrounding it shouldn't be visible.
周围的浅灰色不应该是可见的。 How can it be done?
如何做呢?
This is how the final GUI should look like. 这就是最终GUI的外观。
Here is a short program illustrating the above problem: 这是一个简短的程序,说明了上述问题:
public class del extends javax.swing.JFrame {
JPanel loginPanel;
public del() {
initComponents();
}
public void initComponents() {
loginPanel = new javax.swing.JPanel();
loginPanel.setBackground(new java.awt.Color(204, 204, 204));
loginPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
add(loginPanel);
setUndecorated(true);
setMinimumSize(new Dimension(100, 100));
setLocation(200, 200);
}
public static void main(String args[]) throws Exception {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new del().setVisible(true);
}
});
}
}
}
And here is the resulting GUI: 以下是生成的GUI:
Continuing my comment... 继续我的评论......
Here is an example I made hope it helps: 这是一个我希望它有帮助的例子:
Basically overrides componentResized(ComponentEvent e)
of JFrame
changing its shape to RoundRectangle2D
. 基本上覆盖
JFrame
componentResized(ComponentEvent e)
,将其形状更改为RoundRectangle2D
。 Then a JPanel
is added with custom AbstarctBorder
to take care of round edges of border. 然后添加一个
JPanel
与自定义 AbstarctBorder
来处理边框的圆边。
import java.awt.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.*;
import javax.swing.border.AbstractBorder;
public class ShapedWindowAndBorderDemo extends JFrame {
public ShapedWindowAndBorderDemo() {
super("Shaped Window and Border Demo");
final JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};
// 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 RoundRectangle2D.Float(panel.getX(), panel.getY(), panel.getWidth(), panel.getHeight(), 10, 10));
}
});
setUndecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//panel.setBorder(new CurvedBorder());//creates a single lined border
panel.setBorder(new CurvedBorder(2, true));
panel.add(new JButton("I am a Button"));
add(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
// Determine what the GraphicsDevice can support.
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
//If shaped windows aren't supported, exit.
if (!gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT)) {
System.err.println("Shaped windows are not supported");
System.exit(0);
}
// Create the GUI on the event-dispatching thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ShapedWindowAndBorderDemo();
}
});
}
}
class CurvedBorder extends AbstractBorder {
private Color wallColor = Color.gray;
private int sinkLevel = 1;
private boolean indent = false;
public CurvedBorder(Color wall) {
this.wallColor = wall;
}
public CurvedBorder(int sinkLevel, Color wall, boolean indent) {
this.wallColor = wall;
this.sinkLevel = sinkLevel;
}
public CurvedBorder(int sinkLevel, boolean indent) {
this.sinkLevel = sinkLevel;
this.indent = indent;
}
public CurvedBorder() {
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
super.paintBorder(c, g, x, y, w, h);
g.setColor(getWallColor());
for (int i = 0; i < sinkLevel; i++) {
g.drawRoundRect(x, y, w - 1 - i, h - 1 - i, 10 - i, 10 - i);
if (indent) {
g.drawRoundRect(x + i, y + i, w - 1, h - 1, 10 - i, 10 - i);
}
}
}
@Override
public boolean isBorderOpaque() {
return true;
}
@Override
public Insets getBorderInsets(Component c) {
return new Insets(sinkLevel, sinkLevel, sinkLevel, sinkLevel);
}
@Override
public Insets getBorderInsets(Component c, Insets i) {
i.left = i.right = i.bottom = i.top = sinkLevel;
return i;
}
public boolean isIndented() {
return indent;
}
public int getSinkLevel() {
return sinkLevel;
}
public Color getWallColor() {
return wallColor;
}
}
Reference 参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.