2013-05-05 127 views
0

我不知道如何去制作一个图片库像这样的:平铺图像库中的Java

enter image description here

所有我需要的是让它在一个5x5的方形像上面有显示图像点击事件(我知道如何做ActionEvents)

我试着制作一个GridLayout与构造函数5, 5,然后添加图像与panel.add(image, 0, 0);等,但无济于事。下面是代码:

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

而这就是我得到:

enter image description here

谢谢你们!

回答

1

如果你要做布局,而不是BorderLayout,做GridLayout。它从字面上以网格的方式设置屏幕上的项目。只需设置面板的布局如下:

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

并且应该创建您正在查找的输出。希望这可以帮助!

编辑:

你可以只使用基本面板,并添加一个JButton代替的JLabel。并有图像出现,只是做:

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

不用担心把图像在特定地点(当然,除非你有一个原因),但该程序将已入图像正确的位置,并按照您将它们添加到面板的顺序进行放置,因此担心将它们放入特定位置是浪费时间。希望这可以帮助你!

+0

错字,这是一个网格布局;) – nrubin29 2013-05-05 22:10:56

+0

那么究竟什么是你找谁? – user2277872 2013-05-05 22:12:08

+0

要在示例图像中显示图像的方式。 – nrubin29 2013-05-05 22:15:04

1

的问题是与使用add方法

而不是

imagepanel.add(image, 0, 0); 
    imagepanel.add(image1, 1, 0); 
    imagepanel.add(image2, 1, 1); 
    imagepanel.add(image3, 2, 0); 

尝试

imagepanel.add(image); 
    imagepanel.add(image1); 
    imagepanel.add(image2); 
    imagepanel.add(image3); 

的图像将它们添加

的顺序排列你没有显示它,但我建议使用JFrame#pack以调整框架与例如

根据你后,你可以给更多的重量,以行或列是通过设置其中的一个作用更新其内容

的首选大小0。例如,如果要按行先(即从左到右,从上到下)排列组件,则将rows参数设置为0将使列具有更多的权重/优先级...

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridLayout; 
import java.io.File; 
import java.io.FileFilter; 
import java.io.IOException; 
import java.util.Arrays; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestGrid03 { 

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

    public TestGrid03() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridLayout(0, 5, 5, 5)); 
      File[] files = new File("\path\to\your\images").listFiles(new FileFilter() { 
       @Override 
       public boolean accept(File pathname) { 
        String name = pathname.getName().toLowerCase(); 
        return pathname.isFile() && 
          name.endsWith(".png") || 
          name.endsWith(".jpg"); 
       } 
      }); 
      Arrays.sort(files); 
      int count = 0; 
      while (count < 6 && count < files.length) { 
       try { 
        System.out.println(count + "; " + files[count]); 
        add(new JLabel(new ImageIcon(ImageIO.read(files[count])))); 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } 
       count++; 
      } 
     } 
    } 

} 
+0

这是否适合您?我在我自己的PC上用512x512的图像尝试了它,我只得到一列。 – arynaq 2013-05-05 22:27:14

+0

首先,我不会使用512x512图片,布局管理器正在尽最大努力来满足您的需求,但可用空间不可用,它会尝试其他方法。其次,你如何创建/显示窗口?你有没有尝试将imagePanel添加到JScrollPane? – MadProgrammer 2013-05-05 22:32:18

+0

扩展一个JPanel,使用相同的图像(现在为100x100)添加一个新的JLabel四次,将该面板添加到JFrames内容中,然后将其打包。 – arynaq 2013-05-05 22:35:24