繁体   English   中英

无法让图像显示在秋千上

[英]Can't get image to show up in swing

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

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
        }

你可以试试这个

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

您必须按照Manos的说明将创建的ImageIcons添加到面板中,并将其作为Eclipse项目src文件夹中的图像,执行以下操作:

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

如果资源与应用程序一起嵌入(在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");
}

您从未在框架中添加任何东西-这会导致您的问题。 因此,在您的createWindow方法中,您需要调用:

aWindow.setContentPane(panel);

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

panel.add(empty);

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

其他有用的东西:

  • 通常,在有意义的情况下,请在构造函数中创建/初始化您的对象,然后在运行时中添加/删除/更新它们。
  • 若要进行故障排除,请在要查看的内容上使用.setOpaque(true).setBackground(Color.blue)方法。 如果您没有看到它,则可能是某种东西掩盖了它,或者从未添加过它

祝好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM