JPanel jpanel = new JPanel()
this.setContentPane(jpanel)
//
添加标签组件
GridLayout gird = new GridLayout(3,0)
jpanel.setLayout(gird)
ImageIcon img = new ImageIcon("src/JMXY.JPG")
JLabel imgLabel = new JLabel(img)//
将背景图放在标签里。
this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE))
imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight())
this.getLayeredPane().setLayout(null)
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
jpanel.setOpaque(false)
initComponents()
}
其中
Qua_Main_JFrame
为创建的
java
的代码都应该放在
initComponents()
方法之上,这样你添加进窗体中的空间才会显示在
图片之上,否则看不见控件。
用Swing包下的ImageIcon类就可以实现,比如在一个按钮中添加一张图片,就可以用以下代码实现:ImageIcon imageicon =new ImageIcon(String s)JButton b=new JButton(imageicon)其中参数s是所要添加图片的路径(绝对路径或相对路径)和名字。如想添加D盘下的图片1.jpg,就可以将上面改成:ImageIcon imageicon =new ImageIcon("D:\1.jpg")欢迎分享,转载请注明来源:内存溢出
评论列表(0条)