如何在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;
            }
    }
}
. 好吧,这是我在这里尝试的是绘图图像我想要逃离加载并显示图像所以Dat我可以单击图像并在另一个选项卡中打开以编辑图像 我有些人如何知道它可以通过使用jlist来完成,但我怎么不知道。请告诉我
有帮助吗?

解决方案

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

  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归因
不隶属于 StackOverflow
scroll top