@Override
public void paintComponent(Graphics gs) {
Graphics2D g = (Graphics2D) gs
super.paintComponent(g)
//画背景图片
Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource(imgPath))
g.drawImage(image, 0, 0,width,height, this)
}
一个完整的代码:
package testimport java.awt.Graphics
import java.awt.Image
import java.net.URL
import javax.swing.ImageIcon
import javax.swing.JFrame
import javax.swing.JPanel
//为窗口添加背景图片
public class JFrameBackground {
private JFrame frame = new JFrame("带背景图片的JFrame")
private JPanel imagePanel
private ImageIcon backgroundimg
public JFrameBackground() {
imagePanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g)
URL location = this.getClass().getResource("grapes.gif")
backgroundimg = new ImageIcon(location)
Image img = backgroundimg.getImage()
g.drawImage(img, 0, 0, backgroundimg.getIconWidth(),
backgroundimg.getIconHeight(),
backgroundimg.getImageObserver())
frame.setSize(backgroundimg.getIconWidth(),
backgroundimg.getIconHeight())
}
}
frame.add(imagePanel)
frame.setVisible(true)
frame.pack()
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
public static void main(String[] args) {
new JFrameBackground()
}
}
这是运行的一个截图:
参考网址:
http://www.linuxidc.com/Linux/2011-08/41689.htm 两种设置背景的方法
http://blog.csdn.net/one_and_only4711/article/details/6594770 动态改变背景大小
使用现成的swingx提供的JXPanel
JXPanel panel = new JXPanel()BufferedImage bi = ...
panel.setBackgroundPainter(new ImagePainter(bi))
按照你的要求给jDialog添加背景图片的Java程序如下
比如你的当前项目名是project1, 在D:\project\project1\ 下就是你的当前项目的根目录(其中D:\project\只是打个比方,以你的实际目录为准)
import java.awt.BorderLayoutimport java.awt.Container
import java.awt.Dimension
import java.awt.Image
import java.awt.Point
import java.awt.Rectangle
import java.awt.Toolkit
import javax.swing.ImageIcon
import javax.swing.JButton
import javax.swing.JDialog
import javax.swing.JLabel
import javax.swing.JLayeredPane
import javax.swing.JPanel
public class D {
D(){
JDialog jDialog = new JDialog()
jDialog.setSize(400, 200)
JPanel jp1 = new JPanel()
JPanel jp2 = new JPanel()
Container container = jDialog.getContentPane()
JPanel panelBg=new JPanel()
JPanel panelContent=new JPanel()
JLayeredPane lp=new JLayeredPane()
Dimension dimension = new Dimension(jDialog.getWidth(), jDialog.getHeight())
lp.setSize(dimension)
panelContent.setLayout(new BorderLayout())
Image imageBg = Toolkit.getDefaultToolkit().getImage("autumn1.jpg")
ImageIcon imageIcon = new ImageIcon(imageBg.getScaledInstance
(dimension.width, dimension.height, Image.SCALE_FAST))
JLabel jl1 = new JLabel(imageIcon)
Point origin = new Point(0, 0)
Rectangle rectangle = new Rectangle(origin, dimension)
panelBg.setBounds(rectangle)
panelBg.setLayout(new BorderLayout())
panelBg.add(jl1,BorderLayout.CENTER)
JLabel jl = new JLabel("正在测量......")
jp1.add(jl)
JButton jb = new JButton("取消")
jp2.add(jb)
jp1.setOpaque(false)
jp2.setOpaque(false)
panelContent.setBounds(rectangle)
panelContent.add(jp1,BorderLayout.CENTER)
panelContent.add(jp2,BorderLayout.SOUTH)
panelContent.setOpaque(false)
lp.add(panelBg,new Integer(0))
lp.add(panelContent, new Integer(1))
container.add(lp, BorderLayout.CENTER)
jDialog.setUndecorated(true)
jDialog.setLocationRelativeTo(null)
jDialog.setVisible(true)
}
public static void main(String[] args) {
new D()
}
}
运行结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)