在jtabbedpane中显示多个图像

时间:2012-03-02 17:45:15

标签: java image swing 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 个答案:

答案 0 :(得分:2)

  1. JPanelGridLayoutGridBagLayout一起使用,以显示JLabel的列表。
  2. 为每个标签设置一个图像,并为每个标签附加MouseListener。点击标签后,创建一个包含大图片的新JPanelJLabel并将其添加到标签