您的位置:首页 > 编程语言 > Java开发

JavaSwing添加背景图片

2020-02-03 05:01 916 查看

JavaSwing添加背景图片

代码整理

  • 将标签设置为图片标签实现

/**
* @author how
* JavaSwing测试插入背景图片
* 2020/1/19
*/
import java.awt.*;
import javax.swing.*;
public class Background extends JFrame{
//定义组件
ImageIcon background;
JPanel myPanel;
JLabel label;//用于放标签
JLabel label2;
JButton button;
public static void main(String[] args) {
// TODO Auto-generated method stub
new Background();
}
Background()
{
init();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
void init(){
button = new JButton("图片");   //创建一个按钮
label2=new JLabel("风景");     //创建一个标签

background = new ImageIcon("src/1.jpg");	//创建一个背景图片
label = new JLabel(background);		//把背景图片添加到标签里
label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());	//把标签设置为和图片等高等宽
myPanel = (JPanel)this.getContentPane();		//把我的面板设置为内容面板
myPanel.setOpaque(false);					//把我的面板设置为不可视
myPanel.setLayout(new FlowLayout());		//把我的面板设置为流动布局
this.getLayeredPane().setLayout(null);		//把分层面板的布局置空
myPanel.add(label2);
myPanel.add(button);	//把按钮添加到我的面板里
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));		//把标签添加到分层面板的最底层
//设置界面属性
setTitle("测试背景图");
setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
}

}

to be continued
how

`

  • 点赞
  • 收藏
  • 分享
  • 文章举报
qq_45297146 发布了5 篇原创文章 · 获赞 0 · 访问量 121 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: