【发布时间】:2012-12-14 01:18:25
【问题描述】:
以下是我目前的代码: 所有的导入都是正确的。我确定。 :D
当我运行程序时,我得到的只是一个空白帧,没有图片。它应该出现。
public class WindowPractice extends JFrame {
final static int width= 800;
final static int height= 400;
int x;
int y;
Image steve;
Dimension gamesize= new Dimension (width, height);
public WindowPractice(){
setTitle ("Hangman");
setSize (gamesize);
setVisible (true);
setResizable (false);
setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main (String[] args) {
new WindowPractice();
ImageIcon steve= new ImageIcon ("Libraries/Pictures/ba190cd951302bcebdf216239e156a4.jpg");
JLabel imageLabel = new JLabel(steve);
}
public void paint(Graphics g){
g.setColor(Color.red);
//g.fillRect( x, y, 100, 20);
g.drawImage(steve, x, y,this);
x= 150;
y= 250;
}
}
【问题讨论】:
-
两件事。 1)
steve为空,因此不绘制任何内容。 2)paintComponent应该被覆盖自定义绘画,而不是paint。 -
@Vulcan 虽然我同意你的 (
paintComponent) 是自定义绘画的更好选择,但JFrame没有paintComponent方法;) -
@MadProgrammer 我完全没有注意到它是一个 JFrame,哎呀!我立即认为它是 JPanel。
-
@Vulcan 不过还是不错的建议 ;)