【问题标题】:Can't get image to show up in swing无法让图像显示在摇摆中
【发布时间】:2012-10-24 00:56:43
【问题描述】:

我是一名大学生,这是我第一次用 Java 创建 gui。现在我查看了这个答案GUI in Java using Swing 并按照说明进行操作,但仍然没有任何反应。这是代码。我删除了所有不相关的垃圾。

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Lab4Shell 
{
    // this holds the current game board
        private char[][] gameBoard = new char[7][8];
        private JButton[][] gameButtons = new JButton[7][8];

        private ImageIcon red = new ImageIcon("Red.jpg");
        private ImageIcon black = new ImageIcon("Black.jpg");
        private ImageIcon empty = new ImageIcon("Empty.jpg");

        private JPanel panel = new JPanel();

        private int currentPlayer = 1;
        private int numMoves = 0;
        //Why do you want everything in constructor?
        public Lab4Shell()
        {
            CreateWindow();
            ResetGame();



            // set layout
            // loop through buttons array creating buttons, registering event handlers and adding buttons to panel
            // add panel to frame
            // do other initialization of applet
        }

        public static void CreateWindow()
        {
            //Sets window title and create window object.
            JFrame aWindow = new JFrame("Connect Four");
            //Set window position and size
            aWindow.setBounds(500,100,400,400);
            //What close button does.
            aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Make window visible.
            aWindow.setVisible(true);


        }

        public static void main(String args[])
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {

                    Lab4Shell game = new Lab4Shell();

                }
            });


        }
void ResetGame()
        {



            JLabel label = new JLabel();
            label.setIcon(empty);
            for(int r=0;r<gameBoard.length;r++)
            {
                java.util.Arrays.fill(gameBoard[r],0,gameBoard[r].length,'0');



                //loop through board columns
                for(int c=0;c<gameBoard[r].length;c++)
                {

                }

            }
            // loop through array setting char array back to ' ' and buttons array back to empty pic
            // reset currentPlayer and numMoves variables
        }

【问题讨论】:

  • 我使用的是 Eclipse,图像在程序的 bin 文件夹中。
  • 进行实验,可以使用UI图标,如图here
  • @redelman431 :如果您手动操作,请查看如何Add Images to your Java Project 和此answer 进一步说明。

标签: java swing embedded-resource


【解决方案1】:

您必须按照 Manos 所说的将创建的 ImageIcons 添加到面板中,并且作为 Eclipse 项目的 src 文件夹中的图像,这样做:

java.net.URL url = getClass().getResource("red.JPEG");
ImageIcon red = new ImageIcon(url);

【讨论】:

  • 但是上面那个 ImageSource 实例变量呢?我的教授希望我使用这些。我应该把它放在构造函数中吗?但我会试试你的想法
  • 是的,试一试,你会发现它会起作用。你可以像这样放入构造函数: ImageIcon image = new ImageIcon(getClass().getResource("/red.JPEG"));
【解决方案2】:

如果资源嵌入到应用程序中(在 jar 中),您需要使用Class#getResource 来加载它们。

加载图像的首选机制是通过ImageIO API。它支持更多的图像格式(以及提供可插拔的架构),并保证在read 方法返回时准备好显示的图像

BufferedImage redImage;

// ... 

URL url = getClass().getResource("red.JPEG");
if (url != null) {
    redImage = ImageIO.read(url);
} else {
    throw new NullPointerException("Unable to locate red image resource");
}

【讨论】:

    【解决方案3】:

    你可以试试这个

    BufferedImage myPicture = ImageIO.read(new File("path-to-file"));
    JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
    add( picLabel );
    

    【讨论】:

    • +1 为修订版;另见example
    • add 来自哪个类?我的编译器不喜欢它。
    【解决方案4】:

    您从未在框架中添加任何内容 - 这导致了您的问题。所以在你createWindow方法中,你需要调用:

    aWindow.setContentPane(panel);
    

    然后稍后(例如在您的 resetGame 方法中),您将添加您的内容(例如 JLabel)到面板:

    panel.add(empty);
    

    添加到面板的位置由面板的 LayoutManager 决定(其中有很多 - 默认为 BorderLayout

    其他有用的东西:

    • 通常情况下,在有意义的情况下,在构造函数中创建/初始化您的对象并在运行时添加/删除/更新它们。
    • 要进行故障排除,请使用.setOpaque(true).setBackground(Color.blue) 方法处理您想要查看的内容。如果你当时没有看到它,要么是有什么东西在掩盖它,要么是从未添加过

    祝你好运。

    【讨论】:

    • 非常感谢!你的英雄。
    猜你喜欢
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2021-07-20
    相关资源
    最近更新 更多