2010-12-02 4 views
-1

Je travaille sur un JFrame/panneau qui contiendra un bouton. Lorsque l'utilisateur clique sur le bouton, je veux qu'une image (qui sera préalablement stockée sur le disque dur de l'ordinateur) s'ouvre sur l'écran avant.Permet d'ouvrir une image .bmp/​​.jpeg en utilisant Java

button.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
     //here i want a code that will somehow open the image from a given directory 
      }}); 

Des suggestions sur comment faire à ce sujet? Je dois dire où l'image est stockée et déclencher un double-clic virtuel pour que l'image apparaisse sur l'écran avant. Est-ce encore possible en utilisant Java pour synchroniser ces fonctions de l'ordinateur?

+0

cela semble être ce que vous recherchez http://stackoverflow.com/questions/526037/java-how-to-open-user-system-preffered-editor-for-given-file – Karl

Répondre

3

Je ne sais pas d'une manière très courte, mais je voudrais utiliser quelque chose comme ça (comme qick hack pour une impression):

try { 
    // this is a new frame, where the picture should be shown 
    final JFrame showPictureFrame = new JFrame("Title"); 
    // we will put the picture into this label 
    JLabel pictureLabel = new JLabel(); 

    /* The following will read the image */ 
    // you should get your picture-path in another way. e.g. with a JFileChooser 
    String path = "C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg"; 
    URL url = new File(path).toURI().toURL(); 
    BufferedImage img = ImageIO.read(url); 
    /* until here */ 

    // add the image as ImageIcon to the label 
    pictureLabel.setIcon(new ImageIcon(img)); 
    // add the label to the frame 
    showPictureFrame.add(pictureLabel); 
    // pack everything (does many stuff. e.g. resizes the frame to fit the image) 
    showPictureFrame.pack(); 

    //this is how you should open a new Frame or Dialog, but only using showPictureFrame.setVisible(true); would also work. 
    java.awt.EventQueue.invokeLater(new Runnable() { 

     public void run() { 
     showPictureFrame.setVisible(true); 
     } 
    }); 

    } catch (IOException ex) { 
    System.err.println("Some IOException accured (did you set the right path?): "); 
    System.err.println(ex.getMessage()); 
    } 
0

Je pense que cela va fonctionner ...

Code:

processus = new ProcessBuilder ("mspaint", "votreNomFichier.jpeg"). Start();

Cela ouvrira votre fichier image avec mspaint .....

et également utiliser * Java Advanced Imaging (JAI) *

0

Essayez ce code

try 
{ 
    // the line that reads the image file 
    BufferedImage image; 


    // work with the image here ... 
     image = ImageIO.read(new File("C://Users//Neo//Desktop//arduino.jpg")); 


     jLabel1.setIcon(new ImageIcon(image)); 
} 
catch (IOException e) 
{ 
    // log the exception 
    // re-throw if desired 
} 
0

Je ne suis pas sûr mais essayez ceci ...

try 
{ 

    JLabel picture=new JLabel(); 

    ImageIcon ic=new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("C:\\Users\\Desktop\\xyz.jpg"))); 

    picture.setIcon(ic); 

} 
catch(Exception) 
{ 
} 
Questions connexes