java-在窗口中显示背景图

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Java,在窗口中显示背景图运行如下:代码如下:package在窗口中显示背景图;importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.Graphics;importjava.net.URL;importjavax.swing.ImageIcon;importjavax.swing.JButton;importjavax.swing.JComboBox;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JTextField;publicclassBackgroundPanelextendsJFrame{publicBackgroundPanel(){Containercp=getContentPane();cp.setLayout(newBorderLayout());NewPanelp=newNewPanel();cp.add(p,BorderLayout.CENTER);cp.add(newJButton(Button),BorderLayout.SOUTH);p.add(newJTextField(10));p.add(newJButton(Clickme));p.add(newJComboBox());p.add(newJButton(Hello,world!));setSize(400,300);setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);}publicstaticvoidmain(String[]args){newBackgroundPanel();}classNewPanelextendsJPanel{privatestaticfinallongserialVersionUID=1L;publicvoidpaintComponent(Graphicsg){intx=0,y=0;URLurl=getClass().getResource(图片.jpg);ImageIconicon=newImageIcon(url);g.drawImage(icon.getImage(),x,y,getSize().width,getSize().height,this);while(true){g.drawImage(icon.getImage(),x,y,this);if(xgetSize().width&&ygetSize().height)break;if(xgetSize().width){x=0;y+=icon.getIconHeight();}elsex+=icon.getIconWidth();}}}}

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功