【问题标题】:Java Swing Thumbnail View of Images to select image用于选择图像的 Java Swing 图像缩略图视图
【发布时间】:2013-09-08 05:53:59
【问题描述】:

如何在 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;
            }
    }
}

好吧,这就是我在这里尝试的绘制图像,我想实际加载并显示图像,以便我可以单击图像并在另一个选项卡中打开它以编辑图像 我有些人知道它可以通过使用 jlist 来完成,但我不知道。请给我建议

【问题讨论】:

    标签: java swing javax.swing.text


    【解决方案1】:

    这里有一些提示可以帮助你:

    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 个组件并将它们放在另一个选项卡中。

    希望您对此提出不同的问题,我可能会得到一些赞成/接受的答案或最好的一些赏金或最差的反对票。

    【讨论】:

    • 你能展示我如何在 java 中使用标签的 onclick 监听器吗?
    • @user1235021 添加MouseListener。使用MouseListenermouseClicked(mouseEvent) 方法处理点击。
    • 你好,现在我还有一个小问题。我需要从一个选项卡中同时单击两个图像以进行比较,我的意思是这两个单击的图像应该在另一个选项卡中打开以进行比较,您能否建议我如何解决这个问题?
    • 您可以使用 JSplitPane 在同一个 jpanel 中显示 2 个图像。我不知道比较逻辑。
    • 我想在 jtabbed 窗格的同一选项卡中显示两个图像,需要选择两个在新选项卡中打开的图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 2011-11-22
    • 2011-06-14
    • 1970-01-01
    • 2012-01-27
    相关资源
    最近更新 更多