ImageIcon不能设置为JButton?

ImageIcon cannot be set to JButton?

我在一个程序中使用这个过程来制作一个 JButton 数组。 JButtons 确实出现在框架中,但 ImageIcon 没有放入按钮中。图片和程序在同一个目录下。问题是什么? (space是我的框架)

static void BasicSetup() {
    int count1 = 0;
    int count2 = 0;
    ImageIcon cell = new ImageIcon("cell.png");

    for (int y = 0; y < 16; y++) {
        count1 = count1 + 1;
        count2 = 0;
        for (int x = 0; x < 16; x++) {
            count2 = count2 + 1;
            field[y][x] = new JButton();
            field[y][x].setIcon(cell);
            constraints.gridx = count1;
            constraints.gridy = count2;
            constraints.weightx = 1;
            jpanel.add(field[y][x], constraints);

        }
    }
    space.add(jpanel);
}

据我所知,您的错误是由于您阅读图片的方式所致。

在这种情况下,我使用了来自 question 的图像。

当您将应用程序打包为 JAR 文件时,您无论如何都需要将图像作为资源进行访问,因此,最好现在就开始以这种方式访问​​它们。此时你的代码直接从你的文件系统加载图像而不是作为资源。您可以通过调用 ImageIO.read() 方法来更改它(链接的方法用于 URL,由于链接到上面链接的问题的图像,我正在使用它,但您可以通过 FileInputStreamImageInputStream

例如:

img = ImageIO.read(getClass().getResource("L5DGx.png"));

由于您为每个按钮使用相同的图像(并且可能每个按钮的大小相同),我建议您使用 GridLayout instead of GridBagLayout.

你可以 copy-paste 这段代码不加修改,这叫做 Minimal, Complete and Verifiable Example (MCVE) or a Short, Self Contained, Correct Example (SSCCE) 下次你应该 post 一个类似的,所以我们不必编写导入或推断 field 是一个 JButton 数组而不是另一个 JComponent (例如 JTextField 可能更适合 field 的名称)

import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ImageIconArray {

    private JButton[][] buttons = new JButton[16][16];
    private JPanel pane;
    private JFrame frame;
    private GridBagConstraints constraints = new GridBagConstraints();

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ImageIconArray().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        BufferedImage img = null;
        URL url;
        try {
            url = new URL("https://i.stack.imgur.com/L5DGx.png");
            img = ImageIO.read(url);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        Icon cell = new ImageIcon(img);
        frame = new JFrame("Example");
        pane = new JPanel();
        pane.setLayout(new GridLayout(16, 16));

        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 16; j++) {
                buttons[i][j] = new JButton();
                buttons[i][j].setIcon(cell);
                constraints.gridx = i;
                constraints.gridy = j;
                constraints.weightx = 1;
                pane.add(buttons[i][j], constraints);

            }
        }
        frame.add(pane);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

这是我在 运行 上述代码时得到的输出图像:


其他提示

  1. 您正在为创建 GUI 创建一个 static 方法,我建议您改为创建 class 的一个实例并以这种方式调用该方法( w/o static 修饰符)。

  2. 根据显示的代码,我推断您没有将程序放在 Event Dispatch Thread (EDT), this might cause you problems due to Threading issues, you can fix it the way I did in the main method, calling SwingUtilities#invokeLater() 方法上。

  3. 你的变量命名很乱,你把space叫做JFrame,如果我是你我会叫它framespaceFrame . field 我会将 field 称为 JTextField 而不是 JButton 数组,我会将 JButton 数组称为 buttons(复数形式指的是其中的许多)或 fieldButtons.

  4. 你的 for 循环(在我看来)是颠倒的,我会从 x 开始然后继续 y (或者像每个人一样做在简单循环中使用 ijk 作为计数器变量,或者让它们具有更具描述性的名称)

  5. 您正在使用 count1count2 变量,这不是必需的,您可以使用 constraints.gridx = i;constraints.gridy = j;(如果您更喜欢使用 ij 作为上面推荐的 for 循环中的计数器变量)或 constraints.gridx = y;constraints.gridy = x;(如果你决定忽略我的提示# 4) 或 constraints.gridx = x;constraints.gridy = y;(如果你决定接受我的小费#4)