Hello programmers,
I got a problem, I want to open a file on my Computer with Greenfoot. But how?
I try it with this code but it didn´t work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | public class Bibliothek extends Actor { /** * Act - do whatever the Bibliothek wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseClicked( this )) { if (mouse != null ) { if (mouse.getClickCount() == 1 ) { if (mouse.getButton() == 1 ) { Open(); } } } } } private void Open(){ Desktop desktop = null ; if (!Desktop.isDesktopSupported()) { return ; } desktop = Desktop.getDesktop(); String path = "Desktop/School " ; try { File fPath= new File(path); if (!fPath.exists()){ return ; } if (!fPath.isDirectory()){ return ; } desktop.open( new File(path)); } catch (IOException e) { e.printStackTrace(); return ; } } } |