Swing窗口设置透明窗体

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/blue_space/article/details/83088329

1、AWTUtilities

透明窗体

jFrame.setUndecorated(true);
AWTUtilities.setWindowOpaque(jFrame, false);

半透明窗体

jFrame.setUndecorated(true);
AWTUtilities.setWindowOpacity(jFrame, 0.5f);

2、jan.jar包,

透明窗体

System.setProperty("sun.java2d.noddraw", "true");
jFrame.setUndecorated(true);
WindowUtils.setWindowTransparent(jFrame, true);

半透明窗体

System.setProperty("sun.java2d.noddraw", "true");
jFrame.setUndecorated(true);
WindowUtils.setWindowAlpha(jFrame, 0.5f);

3、JFrame中自带方法

(1)透明窗体

jFrame.setUndecorated(true);
jFrame.setOpacity(0.5f);

(2)透明窗体

jFrame.setUndecorated(true);
jFrame.setBackground(new Color(r, g, b, int alph));

注:上边所有设置(半)透明窗口的方法,是真透明,其所有子组件也都(半)透明了

除了,AWTUtilities.setWindowOpaque(jFrame, false);jFrame.setBackground(new Color(r, g, b, int alph));

Swing组件中透明的位置将不会获得鼠标焦点。意思是鼠标可以直接点到底下的桌面的其他应用。

那就表明,你可以画出任意形状的窗体,只要你愿意!

 

结:有最后一种方法,那前面所有的方法都他妈智障!应该是新版本加的方法。

展开阅读全文

Swing设置透明问题

08-06

我编写了一个Swing登录框,有设置背景,但是外层的jpanel会把背景覆盖掉,感觉很丑,要怎么设置透明,各位大神帮帮忙!rn代码如下:rnrnpublic class UserLogin extends JFramern rn private Graphics g;rn rn //构造函数rn public UserLogin()rn //加载背景图片rn final ImageIcon icon = new ImageIcon("D:\\workspace\\images\\bg_login.jpg");rn JPanel container = new JPanel() rn public void paintComponent(Graphics g) rn super.paintComponent(g);rn Graphics2D g2d = (Graphics2D) g;rn if(icon != null)rn g2d.drawImage(icon.getImage(), 0, 0, getWidth(), this.getHeight(), this);rn rn rn ;rn rn //logo图片rn JLabel lblIamge = new JLabel();rn ImageIcon admin_p = new ImageIcon("D:\\workspace\\images\\admin_p.gif");rn lblIamge.setIcon(admin_p);rn rn //用户名、密码、登录rn JTextField tfUserName = new JTextField(15);rn JPasswordField pfPassword = new JPasswordField(15);rn JButton bntLogin = new JButton("登录");rn tfUserName.setBackground(null);rn tfUserName.setOpaque(false);rn pfPassword.setOpaque(false);rn rn JPanel center_right = new JPanel();rn rn center_right.setLayout(new GridLayout(3,1,10,10));rn center_right.setBackground(null);rn center_right.setOpaque(false);rn center_right.add(tfUserName);rn center_right.add(pfPassword);rn center_right.add(bntLogin);rn rn rn JPanel center = new JPanel();rn center.add(lblIamge);rn center.add(center_right);rn rn rn container.add(center);rn rn //this.setBounds(0,0,400,300);rn this.setSize(400,300); //设置窗口大小rn this.setLocationRelativeTo(null); //设置窗口居中rn this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rn this.setContentPane(container);rn this.setVisible(true);rn rn rn rn public static void main(String[] args)rn SwingUtilities.invokeLater(new Runnable() rn rn @Overridern public void run() rn new UserLogin();rn rn rn );rn rnrn运行后的效果:rn[img=https://img-bbs.csdn.net/upload/201408/06/1407296214_504496.jpg][/img]rn 论坛

没有更多推荐了,返回首页