在Java Swing中显示图像 [英] Displaying an image in Java Swing

查看:137
本文介绍了在Java Swing中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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的新手,所以我没有意识到这是一个错误。任何帮助将不胜感激。

I can't get my image to display in the frame. I've tried everything, but I'm getting the impression it's a mistake that I'm not realizing since I am new to Java Swing. Any help would be greatly appreciated.

推荐答案

通过 非常困扰自己 令人困惑的程序结构,我建议您简化 lot 的事情。

首先,没有必要为您当前的MinesweeperMenu类扩展MinesweeperPanel,并为后一类扩展JFrame。然后你在其他地方有一个静态JFrame - 这是太多的JFrame,并且要启动,你试图在一个JFrame中显示你的图像,但是显示另一个没有图片的那个。你的程序只需要一个JFrame,它应该可以创建,填充其内容,打包并显示在一个地方,而不是像你一样在这里散落。

For one, there's no need for your current MinesweeperMenu class to extend MinesweeperPanel, and for the latter class to extend JFrame. Then you have a static JFrame somewhere else -- that's too many JFrames, and to boot, you're trying to display your image in one JFrame but showing the other one that doesn't have the picture. Your program needs just one JFrame and it should probably be created, stuffed with its contents, packed and displayed in one place, not scattered here and there as you're doing.

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

You're trying to display the picture in a paintComponent override, but this method will never get called since your class extends JFrame (eventually) and JFrame doesn't have this method. You're using the right method, but the class should be extending JPanel, and you should have an @Override annotation above the paintComponent method block to be sure that you're actually overriding a parent method.

你应该摆脱 所有 静态这个程序中的一切。这里唯一静态的应该是main方法,也许是一些常量,但就是这样。

You should get rid of all static everything in this program. The only thing static here should be the main method and perhaps some constants, but that's it.

这里有更多的错误,我没有多少时间去讨论所有错误他们考虑从头开始,从小开始,让小位工作,然后将它们加在一起。

There are more errors here, and I have too little time to go over all of them. Consider starting from the beginning, starting small, getting small bits to work, and then adding them together.

例如,首先创建一个非常小的程序,试图读入将图像放入Image对象,将其放入ImageIcon,将ImageIcon放入JLabel,并在JOptionPane中显示JLabel,这很简单,只是为了看看你是否可以读取图像OK,例如,这样的事情: / p>

For instance, first create a very small program that tries to read in an image into an Image object, place it in a ImageIcon, place the ImageIcon into a JLabel, and display the JLabel in a JOptionPane, that simple, just to see if you can read in images OK, for example, something like this:

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();
      }
   }
}

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

Then when you've done this, see if you can now create a JPanel that shows the same Image in its paintComponent method, and display this JPanel in a JOptionPane.

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

Then create a JFrame and display the image-holding JPanel in the JFrame.

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

Through successive iterations you'll be testing concepts, correcting mistakes and building your program.

这篇关于在Java Swing中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆