Domanda

Come generare o mostrare la visualizzazione miniatura delle immagini in una scheda di JtabbedPane in Java e consentire all'utente di fare clic su quell'immagine da visualizzare in un'altra scheda di un 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;
            }
    }
}
.

Beh, questo ciò che sto provando qui inserito da un'immagine di disegno che voglio caricare e mostrare l'immagine in modo da poter fare clic sull'immagine e aprirlo in un'altra scheda per modificare l'immagine Sono un po 'in grado di sapere che può essere fatto usando jlist ma come non lo so.Per favore mi suggerisci la via

È stato utile?

Soluzione

Ecco alcuni suggerimenti per aiutarti:

    .
  1. Crea un pannello con Gridlayout per visualizzare le immagini nella vista miniatura.
  2. Imposta immagini come icona dell'immagine in Jlabel e aggiungi che etichette al pannello sopra.
  3. Aggiungi questo pannello a JtabbedPane come tab.
  4. Implementa i listener onclick di etichette di immagini. E l'evento si verifica prendi quell'immagine e lo visualizza in qualche altra scheda di questo.

    Per visualizzare l'immagine in un'altra scheda:

      .
    1. Crea un pannello con un'etichetta in esso.
    2. Aggiungi questo nuovo pannello a jtabbedpane.
    3. Quando qualcuno fa clic su un'immagine dalla visualizzazione miniatura dell'immagine, ottenere quell'immagine nel suo ascoltatore e imposta quell'immagine in jlabel del nuovo pannello.
      .

      Per ulteriori informazioni mostraci cosa hai provato e meglio se è possibile postare un breve esempio di codice di lavoro che dimostra il tuo problema.


      .
      .

      modifica

      per un altro requisito descritto nel commento:

      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);
      }
      
      .

      Questo è solo il codice demo, potrebbe essere necessario modificarlo in base alle tue esigenze. Questo è solo per mostrarti come puoi monitorare il clic su 2 componenti e prenderli in un'altra scheda.

      Desideri che tu abbia chiesto una domanda diversa per questo potrei aver ottenuto alcuni upvotes / risposta accettata o il migliore alcune bounty o i peggiori voti negativi.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top