我想知道如何制作像这样的图像库:
我需要的只是它以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);
这就是我得到的:
谢谢你们!
答案 0 :(得分:1)
如果您要进行布局而不是BorderLayout,请执行GridLayout。它实际上以网格方式设置屏幕上的项目。只需将面板的布局设置为:
panel.setLayout(new GridLayout(5,5));
这应该创建您正在寻找的输出。希望这有帮助!
编辑:
您可以使用BASE面板,添加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);
不要担心将图像放在特定的位置(当然除非你有理由),但是程序已经将图像放在正确的位置,并按照放置它们的顺序将它们添加到小组,因此担心将它们放在特定位置是浪费时间。希望这能帮到你!
答案 1 :(得分: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
将使列更具权重/优先级。
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++;
}
}
}
}