2017-01-18 35 views
0

我在一个程序中使用这个过程来创建一个数组JButton s。 JButtons显示在框架中,但ImageIcon未放入按钮中。该图片与程序位于同一目录中。问题是什么? (空间是我的框架)ImageIcon不能设置为JButton吗?

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

为了更好地帮助越早请张贴有效的[MCVE] – Frakcool

+1

这应该起作用。你可以确定“cell.png”是否真的存在你执行这个程序的目录吗?为了调试,只需给出完整的路径,然后尝试。 – VHS

+0

检查'cell.getIconHeight()'的结果,如果它是-1图像未找到/加载 –

回答

2

从我看到的,你的错误是由于你阅读图像的方式。

在这种情况下,我用这个question图像。

将应用程序打包为JAR文件时,无论如何您都需要以图片资源的形式访问图片,所以最好现在开始访问它们。在这一刻,你的代码直接从文件系统中加载图像,而不是作为资源。你可以通过调用ImageIO.read()方法来改变它(链接的一个是我使用的是由于链接到上面链接的问题的图像其中URL,但您可以通过FileInputStreamImageInputStream过阅读。

对于例如:

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

当你使用相同的图像的每个按钮(也可能为每个按钮的大小相同),我推荐你使用GridLayout,而不是GridBagLayout

可以复制。 - 无需修改即可刷新此代码,t他被称为Minimal, Complete and Verifiable Example (MCVE)Short, Self Contained, Correct Example (SSCCE),并且下一次您应该发布一个相似的,所以我们不必编写进口或推断fieldJButton而不是另一个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); 
    } 
} 

下面是运行上面的代码时,我得到了什么的输出图像:

enter image description here


其他提示

  1. 你让为创建GUI的static方法,我建议你,而不是创建类的实例,并调用方法方式(W/O的static修改) 。

  2. 从显示的代码,我推断你不把你的程序在Event Dispatch Thread (EDT),这可能导致你由于线程问题的问题,您可以修复它,我在main方法做的方式,呼吁SwingUtilities#invokeLater()方法。

  3. 你的变量命名为迷惑,你叫spaceJFrame,如果我是你,我会叫它framespaceFrame。我会打电话fieldJTextField而不是JButton阵列,我会称JButton阵列为buttons(复数,因为它指的是其中许多)或fieldButtons

  4. for环路(在我看来)倒,我就从x开始,然后继续y(或做大家都做,并使用ijk在简单的循环计数器变量,或使它们有更具描述性的名称)

  5. 您使用count1count2变量,这是没有必要的,你可以有你constraints.gridx = i;constraints.gridy = j;(如果你喜欢使用ij作为计数器变量您for循环为RECOM上述谁料)或constraints.gridx = y;constraints.gridy = x;(如果你决定要无视我的提示#4)或constraints.gridx = x;constraints.gridy = y;(如果你决定要我的小费4#)

+0

对于downvoter,你能解释为什么吗? – Frakcool