Bagaimana untuk Menambah Lakaran Kecil Imej pada Reka Letak dalam Grid?
Soalan ini melihat grid SpringLayout untuk menyimpan satu siri lakaran kecil imej dalam JScrollPane. Khususnya, cara mengendalikan reka letak apabila saiz senarai foto mungkin besar.
Jadi pada asasnya, anda memerlukan sejenis bekas yang tinggal dalam anak tetingkap tatal (biasanya dikenali sebagai paparan). Untuk ini, anda harus menambah imej anda.
Dalam contoh kod Java berikut, foto tersebut disiarkan pada JPanel, dan itu terletak dalam JScrollPane:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt. image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
Grid Imej kelas awam {
// lebar int = img.getWidth();public static void main(String[] args) { new ImageGrid(); } public ImageGrid() { 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 { private JPanel imagesPane; public TestPane() { setLayout(new BorderLayout()); imagesPane = new JPanel(new WrapLayout()); add(new JScrollPane(imagesPane)); JButton scan = new JButton("Scan"); scan.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String path = "C:\Users\shane\Dropbox\Ponies"; File[] files = new File(path).listFiles(new FileFilter() { @Override public boolean accept(File pathname) { String name = pathname.getName().toLowerCase(); return pathname.isFile() && (name.endsWith(".png") || name.endsWith(".jpg") || name.endsWith(".gif")); } }); imagesPane.removeAll(); for (File file : files) { try { ImagePane pane = new ImagePane(file); imagesPane.add(pane); } catch (Exception exp) { exp.printStackTrace(); } } imagesPane.revalidate(); imagesPane.repaint(); } }); add(scan, BorderLayout.SOUTH); } } public class ImagePane extends JPanel { private Image img; public ImagePane(File source) throws IOException { img = ImageIO.read(source); if (img.getWidth(this) > 200 || img.getHeight(this) > 200) { int width = img.getWidth(this); int height = img.getWidth(this); float scaleWidth = 200f / width; float scaleHeight = 200f / height; if (scaleWidth > scaleHeight) { width = -1; height = (int)(height * scaleHeight); } else { width = (int)(width * scaleWidth); height = -1; } img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH); } } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); if (img != null) {
// float scale = 1f;
// AffineTransform at = new AffineTransform();
// at .translate(
// (getWidth() / 2) - ((img.getWidth() * skala) / 2),
// (getHeight() / 2) - ((img.getHeight() * skala) / 2));
// at.scale(skala, skala);
// g2d.setTransform(at);
Atas ialah kandungan terperinci Bagaimana untuk Memaparkan Sebilangan Besar Imej Kecil Imej dalam Susun Atur Grid Java dalam JScrollPane dengan Cekap?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!