0

如何在 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,但我不知道。请给我建议的方式

4

1 回答 1

6

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

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

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

于 2012-02-27T09:03:24.250 回答