如何在 java 中的 JTabbedPane 选项卡中生成或显示图像的缩略图 View ,并允许用户单击该图像以显示在 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 来完成,但我不知道。请给我建议方法
最佳答案
以下是一些可以帮助您的提示:
- 使用 GridLayout 创建面板以在缩略图 View 中显示图像。
- 在 JLabel 中将图像设置为图像图标,并将该标签添加到上面的面板。
- 将此面板作为选项卡添加到 JTabbedPane。
- 实现图像标签的 onclick 监听器。事件发生时获取该图像并将其显示在除此之外的其他选项卡中。
要在其他选项卡中显示图像:
- 创建一个包含一个标签的面板。
- 将此新面板添加到 JTabbedPane。
- 当有人从图像缩略图 View 中单击图像时,在其监听器中获取该图像,并在新面板的 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 Swing 图像缩略图 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9461320/