簡體   English   中英

java swing - 在同一個 jframe 上繪制多個 jpanel

[英]java swing - painting multiple jpanels on the same jframe

只是作為序言,我已經尋找了幾個小時試圖在此處和其他幾個站點上找到解決方案。 如果您發現我可能遺漏的問題,請告訴我。

Anywho,我正在嘗試創建一個顯示 4 個縮略圖(在 jpanels 中)和 4 個標題的縮略圖查看器。 我可以繪制所有 4 個縮略圖,但它們都是相同的圖像(最后一個繪制的副本)。 我認為這是我試圖重新粉刷它們的一部分,但我不知道要改變什么。 imageAlbum 是 jpg 路徑的 ArrayList。

在此處輸入圖片說明

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

//import MainFrame.ImageComponent;

import javax.swing.JLabel;

public class Thumbnails extends JFrame {
    final int IMG_WIDTH = 80;
    final int IMG_HEIGHT = 60;
    private BufferedImage image;
    private ImageAlbum imageAlbum;
    private JPanel contentPane;
    private JPanel thmbnl_1;
    private JPanel thmbnl_2;
    private JPanel thmbnl_3;
    private JPanel thmbnl_4;
    private JLabel thmbnl_1Label;
    private JLabel thmbnl_2Label;
    private JLabel thmbnl_3Label;
    private JLabel thmbnl_4Label;

    /**
     * Create the frame.
     */
    public Thumbnails(ImageAlbum album) {
        imageAlbum = album;
        String captionUnavailable = "Caption is not available";

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(new GridLayout(4, 2, 0, 0));

        thmbnl_1 = new JPanel();
        thmbnl_1.setPreferredSize(new Dimension(80, 60));
        panel.add(thmbnl_1);

        thmbnl_2 = new JPanel();
        thmbnl_2.setPreferredSize(new Dimension(80, 60));
        panel.add(thmbnl_2);

        thmbnl_1Label = new JLabel(captionUnavailable);
        panel.add(thmbnl_1Label);

        thmbnl_2Label = new JLabel(captionUnavailable);
        panel.add(thmbnl_2Label);

        thmbnl_3 = new JPanel();
        thmbnl_3.setPreferredSize(new Dimension(IMG_WIDTH, IMG_HEIGHT));
        panel.add(thmbnl_3);

        thmbnl_4 = new JPanel();
        thmbnl_4.setPreferredSize(new Dimension(IMG_WIDTH, IMG_HEIGHT));
        panel.add(thmbnl_4);

        thmbnl_3Label = new JLabel(captionUnavailable);
        panel.add(thmbnl_3Label);

        thmbnl_4Label = new JLabel(captionUnavailable);
        panel.add(thmbnl_4Label);

        setupThumbnails();
    }// end Thumbnails(ImageAlbum album)


    // 
    private void setupThumbnails() {
        int albumSize = imageAlbum.getSize();

        for(int i = 0; i < albumSize; i++) {
            try {           
                image = resizeToThumbnail(ImageIO.read(new File(imageAlbum.getAlbum(i))));

                switch(i) {
                    case 0:
                        thmbnl_1.setLayout(new BorderLayout());
                        thmbnl_1.add(new ImageComponent(image), BorderLayout.CENTER);
                        thmbnl_1Label.setText(imageAlbum.getCaption(i));
                        break;
                    case 1:
                        thmbnl_2.setLayout(new BorderLayout());
                        thmbnl_2.add(new ImageComponent(image), BorderLayout.CENTER);
                        thmbnl_2Label.setText(imageAlbum.getCaption(i));
                        break;
                    case 2:
                        thmbnl_3.setLayout(new BorderLayout());
                        thmbnl_3.add(new ImageComponent(image), BorderLayout.CENTER);
                        thmbnl_3Label.setText(imageAlbum.getCaption(i));
                        break;
                    case 3:
                        thmbnl_4.setLayout(new BorderLayout());
                        thmbnl_4.add(new ImageComponent(image), BorderLayout.CENTER);
                        thmbnl_4Label.setText(imageAlbum.getCaption(i));
                        break;
                    default:
                        break;
                }// end switch-case

                revalidate();
                repaint();              
            }// end try-block
            catch(IOException e) {
                e.printStackTrace();
            }// end catch-block
        }// end for-loop
    }// end setupCaptions()


    // 
    public BufferedImage resizeToThumbnail(BufferedImage original) {
        int type;
        BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, original.getType());
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(original, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
        g.dispose();
        g.setComposite(AlphaComposite.Src);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

        return resizedImage;
    }// end resizeToThumbnail(...)


    class ImageComponent extends JComponent {
        /**
         * Desc: constructor for ImageComponent
         * @param: BufferedImage img
         * @return: nothing
         */
        public ImageComponent(BufferedImage img) {
            image = img;
        }// end ImageComponent()


        /**
         * Desc: draws out the image to the panel
         * @param: Graphics g
         * @return: void
         */
        @Override
        public void paintComponent(Graphics g) {
            if(image == null)
                return;

            Graphics2D g2d = (Graphics2D) g;

            // draw the image
            g.drawImage(image, 0, 0, this);
            g.dispose();
        }// end paintComponents(Graphics g)
    }// end class ImageComponent
}// end class class Thumbnails

編輯

ImageAlbum 類:

import java.util.*;


public class ImageAlbum {
    private ArrayList imageAlbum;
    private ArrayList imageCaptions;
    private int size;


    /**
     * Desc: getter for album size
     * @param: none
     * @return: int
     */
    public int getSize() {
        return size;
    }// end getSize()


    /**
     * Desc: getter for the image
     * @param: int index
     * @return: String
     */
    public String getAlbum(int index) {
        return imageAlbum.get(index).toString();
    }// end getAlbum(int index)



    /**
     * Desc: getter for the image caption
     * @param: int index
     * @return: String
     */
    public String getCaption(int index) {
        return imageCaptions.get(index).toString();
    }// end getCaption(int index)


    /**
     * Desc: default constructor for ImageAlbum
     * @param: none
     * @return: nothing
     */
    public ImageAlbum() {
        imageAlbum = new ArrayList();
        imageCaptions = new ArrayList();
        size = 0;
    }// end ImageAlbum()


    /**
     * Desc: parameterized constructor for ImageAlbum
     * @param: none
     * @return: nothing
     */
    public ImageAlbum(ArrayList tempImageAlbum, ArrayList tempImageCaptions) {
        imageAlbum = tempImageAlbum;
        imageCaptions = tempImageCaptions;
    }// end ImageAlbum(...)


    /**
     * Desc: adds the image directory and caption to both array lists
     * @param: String imageDirectory, String imageCaption
     * @return: void
     */
    public void add(String imageDirectory, String imageCaption) {
        imageAlbum.add(imageDirectory);
        imageCaptions.add(imageCaption);
        size++;
    }// end add(...)


    /**
     * Desc: clears imageAlbum and imageCaptions array lists
     * @param: nothing
     * @return: void
     */
    public void clear() {
        imageAlbum.clear();
        imageCaptions.clear();
        size = 0;
    }// end clear()
}// end class ImageAlbum

最終編輯

我顯然不是很理解,所以我決定采取不同的方法 - 我正在使用 JLabels 並制作圖標。 效果很好,謝謝大家的幫助

您的panel設置為 BorderLayout,您為每個縮略圖調用panel.add() 該方法將給定的組件設置到 BorderLayout 的中間,替換那里的任何內容,這就是為什么您只看到添加的最后一個。 BorderLayout 不會為縮略圖執行您想要的操作。

我希望你想要 GridLayout; 它以行和列的形式排列添加到其中的組件。 將您的panel設置為 GridLayout(或您想要布局縮略圖的任何其他內容),並將縮略圖添加到其中。 然后把panel放在任何你想要的地方; 默認情況下,JFrame 上有一個 Borderlayout,您可能希望將panel放在它的中間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM