jframe 添加背景图片问题_百度知道

2022-08-29 14:37:27   第一文档网     [ 字体: ] [ 阅读: ] [ 文档下载 ]
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。下载word有问题请添加QQ:admin处理,感谢您的支持与谅解。点击这里给我发消息

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《jframe 添加背景图片问题_百度知道》,欢迎阅读!
背景图片,添加,jframe,知道,问题

添加背景代码如下:

一个例子:

import java.awt.FlowLayout;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class bj {

private JFrame frame = new JFrame("背景图片测试");

private JPanel imagePanel;

private ImageIcon background;

public static void main(String[] args) {

new bj();

}

public bj() {

background = new ImageIcon("C:\\Users\\Pictures\\2.jpg");// 背景图片

JLabel label = new JLabel(background);// 把背景图片显示在一个标签里面

// 把标签的大小位置设置为图片刚好填充整个面板

label.setBounds(0, 0, background.getIconWidth(),

background.getIconHeight());

// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明

imagePanel = (JPanel) frame.getContentPane();

imagePanel.setOpaque(false);

// 内容窗格默认的布局管理器为BorderLayout

imagePanel.setLayout(new FlowLayout());

imagePanel.add(new JButton("测试按钮"));

frame.getLayeredPane().setLayout(null);

// 把背景图片添加到分层窗格的最底层作为背景

frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(background.getIconWidth(), background.getIconHeight());

frame.setResizable(false);

frame.setVisible(true);

}

}

本文来源:https://www.dywdw.cn/cfdc911279563c1ec5da717a.html

相关推荐
推荐阅读