- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.JWindow;
- public class StartWindow extends JWindow
- {
- String imgName = "/images/love.png";//在images目录下存放的图片
- Toolkit tool = Toolkit.getDefaultToolkit();
- Image img = tool.createImage(imgName);//tool.getImage(imgName);
- int scnWidth = (int)tool.getScreenSize().getWidth();
- int scnHeight = (int)tool.getScreenSize().getHeight();
- int imgWidth = img.getWidth(this);
- int imgHeight = img.getHeight(this);
-
- StartWindow()
- {
-
- guiInit();
- //添加了鼠标单击事件,单击则退出程序
- addMouseListener(new MouseAdapter()
- {
- @Override
- public void mouseClicked(MouseEvent e)
- {
- // TODO Auto-generated method stub
- StartWindow.this.dispose();
- System.exit(0);
- }
- });
- }
-
- void guiInit()
- {
- //this.setBackground(Color.blue);
- this.setBounds(scnWidth/2 - imgWidth/2, scnHeight/2 - imgHeight/2, imgWidth, imgHeight);
- this.setVisible(true);
- this.toFront();
- }
-
- public void paint(Graphics g)
- {
- g.drawImage(img, 0, 0, this);//运行后,没有任何反应
- //下面的可以正常显示
- // g.draw3DRect(0, 0, imgWidth-1, imgHeight-1, true);
- // g.drawString(""+imgWidth, 10, 10);
- // g.drawLine(0, 0, imgWidth-1, imgHeight-1);
-
- }
-
- }
复制代码 为什么我的图片不能显示?
|