wuzhuang12
ant_1024
2016-01-22 04:06
采纳率: 60%
浏览 1.9k
已采纳

关于java为什么我的背景图片出不来

package org.liky.game.frame;

import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class FiveChessFrame extends JFrame implements MouseListener {

//获取屏幕的高度
int height = Toolkit.getDefaultToolkit().getScreenSize().height ;
//获取屏幕的宽度
int width = Toolkit.getDefaultToolkit().getScreenSize().width ;
//图片初始化
BufferedImage background = null ;
public FiveChessFrame(){

    //设置标题
    this.setTitle("五子棋") ;
    //设置窗体大小不可改变
    this.setSize(500,500) ;
    //设置初始窗体显示的位置
    this.setLocation((width - 500)/2, (height - 500)/2) ;
    //将窗体大小设置为大小不可改变
    this.setResizable(false) ;
    //将窗体的关闭设置为默认关闭后结束
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    //将窗体显示出来
    this.setVisible(true) ;
    //获取图片
    String image = "D:"+File.separator+"b.jpg" ;
    try {
        background = ImageIO.read(new File(image)) ;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
public void paint(Graphics g){

    g.drawImage(background , 0 , 20, this) ;
    g.drawString("黑体", 10, 30) ;
}
@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

}

  • 图片说明

修改过后还是显示不出背景图片

package org.liky.game.frame;

import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class FiveChessFrame extends JFrame implements MouseListener {

//获取屏幕的高度
int height = Toolkit.getDefaultToolkit().getScreenSize().height ;
//获取屏幕的宽度
int width = Toolkit.getDefaultToolkit().getScreenSize().width ;
//图片初始化
BufferedImage background = null ;
public FiveChessFrame(){

    //设置标题
    this.setTitle("五子棋") ;
    //设置窗体大小不可改变
    this.setSize(500,500) ;
    //设置初始窗体显示的位置
    this.setLocation((width - 500)/2, (height - 500)/2) ;
    //将窗体大小设置为大小不可改变
    this.setResizable(false) ;
    //将窗体的关闭设置为默认关闭后结束
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    //将窗体显示出来
    this.setVisible(true) ;
    //获取图片
    String image = "D:"+File.separator+"b.jpg" ;
    try {
        background = ImageIO.read(new File(image)) ;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
public void paint(Graphics g){
    super.paint(g) ;
    g.drawImage(background , 0 , 20, this) ;
    g.drawString("黑体", 10, 30) ;
}
@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

}
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • wojiushiwo945you
    毕小宝 2016-01-22 06:11
    已采纳

    测试了你的代码,的确无法显示背景图片,虽然 g.drawImage(background , 0 , 20, this) ;方法返回为true,但是不知道为何没有绘制背景。
    如果不介意话,可以换一种方法,用JLabel设置背景。参考这个:http://ask.csdn.net/questions/232946

    点赞 评论
  • caozhy

    String image = "D:"+File.separator+"b.jpg" ;
    是放在d盘么

    点赞 评论
  • enpterexpress
    把分全给哥 2016-01-22 05:26

    String image = "D:"+File.separator+"b.jpg" ;
    生成的路径和你的背景图片路径一样不

    点赞 评论

相关推荐