java - 无法让图像显示在 Swing 中

标签 java swing embedded-resource

我是一名大学生,这是我第一次用 Java 创建图形用户界面。现在我看着这个答案 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
        }

最佳答案

您必须像 Manos 所说的那样将创建的 ImageIcons 添加到面板,并且作为 Eclipse 项目的 src 文件夹中的图像,执行此操作:

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

关于java - 无法让图像显示在 Swing 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13041554/

相关文章:

java - 是否有用于更改目录的 JVM 命令行选项?

MenuItem/MenuBar 和声音播放器的 Java 退出按钮

java - 如何从返回两个不同值的 swingworker 更新 GUI

java - 加载图像时遇到问题

c# - 如何获取资源文件的路径?

java - 从资源中的图像设置图标图像

java - 创建数组的新实例时程序卡住

java - 将 3D 世界(arcore anchor/pose)转换为其对应的 2D 屏幕坐标

java - AWT 中的 Java 线程池和 Java EventQueue 背后的概念是否有相似之处?

java - java中如何优雅地包装重载方法?