1

我是一名大学生,这是我第一次用 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
        }
4

4 回答 4

2

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

java.net.URL url = getClass().getResource("red.JPEG");
ImageIcon red = new ImageIcon(url);
于 2012-10-24T01:31:12.197 回答
2

你可以试试这个

BufferedImage myPicture = ImageIO.read(new File("path-to-file"));
JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
add( picLabel );
于 2012-10-24T01:08:16.780 回答
2

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

加载图像的首选机制是通过ImageIOAPI。它支持更多的图像格式(以及提供可插拔的架构)并保证一旦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");
}
于 2012-10-24T01:39:06.050 回答
1

你从来没有在你的框架中添加任何东西——这导致了你的问题。所以在你的createWindow方法中,你需要调用:

aWindow.setContentPane(panel);

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

panel.add(empty);

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

其他有用的东西:

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

祝你好运。

于 2012-10-24T04:36:04.680 回答