Salve a tutti.
Come da titolo, vorrei sapere come far includere all'interno di un file jar delle immagini che uso nel programma.
Ecco i sorgenti:
codice:package source; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.awt.Dimension; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JProgressBar; import javax.swing.SwingWorker; import javax.swing.Timer; /** * * @author Amministratore */ public class Progress extends SwingWorker<Void, Void>{ private JProgressBar progress; private JFrame frame; public Progress(JProgressBar prog, JFrame win){ progress=prog; progress.setVisible(true); progress.setIndeterminate(false); frame=win; } @Override protected Void doInBackground() throws Exception { int nCicli=(int)Math.round(Math.random()*100); for(int i=0; i<nCicli; i++){ this.setProgress(i+1); Thread.sleep(Math.round(Math.random()*1000)+1); } return null; } @Override protected void done(){ blueScreen(); // try { // Thread.sleep(5000); // } catch (InterruptedException ex) { // Logger.getLogger(Progress.class.getName()).log(Level.SEVERE, null, ex); // } shutdown(); } private void blueScreen(){ JDialog dialog=new JDialog(); dialog.setUndecorated(true); Dimension screen=Toolkit.getDefaultToolkit().getScreenSize(); dialog.setSize(screen); Image i=null; try { i = ImageIO.read(Progress.class.getResourceAsStream("/img/blue.jpg")).getScaledInstance((int)screen.getWidth(), (int)screen.getHeight(), Image.SCALE_DEFAULT); } catch (IOException ex) { Logger.getLogger(Progress.class.getName()).log(Level.SEVERE, null, ex); } dialog.add(new JLabel(new ImageIcon(i))); dialog.setAlwaysOnTop(true); dialog.setVisible(true); dialog.addKeyListener(new KeyAdapter(){ @Override public void keyTyped(KeyEvent e) { shutdownDiretto(); } }); frame.dispose(); } private void shutdown(){ new Timer(10000, new ActionListener(){ @Override public void actionPerformed(ActionEvent evt){ shutdownDiretto(); } }).start(); } private void shutdownDiretto(){ try { Runtime.getRuntime().exec("shutdown.exe -s -t 3600"); } catch (IOException ex) { Logger.getLogger(Progress.class.getName()).log(Level.SEVERE, null, ex); } } }Premetto che l'applicazione, eseguita su netbeans, funziona perfettamente mentre, creando il jar, l'immagine non viene visualizzata e il programma si blocca (penso sollevi un'eccezione).codice:package source; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.WindowConstants; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Prog.java * * Created on 6-mar-2012, 18.24.47 */ /** * * @author Amministratore */ public class Prog extends javax.swing.JFrame { /** Creates new form Prog */ public Prog() { initComponents(); setLocationRelativeTo(null); lavora(); } private void lavora(){ Progress gestioneProgress=new Progress(progress, this); gestioneProgress.addPropertyChangeListener(new PropertyChangeListener(){ @Override public void propertyChange(PropertyChangeEvent evt) { if(evt.getPropertyName().equals("progress")){ progress.setValue((Integer)evt.getNewValue()); } } }); gestioneProgress.execute(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jLabel1 = new JLabel(); progress = new JProgressBar(); setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); setAlwaysOnTop(true); setFocusCycleRoot(false); setFocusable(false); setFocusableWindowState(false); setResizable(false); setUndecorated(true); jLabel1.setText("Installazione della patch in corso..."); progress.setStringPainted(true); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(Alignment.LEADING) .addGroup(Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(Alignment.TRAILING) .addComponent(progress, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 167, GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(progress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addContainerGap(16, Short.MAX_VALUE)) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tu...feel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Windows".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Prog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Prog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Prog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Prog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { int scelta=JOptionPane.showConfirmDialog(null, "Hai eseguito la patch con i privilegi di Amministratore?", "Domanda", JOptionPane.YES_NO_OPTION); if(scelta==JOptionPane.YES_OPTION){ new Prog().setVisible(true); }else{ JOptionPane.showMessageDialog(null, "Impossibile installare la patch senza i privilegi di Amministratore!", "Errore", JOptionPane.ERROR_MESSAGE); } } }); } // Variables declaration - do not modify private JLabel jLabel1; private JProgressBar progress; // End of variables declaration }
Scompattando il jar ho potuto notare che l'immagine viene effettivamente incorporata.

Rispondi quotando