如何在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来完成,但我怎么也不知道。请按照方式建议我
答案 0 :(得分:6)
以下是一些可以帮助您的提示:
要在其他标签中显示图像:
如果您可以发布一个简短的工作代码示例来证明您的问题,请向我们展示您尝试过的更多帮助。
对于评论中描述的其他要求:
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个组件并将其放在另一个标签中。
希望你提出一个不同的问题,我可能会得到一些赞成/接受的答案或最好的一些赏金或最差的选票。