java 窗体半透明_java – 半透明窗口内的半透明JPopupMenu – 替代?

我不确定这是否可行,但有没有办法安全地允许弹出窗口是半透明的,即使父容器也是半透明的?

如果没有,使用或扩展而不是JPopupMenu的明智替代方案是什么?

注意:半透明是指不具有“背景”的组件,类似于setOpaque(false);的效果.谢谢.

来自2009年用户camickr的论坛回答:

I don’t know if transparency painting has changed in 1.6.0_10. Prior

to that I believe transparency can only be achieved in lightweight

components (ie. Swing does all the painting). JFrame, JWindow and

JDialog are not lightweight because they use OS components.

In the case of a popup, it is lightweight when entirely contained

within its parent frame. But a lightweight popup can not be painted

outside the bounds of the frame so a JWindow (I believe) is used as

the popup, which can’t be transparent.

SSCCE:在半透明的JFrame顶部显示半透明的JWindow

import com.sun.awt.AWTUtilities;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class OpaqueWindowSSCCE {

private int countdown = 5;

public static void main(String[] args) {

new OpaqueWindowSSCCE();

}

public OpaqueWindowSSCCE() {

final JFrame frame = new JFrame("OpaqueWindowSSCCE");

final JWindow window = new JWindow();

new Timer(1000, new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

if(--countdown == 0){

frame.dispose();

window.dispose();

System.exit(0);

} else {

frame.repaint();

}

}

}).start();

frame.setContentPane(new JPanel() {

@Override

public void paintComponent(Graphics paramGraphics) {

super.paintComponent(paramGraphics);

Graphics2D g = (Graphics2D) paramGraphics.create();

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(new Color(50, 50, 50));

g.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);

g.setColor(new Color(180, 180, 180));

g.drawString("Closing in " + countdown + " seconds", 20, 25);

}

});

window.setContentPane(new JPanel() {

@Override

public void paintComponent(Graphics paramGraphics) {

super.paintComponent(paramGraphics);

Graphics2D g = (Graphics2D) paramGraphics.create();

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(new Color(180, 180, 180));

g.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);

}

});

frame.setUndecorated(true);

((JComponent) frame.getContentPane()).setOpaque(false);

((JComponent) window.getContentPane()).setOpaque(false);

AWTUtilities.setWindowOpaque(frame, false);

AWTUtilities.setWindowOpaque(window, false);

window.setAlwaysOnTop(true);

frame.setBounds(200,200,500,500);

window.setBounds(600,600,200,200);

frame.setVisible(true);

window.setVisible(true);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值