Swing设置窗体背景图片,简单的实现方法就是在窗体中放在一个JLabel标签组件,然后设置该标签的图标属性,最后设置该标签的大小与位置将其铺满窗体。
// 设置背景
JLabel lblBackground = new JLabel(); // 创建一个标签组件对象
URL resource = this.getClass().getResource("/background.jpg"); // 获取背景图片路径
ImageIcon icon = new ImageIcon(resource); // 创建背景图片对象
lblBackground.setIcon(icon); // 设置标签组件要显示的图标
lblBackground.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight()); // 设置组件的显示位置及大小
frame.getContentPane().add(lblBackground); // 将组件添加到面板中
示例:设置窗体背景图片,并在窗体中放置两个按钮。
import java.awt.EventQueue;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* Swing设置窗体背景图片
*
* @author pan_junbiao
*
*/
public class SetBackground
{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
SetBackground window = new SetBackground();
window.frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public SetBackground()
{
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize()
{
// 初始化窗体
frame = new JFrame("有背景图的窗体");
frame.setBounds(100, 100, 350, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
// 按钮1
JButton btnNewButton_1 = new JButton("按钮1");
btnNewButton_1.setBounds(10, 10, 93, 23);
frame.getContentPane().add(btnNewButton_1);
// 按钮2
JButton btnNewButton = new JButton("按钮2");
btnNewButton.setBounds(114, 10, 93, 23);
frame.getContentPane().add(btnNewButton);
// 设置背景
JLabel lblBackground = new JLabel(); // 创建一个标签组件对象
URL resource = this.getClass().getResource("/background.jpg"); // 获取背景图片路径
ImageIcon icon = new ImageIcon(resource); // 创建背景图片对象
lblBackground.setIcon(icon); // 设置标签组件要显示的图标
lblBackground.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight()); // 设置组件的显示位置及大小
frame.getContentPane().add(lblBackground); // 将组件添加到面板中
}
}
注意:“设置背景”的代码必须放在其它控件的下面,否则会遮住其它的控件。
执行效果: