簡體   English   中英

Java Swing縮略圖的圖像視圖以選擇圖像

[英]Java Swing Thumbnail View of Images to select image

如何在Java中的JTabbedPane的選項卡中生成或顯示圖像的縮略圖視圖,並允許用戶單擊該圖像以顯示在JTabbedpane的其他選項卡中?


    import javax.swing.*;
    import java.awt.*;
    import java.awt.Event.*;
    import java.io.File;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.io.IOException;

    public class SwindDesign {
    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame("Split Pain");
        frame.setSize(700, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout());

        //panel
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(new PicturePanel());

       JTabbedPane jtp = new JTabbedPane();

         jtp.addTab("Set Image", panel);
          jtp.addTab("Compare Image", new JButton());
          frame.add(jtp);

    }
}
class PicturePanel extends JPanel {

    File folder = new File("C:/Documents and Settings/All Users/Documents/My      Pictures/Sample Pictures");
    File[] listOfFiles = folder.listFiles();
    ImageIcon[] img ;
    JComponent lblimg;
    JTabbedPane jtp = new JTabbedPane();
    private BufferedImage[] b = new BufferedImage[10];

    public PicturePanel() throws IOException {
        for (int i = 0; i < listOfFiles.length; i++) {
            System.out.println("chek panth"+listOfFiles[i].getName().toString());
            b[i] = ImageIO.read(new File("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/" + listOfFiles[i].getName().toString()));
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponents(g);
        Graphics2D g2 = (Graphics2D) g;
        int k = 10;
        for (int j = 0; j < listOfFiles.length - 1; j++) {
            g2.drawImage(b[j], k, 0, 100, 100, null);
            k = k + 75;
            }
    }
}

好吧,這是我在這里嘗試繪制的圖像,我想強制加載並顯示圖像,這樣我就可以單擊圖像並在另一個選項卡中將其打開以編輯圖像,我如何能夠知道可以通過使用傑利斯特,但我不知道。 請給我建議一下

這里有一些提示可以幫助您:

  1. 使用GridLayout創建一個面板以在縮略圖視圖中顯示圖像。
  2. 在JLabel中將圖像設置為圖像圖標,然后將該標簽添加到上面的面板中。
  3. 將此面板作為選項卡添加到JTabbedPane。
  4. 實現圖像標簽的onclick偵聽器。 事件發生后,獲取該圖像並將其顯示在其他選項卡中。

要在其他選項卡中顯示圖像:

  1. 創建一個帶有一個標簽的面板。
  2. 將此新面板添加到JTabbedPane。
  3. 當某人從圖像縮略圖視圖中單擊圖像時,將該圖像放入監聽器中,然后在新面板的JLabel中設置該圖像。

要獲得更多幫助,請向我們展示您嘗試過的事情,如果可以發布一個簡短的示例代碼來演示您的問題,可以做得更好。



編輯

對於注釋中描述的另一個要求:

boolean isSelected = false;
JButton jButton;
void imageClickTest() throws MalformedURLException, IOException {
    final JFrame frame = new JFrame("Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setLayout(new BorderLayout());

    final JTabbedPane tabbedPane = new JTabbedPane();

    JPanel pane = new JPanel();
    JButton button;
    pane.setLayout(new BorderLayout());

    button = new JButton("I'm second button");
    button.setIcon(new ImageIcon(ImageIO.read(new URL("http://cdn5.iconfinder.com/data/icons/ie_Financial_set/24/26.png"))));
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton) e.getSource();
            if(isSelected) {
                System.out.println("two selected");
                button.setBorder(BorderFactory.createEtchedBorder());
                isSelected = false;
                JSplitPane jSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
                jSplitPane.add(button);
                jButton.setBorder(BorderFactory.createEtchedBorder());
                jButton.setText("First click me");
                jSplitPane.add(jButton);
                jSplitPane.setDividerLocation(150);
                tabbedPane.addTab("Image Comparision", jSplitPane);
            }
        }
    });
    pane.add(button, BorderLayout.SOUTH);

    button = new JButton("First click me");
    button.setIcon(new ImageIcon(ImageIO.read(new URL("http://cdn4.iconfinder.com/data/icons/REALVISTA/web_design/png/24/testimonials.png"))));
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton) e.getSource();
            button.setBorder(BorderFactory.createLineBorder(Color.RED, 5));
            button.setText("Now Click on second button.");
            jButton = button;
            isSelected = true;
        }
    });
    pane.add(button, BorderLayout.NORTH);

    button = new JButton("I'm just extra button");
    button.setIcon(new ImageIcon(ImageIO.read(new URL("http://cdn2.iconfinder.com/data/icons/crystalproject/64x64/apps/kservices.png"))));
    button.setEnabled(false);
    pane.add(button, BorderLayout.CENTER);

    tabbedPane.addTab("ImagePane", pane);
    frame.add(tabbedPane, BorderLayout.CENTER);
    frame.setVisible(true);
}

這只是演示代碼,您可能需要根據需要進行修改。 這只是向您展示如何監視2個組件的點擊並使它們進入另一個選項卡。

希望您對此提出不同的問題,我可能得到一些支持/接受的答案,或者最好的賞金或最差的不贊成票。

暫無
暫無

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

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