在Java Swing中显示图像。

14
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吗?我建议在绘制之前进行调试或添加一个打印语句来确保图像被正确设置。 - Dan W
new JButton("最高分和\n统计数据"); JButton不支持换行。可以使用HTML来实现换行,但是当按钮被禁用时,按钮的外观会出现问题。 - Andrew Thompson
2个回答

40
你的程序结构非常混乱,这会让任务变得更加困难。我建议你大大简化它。
首先,你的MinesweeperMenu类不需要扩展MinesweeperPanel类,而后者也不需要扩展JFrame类。然后你在其他地方有一个静态的JFrame -- 这太多了,而且你试图在其中一个JFrame中显示图片,但却显示了另一个没有图片的JFrame。你的程序只需要一个JFrame,它应该在一个地方创建、填充和显示,而不是像现在这样到处零散。
你正在尝试在paintComponent重写中显示图片,但由于你的类(最终)扩展自JFrame,所以这个方法永远不会被调用。你使用了正确的方法,但类应该扩展JPanel,并且你应该在paintComponent方法块上方有一个@Override注释,以确保你实际上覆盖了父类的方法。
你应该摆脱此程序中所有静态内容。这里唯一静态的东西应该是main方法和一些常量,但也只限于此。
这里还有更多错误,而我时间太少,无法全部解释。考虑从头开始,从小处着手,先让小的部分工作起来,然后再把它们组合在一起。
例如,首先创建一个非常小的程序,尝试将图像读入Image对象中,将它放入ImageIcon中,将ImageIcon放入JLabel中,并在JOptionPane中显示JLabel,只要看看你能否正确读取图像即可。如下所示:
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方法中显示相同的图像,并在JOptionPane中显示此JPanel。

然后创建一个JFrame,并在其中显示持有图像的JPanel

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


你的程序结构非常混乱,这让你自己变得很困难。我建议你大大简化事情。 - Akhil Jain

7
File input = new File("./setup/images/MineMenuPicture.jpg");

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接