Swing界面透明设置方法

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

终极解决之道

经过反复的研究探索,终于获得了一个完美的解决方法:不用shape、不用抓图、不用workaround,真正的、彻底的、完全的、随意的在桌面上任意绘图、涂鸦、撒野,真正的属于程序员的Freedom!下面就来一起揭开这层窗户纸吧!

在程序中依次设置以下几个参数:

设置窗口完全透明:AWTUtilities.setWindowOpaque(frame, false); 
设置窗口无边缘:frame.setUndecorated(true); 
设置窗口的ContentPane为要显示的Pane:frame.setContentPane(myPane); 
在myPane中放置具体要显示的内容,也可以重载paint方法进行Java2D绘制。这些paint会直接发生在桌面背景上。 
 
展开阅读全文

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 论坛

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