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
}
}
測試了你的代碼,的確無法顯示背景圖片,雖然 g.drawImage(background , 0 , 20, this) ;方法返回為true,但是不知道為何沒有繪制背景。
如果不介意話,可以換一種方法,用JLabel設置背景。參考這個:http://ask.csdn.net/questions/232946