Swing设置窗体背景图片

67 篇文章 16 订阅

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); // 将组件添加到面板中
	}

}

注意:“设置背景”的代码必须放在其它控件的下面,否则会遮住其它的控件。

执行效果:

  • 15
    点赞
  • 104
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Java Swing中为窗体添加背景图片,可以按照以下步骤进行操作: 1. 创建一个继承自JFrame的自定义窗体类。 2. 在该类的构造函数中设置窗体的标题、大小和关闭操作。 3. 创建一个继承自JPanel的自定义面板类,并重写其paintComponent方法来绘制背景图片。 4. 在自定义窗体类的构造函数中创建背景面板对象,并将其设置窗体的内容面板。 下面是一个示例代码: ```java import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; public class BackgroundFrame extends JFrame { public BackgroundFrame() { setTitle("Background Image Demo"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BackgroundPanel backgroundPanel = new BackgroundPanel(); setContentPane(backgroundPanel); pack(); setLocationRelativeTo(null); // 居中显示窗口 } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { BackgroundFrame frame = new BackgroundFrame(); frame.setVisible(true); }); } private static class BackgroundPanel extends JPanel { private Image backgroundImage; public BackgroundPanel() { backgroundImage = new ImageIcon("path/to/your/image.jpg").getImage(); Dimension dimension = new Dimension(backgroundImage.getWidth(null), backgroundImage.getHeight(null)); setPreferredSize(dimension); } @Override protected void paintComponent(Graphics graphics) { super.paintComponent(graphics); graphics.drawImage(backgroundImage, 0, 0, null); } } } ``` 在上述代码中,将`path/to/your/image.jpg`替换为你自己的背景图片路径。然后运行`BackgroundFrame`类,就会显示带有背景图片窗体。 希望这对你有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pan_junbiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值