繁体   English   中英

ImageIcon不能设置为JButton?

[英]ImageIcon cannot be set to JButton?

我在程序中使用了此过程来制作JButton的数组。 JButtons确实显示在框架中,但是ImageIcon没有放入按钮中。 图片与程序位于同一目录。 问题是什么? (空间是我的框架)

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);
}

据我所知,您的错误是由于您读取图像的方式引起的。

在这种情况下,我使用了这个问题的图像。

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

例如:

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

由于每个按钮使用相同的图像(每个按钮可能使用相同的大小),因此建议您使用GridLayout而不是GridBagLayout

您可以复制粘贴此代码而无需进行修改,这被称为“ 最小,完整和可验证的示例(MCVE)”或“ 简短,自包含,正确的示例(SSCCE)” ,下次您应发布一个类似的示例 ,因此我们没有编写导入或推断该field是一个JButton数组而不是另一个JComponent (例如,一个可能更适合field名称的JTextField

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方法,建议您改为创建类的实例,然后以这种方式调用该方法(不带static修饰符)。

  2. 从显示的代码中,我推断出您没有将程序放在事件分发线程(EDT)上 ,这可能会由于线程问题而导致您出现问题,您可以像在main方法中那样,通过调用SwingUtilities#invokeLater()进行修复SwingUtilities#invokeLater()方法。

  3. 您的变量命名很混乱,您将space调用给JFrame ,如果您是我,我将其称为framespaceFrame field我称fieldJTextField不是JButton阵列,我会调用JButton阵列buttons (在复数它指的是其中许多)或fieldButtons

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

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

暂无
暂无

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

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