JFrame没有显示图片

以下是我到目前为止的代码:所有导入都是正确的。 我确定。 :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

你在WindowPractice类中声明了一个名为steve的实例字段,这很好,但是在你的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); } 

这意味着类实例变量从不初始化并保持为null

问题#2

虽然没有直接关系,但你永远不会从你的paint方法中调用super.paint 。 这是一个很大的NO,NO。 您有义务维护油漆链。 油漆方法很复杂,非常非常重要。

问题#3

您永远不应该覆盖顶级容器(例如JFrame ),也不应该覆盖它的任何paint方法。 这有很多原因,但在前两个容器中,大多数顶级容器实际上包含许多组件( JRootPane ,它包含玻璃窗格,内容窗格,图层窗格和菜单栏),可以支持您的绘画工作而且,一般来说,它们不是双缓冲的,这意味着你的绘画更新会闪烁,看起来很可怕;)

你也应该避免使用paint ,相反,你应该考虑使用paintComponent

问题#4

ImageIcon不是加载图像的最佳选择。 我不使用它们的主要原因是你不知道加载的图像何时才真正可用(实际上有方法,但坦率地说, ImageIO更简单)。 这是1999年的一个很棒的function,当拨号速度达到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); } } } } 

读一读

  • 执行自定义绘画
  • 使用图像

有关更多信息。