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

packagetupian;importjava.awt.Dimension;importjava.awt.FlowLayout;importjava.awt.Graph... package tupian;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.print.DocFlavor.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class beijing2 extends JFrame {
JButton jb=new JButton("yundfj");
JButton jb1=new JButton("dsfskdjf");
JLabel jl=new JLabel("dsfdsfksdfsj");

beijing2(){
this.setContentPane(new MyPanel());
setBounds(300, 300, 300, 300);
jb.setBounds(300, 300, 100, 50);
jb1.setBounds(400, 400, 100, 50);
add(jl);
add(jb1);
add(jb);
setLayout(null);
// setLayout(new FlowLayout());

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);

}
public static void main(String[] args) {
// TODO Auto-generated method stub
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);
}
}

}这样图片会覆盖按钮的,要把setLayout(null);改成setLayout(new FlowLayout());
才行哦,怎么样改采能够自由设置按钮的位置又不会被图片覆盖哦?那位高手帮帮小弟哦,谢谢哦
展开
 我来答
ania4
2010-06-22
知道答主
回答量:29
采纳率:0%
帮助的人:0
展开全部
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格式的
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式