简体   繁体   English

Java Swing缩略图的图像视图以选择图像

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

How To Generate or Show thumbnail view of images in a tab of JTabbedPane in java and allow user to click on that image to be display in other tab of a JTabbedpane ? 如何在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;
            }
    }
}

well this what i am trying here instated of drawing image i want to actully load and show the image so dat i can click on image and open it in another tab to edit the image i some how able to know that it can be done by using jlist but how that i dont know. 好吧,这是我在这里尝试绘制的图像,我想强制加载并显示图像,这样我就可以单击图像并在另一个选项卡中将其打开以编辑图像,我如何能够知道可以通过使用杰利斯特,但我不知道。 please suggest me the way 请给我建议一下

Here are some hints to help you: 这里有一些提示可以帮助您:

  1. Create a panel with GridLayout to display images in thumbnail view. 使用GridLayout创建一个面板以在缩略图视图中显示图像。
  2. Set images as image icon in JLabel and add that labels to above panel. 在JLabel中将图像设置为图像图标,然后将该标签添加到上面的面板中。
  3. Add this panel to JTabbedPane as a tab. 将此面板作为选项卡添加到JTabbedPane。
  4. Implement onclick listeners of image labels. 实现图像标签的onclick侦听器。 And event occurs get that image and display it in some other tab than this. 事件发生后,获取该图像并将其显示在其他选项卡中。

To display image in other tab: 要在其他选项卡中显示图像:

  1. Create a panel with one label in it. 创建一个带有一个标签的面板。
  2. Add this new panel to JTabbedPane. 将此新面板添加到JTabbedPane。
  3. When someone clicks on an image from image thumbnail view, get that image in it's listener and set that image in JLabel of new panel. 当某人从图像缩略图视图中单击图像时,将该图像放入监听器中,然后在新面板的JLabel中设置该图像。

For more help show us what you have tried and better if you can post a short working code example which demonstrate your problem. 要获得更多帮助,请向我们展示您尝试过的事情,如果可以发布一个简短的示例代码来演示您的问题,可以做得更好。



EDIT 编辑

For another requirement described in comment: 对于注释中描述的另一个要求:

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);
}

This is just demo code, you might need to modify it based on your requirements. 这只是演示代码,您可能需要根据需要进行修改。 This is just to show you how you can monitor click on 2 components and get them in another tab. 这只是向您展示如何监视2个组件的点击并使它们进入另一个选项卡。

Wish you have asked a different question for this I might have got some upvotes/accepted answer or the best some bounty or the worst down votes. 希望您对此提出不同的问题,我可能得到一些支持/接受的答案,或者最好的赏金或最差的不赞成票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM