如何將可捲動影像縮圖整合到 SpringLayout 網格中?
將圖像縮圖加入網格佈局
挑戰:
挑戰:
解決方案:
要在SpringLayout 中顯示縮圖網格,您需要實作一個自訂容器。這是改進的回應:
改進的回應:
- 您可以建立一個自訂容器來在 SpringLayout 中顯示縮圖網格。這是一個增強的解決方案:
- 建立自訂面板:
- 建立一個名為ImagePanelel 的自訂Jel 子類。
- 實作preferredLayoutSize()根據面板的數量和大小計算首選尺寸縮圖。
重寫paintComponent() 以網格圖案繪製縮圖。 -
- 新增自訂面板:
- 將ImageGridPanel 執行個體新增至JPanel
將ImageGridPanel 執行個體加到JPanel -
將ImageGridPanel 執行個體加約來定位ImageGridPanel 並調整其大小。
- 新增縮圖:
建立ImagePane元件來表示每個縮圖影像。
將 ImagePane 元件加入 ImageGridPanel。
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ImageGrid { public static void main(String[] args) { // Create the frame and panel. JFrame frame = new JFrame("Image Grid"); JPanel panel = new JPanel(); frame.add(panel, BorderLayout.CENTER); // Create the custom image grid panel. ImageGridPanel imageGridPanel = new ImageGridPanel(); panel.add(imageGridPanel, BorderLayout.CENTER); // Add thumbnails to the image grid panel. for (int i = 0; i < 10; i++) { imageGridPanel.addImage(new ImageIcon("image" + i + ".png")); } // Set the frame properties. frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(600, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); } static class ImageGridPanel extends JPanel { private List<ImageIcon> images; public ImageGridPanel() { setBackground(Color.WHITE); setLayout(null); images = new ArrayList<>(); } public void addImage(ImageIcon image) { images.add(image); invalidate(); repaint(); } @Override public Dimension getPreferredSize() { int numImages = images.size(); int numRows = (int) Math.ceil(Math.sqrt(numImages)); int numCols = (int) Math.ceil(numImages / numRows); int width = numCols * 100; int height = numRows * 100; return new Dimension(width, height); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int numImages = images.size(); int numRows = (int) Math.ceil(Math.sqrt(numImages)); int numCols = (int) Math.ceil(numImages / numRows); int width = getWidth() / numCols; int height = getHeight() / numRows; for (int i = 0; i < numImages; i++) { ImageIcon image = images.get(i); int row = i / numCols; int col = i % numCols; g.drawImage(image.getImage(), col * width, row * height, null); } } } }
程式碼片段:
- 附加註意事項:
以上是如何將可捲動影像縮圖整合到 SpringLayout 網格中?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

在使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名以構建查詢條件,是一個常見的難題。本文將針...
