Java平铺图像库 [英] Tiled Image Gallery in Java

查看:83
本文介绍了Java平铺图像库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何制作像这样的图片库:

I was wondering how to go about making an image gallery like this one:

我需要做的是像上面一样在5x5的正方形中显示图像,并为每个图像单击事件(我知道该怎么做ActionEvents)

All I would need is for it to display images in a 5x5 square like above with a click event for each image (I know how to do ActionEvents)

我尝试使用构造函数5, 5创建一个GridLayout,然后使用panel.add(image, 0, 0);添加图像,依此类推,但无济于事.这是代码:

I tried making a GridLayout with the constructor 5, 5, then adding images with panel.add(image, 0, 0); and so on, but to no avail. Here's that code:

        imagelayout = new GridLayout(5, 5, 5, 5);

        imagepanel = new JPanel(imagelayout);

        JLabel image = new JLabel(LogoManager.getInstance().getLogo("Apple"));
        JLabel image1 = new JLabel(LogoManager.getInstance().getLogo("McDonalds"));
        JLabel image2 = new JLabel(LogoManager.getInstance().getLogo("Fox"));
        JLabel image3 = new JLabel(LogoManager.getInstance().getLogo("Microsoft"));
        imagepanel.add(image, 0, 0);
        imagepanel.add(image1, 1, 0);
        imagepanel.add(image2, 1, 1);
        imagepanel.add(image3, 2, 0);

这就是我得到的:

谢谢大家!

推荐答案

如果要进行布局,请使用GridLayout代替BorderLayout.它实际上以网格方式在屏幕上设置了项目.只需这样设置面板的布局即可:

If you are going to be doing layouts, instead of BorderLayout, do GridLayout. It literally sets up the items on your screen in a grid fashion. Just set the panel's layout as so:

    panel.setLayout(new GridLayout(5,5));

,这将创建您要查找的输出.希望这会有所帮助!

and that should create the output you are looking for. hope this helps!

您可以只使用BASE面板,并添加一个JButton而不是JLabels.要显示图像,只需执行以下操作:

You can just use the BASE panel, and add a JButton instead of JLabels. And to have the images appear, just do:

  JButton image1 = new JButton(new ImageIcon(//apple logo));
  JButton image2 = new JButton(new ImageIcon(//next logo));      
  JButton image3 = new JButton(new ImageIcon(//next logo));     
  JButton image4 = new JButton(new ImageIcon(//next logo));
  JButton image5 = new JButton(new ImageIcon(//next logo));

  panel.setLayout(new GridLayout(5,5));
  panel.add(image1);
  panel.add(image2);
  panel.add(image3);
  panel.add(image4);
  panel.add(image5);

不必担心将图像放置在特定位置(当然,除非您有此理由),但是程序已经将图像放置在正确的位置,并且可以通过将它们添加到正确的位置来放置它们面板,所以浪费时间担心将其放置在特定位置.希望对您有帮助!

don't worry about putting the images in specific spots (unless of course you have a reason for that), but the program will already put the images in the correct spot, and in the order you place them by adding them onto the panel, so it's a waste of time to worry about putting them in the specific spot. Hope this helps you!

这篇关于Java平铺图像库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆