[英]Tiled Image Gallery in Java
I was wondering how to go about making an image gallery like this one: 我想知道如何制作像这样的图像库:
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) 我需要的是它以5x5的方格显示图像,每个图像都有一个点击事件(我知道怎么做ActionEvents)
I tried making a GridLayout
with the constructor 5, 5
, then adding images with panel.add(image, 0, 0);
我尝试使用构造函数
5, 5
创建GridLayout
,然后使用panel.add(image, 0, 0);
添加图像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);
And this is what I get: 这就是我得到的:
Thanks guys! 多谢你们!
If you are going to be doing layouts, instead of BorderLayout, do GridLayout. 如果您要进行布局而不是BorderLayout,请执行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!
希望这可以帮助!
EDIT: 编辑:
You can just use the BASE panel, and add a JButton instead of JLabels. 您只需使用BASE面板,然后添加JButton而不是JLabel。 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!
希望这对你有所帮助!
The problem is with the use of the add
method 问题在于使用
add
方法
Instead of 代替
imagepanel.add(image, 0, 0);
imagepanel.add(image1, 1, 0);
imagepanel.add(image2, 1, 1);
imagepanel.add(image3, 2, 0);
Try 尝试
imagepanel.add(image);
imagepanel.add(image1);
imagepanel.add(image2);
imagepanel.add(image3);
The images will laid out in the order they are added 图像将按照添加顺序排列
You've not shown it, but I'd recommend using JFrame#pack
to resize the frame to the preferred size of its content 你没有展示它,但我建议使用
JFrame#pack
将框架的大小调整为其内容的首选大小
Updated with example 更新了示例
Depending on the effect you're after, you can give more weight to either rows or columns by setting one of them to 0
. 根据您所使用的效果,您可以通过将其中一个设置为
0
来为行或列赋予更多权重。
For example, if you want to layout components by row first (ie left to right, top to bottom), setting the rows
parameter to 0
will give the columns more weight/precedence... 例如,如果要先按行布局组件(即从左到右,从上到下),将
rows
参数设置为0
将使列更多权重/优先级...
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++;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.