添加背景图片为什么setlayout(null)后就会覆盖按钮,要用流式布局才不会覆盖哦?

2025-05-02 01:06:19
推荐回答(1个)
回答1:

public class beijing2 extends JFrame {
....
.......
beijing2(){
this.setContentPane(new MyPanel());//调用MyPanel
........
..............

}
public static void main(String[] args) {

new beijing2();
}
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("httpi.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}

}

把整个图片加在标签上,然后把标签的大小和窗口一样也可以充当背景图片//如果不能显示jpg的图片就把jpg图片转化为png格式的