【问题标题】:How to add images to a JButton when the images are in an array当图像在数组中时如何将图像添加到 JButton
【发布时间】:2016-10-16 16:48:41
【问题描述】:

我正在尝试将图像添加到 Jbuttons。这些图像位于我使用单独的类创建的数组中。每次添加图像的尝试都失败了。我试图将它们变成 ImageIcon 并按原样添加它们,但它不起作用。第一组代码是我用来创建按钮并添加到 JFrame 的代码。我的目标是添加事件侦听器,以便在按下按钮时按钮将显示图像、矩形或带有文本的矩形

import java.awt.Dimension;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ButtonTest extends JFrame{


    public ButtonTest(){

          JPanel panel = new JPanel();
          JButton[] b = new JButton[9];
          Image[] imgs = MyImageSplit.splitter("dog", 9);


          for (int i=0; i<b.length; i++){
             b[i]=new JButton();
             b[i].setPreferredSize(new Dimension(100, 100));

             panel.add(b[i]);
          }

        add(panel);  
        setVisible(true);
        setSize(400,400);
      }

        public static void main(String[] args) {
            new ButtonTest();

        }
}

这段代码是我用来分割图像并将较小的图像添加到数组中的代码。

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import javax.imageio.ImageIO;

public class MyImageSplit {

    public static Image[] splitter (String filename, int numGroup){
        int rows=(int) Math.sqrt(numGroup);
        int cols=(int) Math.sqrt(numGroup);

        BufferedImage[] buffimage = new BufferedImage[rows*cols];
        Image[] images = new Image[rows*cols];

        try{
            File file = new File(filename + ".jpg"); //gets image from directory
            FileInputStream f = new FileInputStream(file);
            BufferedImage img = ImageIO.read(f); //reads the image file



            //determines the width and height for each piece
            int pieceWidth=img.getWidth()/cols;
            int pieceHeight = img.getHeight()/rows;


            for (int x = 0; x < rows; x++) {
                for (int y = 0; y < cols; y++) {
                    //Initialize the image array with image pieces
                    buffimage[x * rows + y] = new BufferedImage(pieceWidth, pieceHeight, img.getType());

                    // draws the image piece
                    Graphics2D gr = buffimage[x * rows + y].createGraphics();
                    gr.drawImage(img, 0, 0, pieceWidth, pieceHeight, pieceWidth * y, pieceHeight * x, pieceWidth * y + pieceWidth, pieceHeight * x + pieceHeight, null);
                    gr.dispose();
                }
            }

            for (int i =0; i < buffimage.length; i++){
                ImageIO.write(buffimage[i], "jpg", new File(filename + i + ".jpg"));

            }
        }

        catch (Exception e) {
            System.out.println(e.getStackTrace());
        }

        for(int i = 0; i < rows*cols; i++){
            images[i] = (Image) buffimage[i];

        }

        return images;
    }

    public static void main(String[] args){
        splitter("monkey", 9);
    }

}

dog.jpg

【问题讨论】:

  • 你检查过图片加载是否正确吗?尝试使用简单的代码在 JFrame 上使用带有 JLabel 的 ImageIcon。

标签: java arrays image swing


【解决方案1】:

您忘记为每个按钮添加图片。将此添加到您的for-loop

for (int i=0; i<b.length; i++){
    b[i]=new JButton();
    b[i].setPreferredSize(new Dimension(200, 100));
    b[i].setIcon(new ImageIcon(imgs[i]));
    panel.add(b[i]);
}

此外,我建议您更改JButton 首选尺寸,因为图片不适合它们。

b[i].setPreferredSize(new Dimension(200, 100));

还有JFrame的大小。

setSize(800,400);

编辑:使用.png 图像文件扩展名来正确保持颜色。 .jpg 造成一些颜色的损失简单说。比较:

dog.jpg

dog.png

【讨论】:

  • 图像已使用 MyImageSplit 类导入。该类返回图像数组。我需要从数组中提取图像,为什么我需要再次输入文件?
  • 是的,很抱歉,我忘记了那个变量。我已经编辑了我的答案。请再次检查for-loop 部分。还要检查图片的路径是否正确。
  • 谢谢。这是正确的。我不断跳转到不同的项目文件夹,但我当前正在处理的文件夹中没有图像。
【解决方案2】:

似乎您创建了按钮但从未设置它们的图像。

将以下行添加到ButtonTest 中的主循环:

b[i].setIcon(new ImageIcon(imgs[i]));

【讨论】:

  • 我之前试过这个,它没有运行。这是控制台中的内容:
  • [Ljava.lang.StackTraceElement;@27973e9b javax.swing.ImageIcon.(未知来源)在 ButtonTest.(ButtonTest .java:22) 在 ButtonTest.main(ButtonTest.java:35)
  • 您可能使用了错误的图像路径。请注意,由于您使用的是相对路径,因此图像必须位于当前工作目录中。
  • 谢谢。这是正确的。我不断跳转到不同的项目文件夹,但我当前正在处理的文件夹中没有图像
猜你喜欢
  • 2011-06-15
  • 2012-08-15
  • 2012-11-02
  • 2018-11-20
  • 2022-01-12
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
相关资源
最近更新 更多