![]()
Sì, ho visto il tuo codice e secondo me ti stai complicando la vita.
Il codice del primo esempio della pagina del progetto è più che sufficiente... ti basta solo mettere insieme la gestione degli internal frame con quel codice. Mi riferisco sempre al codice che ho postato sopra, modificato per utilizzare un JInternalFrame (con qualche commento...)
codice:import com.sun.pdfview.PDFFile; import com.sun.pdfview.PDFPage; import com.sun.pdfview.PagePanel; import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * An example of using the PagePanel class to show PDFs. For more advanced * usage including navigation and zooming, look ad the * com.sun.pdfview.PDFViewer class. * * @author joshua.marinacci@sun.com */ public class ViewTest extends JFrame { private class MyPageButtonListener implements ActionListener { public MyPageButtonListener() { if (pg == 1) { indietro.setEnabled(false); } if (pg == tot_pages) { avanti.setEnabled(false); } } public void actionPerformed(ActionEvent ae) { JButton src = (JButton)ae.getSource(); if (src.equals(indietro)) { pg--; if (pg <= 1) { indietro.setEnabled(false); } avanti.setEnabled(true); showPage(pg); } else { pg++; if (pg >= tot_pages) { avanti.setEnabled(false); } indietro.setEnabled(true); showPage(pg); } } } private JButton avanti, indietro; private PagePanel panel; private PDFFile pdffile; private String path; private int pg, tot_pages; private JInternalFrame iframe; private JDesktopPane desktop; public ViewTest(String path, int pg) { super("Test PDF"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.avanti = new JButton("Avanti"); this.indietro = new JButton("Indietro"); this.pg = pg; this.panel = new PagePanel(); this.path = path; //Creo un JDesktop pane per accogliere i JInternalFrame desktop = new JDesktopPane(); this.setContentPane(desktop); //Creo il JInternalFrame vero e proprio. iframe = new JInternalFrame("PDF File", true, true, true, true); iframe.setLayout(new BorderLayout()); iframe.getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER); //pannello bottoni JPanel northPanel = new JPanel(new GridLayout(1,2)); northPanel.add(indietro); northPanel.add(avanti); iframe.getContentPane().add(northPanel, BorderLayout.NORTH); //dimensioni e posizione relativa del JInternalFrame iframe.setSize(400, 300); iframe.setLocation(50, 50); //rendiamo il JInternalFrame visibile e aggiungiamolo al JDestopPane iframe.setVisible(true); desktop.add(iframe); this.setSize(800,800); this.setVisible(true); try { this.setup(path); this.showPage(pg); } catch (Exception e) { e.printStackTrace(); } indietro.addActionListener(new MyPageButtonListener()); avanti.addActionListener(new MyPageButtonListener()); } public void showPage(int pg) { PDFPage page = pdffile.getPage(pg); panel.showPage(page); panel.validate(); } public void setup(String path) throws IOException { //set up the frame and panel //load a pdf from a byte buffer File file = new File(path); RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); pdffile = new PDFFile(buf); tot_pages = pdffile.getNumPages(); } public static void main(final String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new ViewTest("tuo_pdf.pdf", 1); } }); } }

Sì, ho visto il tuo codice e secondo me ti stai complicando la vita.
Rispondi quotando