图像图标未显示在JPanel中

时间:2015-05-06 22:20:02

标签: java swing intellij-idea jpanel imageicon

代码非常简单。我从arraylist获取了一个URL字符串。然后我尝试在我的JPanel中绘制图像。如果我没有在代码中初始化布局,则会返回错误。如果我初始化布局,则没有错误,但图标也不显示。我错过了什么吗?谢谢!

public class CharacterPage extends JFrame {
   private JPanel imagesPanel;
   private WikiDB wikiDB;

     public CharacterPage(WikiDB db) throws IOException {
          super("Character Page");
          setContentPane(rootPanel);
          pack();
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          setVisible(true);
          setSize(new Dimension(500,500));
          imagesPanel.setLayout(new BorderLayout());
          this.wikiDB = db;

               searchButton.addActionListener(new ActionListener() {
               @Override
               public void actionPerformed(ActionEvent e) {
               ArrayList<String> characterImages =    
                wikiDB.searchImages(characterID);
              //array contains this string: http://cosplayidol.otakuhouse.com/wp-content/uploads/2012/06/s-1-1.jpg

                for (int x = 0; x < characterImages.size(); x++){
                    String imageURL = characterImages.get(x);
                    Graphics g = getGraphics();


                   try {
                       URL url = new URL(imageURL);

                       //icon.paintIcon(imagesPanel,g,300,100);

                       JLabel wIcon = new JLabel(new ImageIcon(url));

                       imagesPanel.setVisible(true);
                       imagesPanel.add(wIcon);


                   } catch (MalformedURLException mue){
                       mue.printStackTrace();
                   }
            }
      });
}

下面是我用来读取网址的单独程序。使用下面的代码,我可以在不返回任何错误的情况下读取我的URL,但它仍然没有在GUI中显示。

public class SimpleGUI extends JFrame {
private JPanel imagesPanel;

public SimpleGUI() throws IOException {
    super("GUI Page");
    setContentPane(imagesPanel);
    pack();
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setVisible(true);
    setSize(new Dimension(500, 500));
    imagesPanel.setLayout(new GridLayout());


    BufferedImage img1 = null;

    try
    {
        URL url1 = new URL("http://cosplayidol.otakuhouse.com/wp-content/uploads/2012/06/s-1-1.jpg");

        URLConnection conn1 = url1.openConnection();
        conn1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
        InputStream in1 = conn1.getInputStream();

        img1 = ImageIO.read(in1);
    } catch (IOException e)
    {
        e.printStackTrace();
    }


    JLabel wIcon = new JLabel(new ImageIcon(img1));
    imagesPanel.add(wIcon);

   }
}

编辑:我越来越接近了,我现在可以使用此代码弹出一个包含图片的单独框架:

public class SimpleGUI extends JFrame {
private JPanel imagesPanel;
private JFrame mainFrame;

public SimpleGUI() throws IOException {
    super("GUI Page");
    setContentPane(imagesPanel);
    pack();
    setVisible(true);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setSize(new Dimension(500, 500));

    mainFrame = new JFrame();

    imagesPanel.setLayout(new GridLayout());
    imagesPanel.setBounds(0,0,200,200);
    mainFrame.add(imagesPanel);
    mainFrame.setVisible(true);

我似乎无法在我的GUI表单中找到原始帧,因此我可以将imagesPanel添加到其中。我正在使用IntelliJ的GUI表单构建器。

1 个答案:

答案 0 :(得分:3)

有一个问题,你正试图在for循环中使用JPanel将多个组件添加到BorderLayout,并且通过这样做,你将覆盖之前添加的所有新内容。使用更合适的布局,例如GridLayout。

另一个问题,你在哪里添加imagesPanel?在其上调用setVisible(true)将无济于事,但将其添加到JFrame或最终由JFrame持有的JPanel将会做很多事情。

此外,您需要在到来之前通常进行一些严肃的调试 - 您对图像的阅读是否有效?创建一个简单的GUI,一个读入Image(使用ImageIO.read(URL url)),创建一个ImageIcon并在JOptionPane中显示Icon。如果这样可行,则问题出在其他地方。如果它没有修改URL地址。