contentPane添加背景图片
用哪个eclipse可视化界面如何给contentPane添加背景图片呢?public Test() {
JLayeredPane pane = new JLayeredPane();
JLabel label;
JPanel panel1 = new JPanel();
JTextField field1 = new JTextField();
ImageIcon image;
// 读取图片注意文件路径和文件后缀
image = new ImageIcon("0.jpeg");
label = new JLabel(image);//把图片添加到标签里
panel1.setBounds(0, 0, image.getIconWidth(), image.getIconHeight()); //把标签设置为和图片等高等宽
panel1 = (JPanel)this.getContentPane(); //把我的面板设置为内容面板
panel1.add(label);
pane.add(panel1,JLayeredPane.DEFAULT_LAYER);
this.setTitle("派大星");
this.setBounds(100,100,image.getIconWidth(), image.getIconHeight());
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setLayeredPane(pane);
this.setVisible(true);
}
public static void main(String[] args) {
new Test();
} @Override
public void paintComponent(Graphics g) {
backimage = ImageIcon("图片路径地址").getImage();
Graphics2D g2 = (Graphics2D)g;
int width = getWidth();// 定义桌面面板的宽度
int height = getHeight();// 定义桌面面板的高度
g2.drawImage(backimage, 0, 0, width, height, this);// 绘制背景图片
}
页:
[1]