在Java应用程序中,如何实现在背景图片上显示按钮,也就是在背景图片上添加个按钮

就是在窗体加个背景图片,然后我想在背景图片上加个按钮,不知道怎么弄,求各路大神帮忙啊!!!

用JLabel显示图片,在JLabel上添加按钮
例如:
JLabel lblImg = new JLabel(new ImageIcon("iimg/tupian.png"));
JButton btn = new JButton();
lblImg.add(btn);
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-02-14
在paintComponent事件,绘一张图片
第2个回答  2018-01-04
final JFrame jf=new JFrame("我的窗体");
JButton jb=new JButton("嘟嘟嘟");
//背景图片的路径。(相对路径或者绝对路径。)
String path = "E:\\优美图片\\123.jpg";
// 背景图片
ImageIcon background = new ImageIcon(path);
// 把背景图片显示在一个标签里面
JLabel label = new JLabel(background);
// 把标签的大小位置设置为图片刚好填充整个面板
label.setBounds(0, 0, this.getWidth(), this.getHeight());
// 把内容窗格转化为JPanel,否则不能用方法setOpaque()来使内容窗格透明
JPanel imagePanel = (JPanel) this.getContentPane();
imagePanel.setOpaque(false);
// 把背景图片添加到分层窗格的最底层作为背景
this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
label.add(jb);
jf.add(label); //将标签添加至窗体中
相似回答