The key source Java interface background images settings

How to make Java GUI components is not a background image to cover up?

Code is not much, but except for a few, most of the code is very redundant and online search to no avail, after a long test, I summarize the key code for a Java interface background image of the swing set in the following code from the Internet:


///把标签的大小位置设置为恰好填充整个面板
jl.setBounds(0, 0, img.getIconWidth(), img.getIconHeight()); 
//把内容窗格转化为JPanel,否则不能使用setOpaque()方法来使内容窗格透明化
jp1=(JPanel) this.getContentPane();
jp1.setOpaque(false);//////////////////////////////////////////////
jp1.setLayout(new FlowLayout());
jp1.add(jb);
this.getLayeredPane().setLayout(null);/////////////////////////////////////////////
//把内容图片添加到分层窗口的最底层作为背景
this.getLayeredPane().add(jl, new Integer(Integer.MIN_VALUE));



jl is JLabel object, i.e. JLabel jl = new JLabel ();
jp1 is JPanel objects.
jb is the JButton objects.
We can see that the background picture does not hide components, it only need to add the key 7 lines of code, of course, does he not cut a line or two I have not tried, but it added a few lines of code, the other to follow the normal way write, will be able to set the background image of success, you can not go wrong. Of course, in fact, the principle of which I still did not understand. But it can be used on the line, right?


Guess you like

Origin blog.csdn.net/z1094219402/article/details/45397053