Java Swingのサムネイル画像を選択するための画像の表示
-
13-11-2019 - |
質問
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を使用して行うことができることを知ることができるが、私が知っていないことを知ることができます。の道を提案してください
解決
これはあなたを助けるためのいくつかのヒントです:
- グリッドラインでパネルを作成して、サムネイルビューに画像を表示します。
- JLABELの画像アイコンとして画像を設定し、そのラベルを上記のパネルに追加します。
- このパネルをタブとしてJtabbedPaneに追加します。
- 画像ラベルのオンクリックリスナーを実装します。そしてイベントが発生し、その画像を取得し、それ以外のタブに表示します。
他のタブに画像を表示するには:
- 1つのラベルを持つパネルを作成します。
- この新しいパネルをJtabbedPaneに追加します。
- 画像のサムネイルビューから画像をクリックすると、そのイメージをリスナーで取得し、その画像を新しいパネルの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つのコンポーネントをクリックして別のタブに入れる方法を監視する方法をお勧めします。
あなたがこれについて異なる質問をしたことを願っています私は、一部の上向き/承認答えまたは最高の紀元前または最悪の投票票を持っているかもしれません。
所属していません StackOverflow