JavaSwing添加背景图片
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
`
还没有评论,来说两句吧...