以下是我到目前为止的代码:所有导入都是正确的.我确定.: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;
}
}
Run Code Online (Sandbox Code Playgroud)
这有很多问题我不知道从哪里开始......
让我们从头开始......
问题#1
你声明了一个steve在你的WindowPractice类中调用的实例字段,这很好,但在你的main方法中,你声明了steve一个你正在使用对加载图像的引用的另一个变量...
public static void main(String[] args) {
new WindowPractice();
ImageIcon steve = new ImageIcon("C:/Users/shane/Dropbox/issue459.jpg");
JLabel imageLabel = new JLabel(steve);
}
Run Code Online (Sandbox Code Playgroud)
这意味着类实例变量永远不会被初始化并保留null.
问题#2
虽然没有直接关系,但您从不打电话super.paint给您的paint方法.这是一个很大的没有,没有.您有义务维护油漆链.涂料方法很复杂,非常非常重要.
问题#3
你永远不应该覆盖顶级容器(例如JFrame),也不应该覆盖任何paint方法.这有很多原因,但在前两个中,大多数顶级容器实际上包含许多组件(包含JRootPane玻璃窗格,内容窗格,图层窗格和菜单栏),这些组件可以支持您的绘画工作和一般来说,它们不是双缓冲的,这意味着你的绘画更新会闪烁,看起来很可怕;)
您应该避免使用paint,相反,您应该考虑使用paintComponent它可用的地方.
问题#4
ImageIcon不是你加载图片的最佳选择.我不使用它们的主要原因是你不知道加载的图像何时才真正可用(实际上有方法,但坦白说,ImageIO只是更简单).这是1999年的一个很好的功能,当拨号速度达到14.4k左右时,但现在......
ImageIO 支持更广泛的图片格式,支持图像的读取和写入,并保证当方法返回(成功)时,图像像素数据可供您的应用程序使用.
例
这是一个更好的(恕我直言)方法......

public class BetterDrawing {
public static void main(String[] args) {
new BetterDrawing();
}
public BetterDrawing() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new PaintPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintPane extends JPanel {
private BufferedImage background;
public PaintPane() {
try {
background = ImageIO.read(new File("/path/to/image"));
// Use this instead to load embedded resources instead
//background = ImageIO.read(getClass().getResource("/path/to/image"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g.drawImage(background, x, y, this);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
读一读
有关更多信息.