【问题标题】:Displaying an image in Java Swing在 Java Swing 中显示图像
【发布时间】:2011-11-30 22:21:12
【问题描述】:
public class MinesweeperMenu extends MinesweeperPanel{

private JPanel picture = new JPanel();
private JButton play = new JButton("Play");
private JButton highScores = new JButton("High Score and \nStatistics");
private JButton changeMap = new JButton("Create Custom \nor Change Map");
private JButton difficulty = new JButton("Custom or\nChange Difficulty");
private JButton user = new JButton("Change User");
Image img;

public MinesweeperMenu()
{
    // Set Layout for the menu
    LayoutManager menuLayout = new BoxLayout(menu, BoxLayout.Y_AXIS);
    menu.setLayout(menuLayout);

    // Set Layout for the window
    LayoutManager windowLayout = new BorderLayout();
    window.setLayout(windowLayout);

    // Add buttons to the panels
    menu.add(play);
    menu.add(highScores);
    menu.add(changeMap);
    menu.add(difficulty);
    menu.add(user);

    // Add picture to the frame
    try{
    File input = new File("./setup/images/MineMenuPicture.jpg");
    img = ImageIO.read(input);
    }
    catch(IOException ie)
    {
        System.out.println(ie.getMessage());
    }

    // Add action listeners
    changeMap.addActionListener(new ChangeMapListener());   

}


public void paintComponent(Graphics g)
{
    // POSITION OF THE PICTURE
    int x = 650;
    int y = 585;
    g.drawImage(img, x, y, null);
}

public void displayFrame()
{
    // Display Frame
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

public static void main(String[] args)
{
    MinesweeperMenu menu = new MinesweeperMenu();
    window.pack();
    menu.displayFrame();
    window.repaint();
}
}


public class MinesweeperPanel extends JFrame{

public static final Color COLOR_KEY = new Color(220, 110, 0);

// Initialize all the objects
public static JFrame window = new JFrame("Minesweeper++");
public static JPanel menu = new JPanel();

// Close the current window
public static void close()
{
    window.setVisible(false);
    window.dispose();
}



}

我无法让我的图像显示在框架中。我已经尝试了所有方法,但我觉得这是一个错误,因为我是 Java Swing 的新手,所以我没有意识到。任何帮助将不胜感激。

【问题讨论】:

  • 您检查过以确保 img != null 吗?在绘制之前,我会调试或使用打印语句进行检查,以确保图像设置正确。
  • new JButton("High Score and \nStatistics"); JButton 不支持换行符。可以使用 HTML 来获得换行符,但是禁用时按钮会看起来不对。

标签: java swing icons imageicon


【解决方案1】:

非常令人困惑的程序结构让你自己变得困难,我建议你简化一些事情

首先,您当前的 MinesweeperMenu 类无需扩展 MinesweeperPanel,而后一个类无需扩展 JFrame。然后你在其他地方有一个静态的 JFrame——JFrame 太多了,启动时,你试图在一个 JFrame 中显示你的图像,但显示另一个没有图片的 JFrame。您的程序只需要一个 JFrame,它可能应该被创建、填充其内容、打包并显示在一个地方,而不是像您正在做的那样分散在各处。

您正试图在paintComponent 覆盖中显示图片,但此方法永远不会被调用,因为您的类扩展了JFrame(最终)并且JFrame 没有此方法。您使用了正确的方法,但该类应该扩展 JPanel,并且您应该在paintComponent 方法块上方有一个@Override 注释,以确保您实际上覆盖了父方法。

你应该去掉这个程序中的所有静态的一切。这里唯一的静态应该是 main 方法,也许还有一些常量,但仅此而已。

这里有更多错误,我没有太多时间去检查所有这些错误。考虑从头开始,从小处着手,让小部分发挥作用,然后将它们加在一起。

例如,首先创建一个非常小的程序,尝试将图像读入 Image 对象,将其放入 ImageIcon,将 ImageIcon 放入 JLabel,并将 JLabel 显示在 JOptionPane 中,就这么简单,只是为了看看你能不能读入图片OK,比如这样的:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class TestImages {

   // *** your image path will be different *****
   private static final String IMG_PATH = "src/images/image01.jpg";

   public static void main(String[] args) {
      try {
         BufferedImage img = ImageIO.read(new File(IMG_PATH));
         ImageIcon icon = new ImageIcon(img);
         JLabel label = new JLabel(icon);
         JOptionPane.showMessageDialog(null, label);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

然后,当您完成此操作后,看看您现在是否可以创建一个 JPanel,在其 paintComponent 方法中显示相同的 Image,并在 JOptionPane 中显示此 JPanel。

然后创建一个 JFrame 并在 JFrame 中显示保存图像的 JPanel。

通过连续迭代,您将测试概念、纠正错误并构建程序。

【讨论】:

  • 您的程序结构非常混乱,这让您自己感到困难,我建议您将事情简化很多。 ...似乎是人生的教训... :)
【解决方案2】:
File input = new File("./setup/images/MineMenuPicture.jpg");

如果MineMenuPicture.jpg是应用程序资源,它应该在一个Jar中,并通过从Class.getResource(String)获取的URL访问。

【讨论】:

    猜你喜欢
    • 2021-06-08
    • 1970-01-01
    • 2014-12-18
    • 2015-05-16
    • 2017-05-20
    • 1970-01-01
    • 2010-10-09
    • 2010-09-07
    相关资源
    最近更新 更多