2016-09-13 47 views
-1

我是Java的新手,我試圖創建一個相當簡單/基本的WatchService程序。我使用Eclipse和這裏的部分代碼片段顯示出是有問題的行:Java導入處理聲明WatchService

import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE; 
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE; 
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY; 

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowEvent; 
import java.awt.event.WindowListener; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.nio.file.FileSystems; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.nio.file.WatchEvent; 
import java.nio.file.WatchKey; 
import java.nio.file.WatchService; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

@SuppressWarnings("serial") 
public class MDMButtons extends JFrame implements WindowListener,ActionListener,Runnable { 
    JButton b1, b2, b3, b4; 
    ImageIcon red = new ImageIcon("img/red.gif","Down"); 
    ImageIcon green = new ImageIcon("img/green.png","Up"); 
    JLabel imageRed; 
    JLabel imageGreen; 
    // File f = new File("fault.txt"); 
    // File file = new File("%userprofile%\\desktop\\error.txt"); 
    WatchService watcher = FileSystems.getDefault().newWatchService(); 

    public static void main(String[] args) throws IOException { 
     MDMButtons window = new MDMButtons("MDM Dashboard Beta v1.0"); 
     window.setSize(400,200); 
     window.setVisible(true); 

我有以下錯誤交易的問題:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type IOException at MDMButtons.(MDMButtons.java:38)
at MDMButtons.main(MDMButtons.java:41)

這似乎處理這條線的後半部分(在「=」後):

WatchService watcher = FileSystems.getDefault().newWatchService(); 

我認爲這與進口的事,但我在爲我需要做什麼來糾正它的損失。

如果我刪除了導入的靜態行,錯誤消失了,但在程序的後面留下了問題,導致依賴於這些導入的代碼行。

任何和所有幫助表示讚賞!

+0

如果你失望的排名我的問題,請沒有禮貌地告訴我爲什麼,所以我可以糾正它。 – makadus

回答

1

它說你需要處理可以從你的代碼拋出的IOException。
而你的代碼片段並不真正顯示編譯錯誤的地方。
它是在線41
要麼聲明main方法拋出IOException異常或包裝代碼可能拋出IOException到

try{ 

    }catch(IOException ex){ 
     //do something 
    } 
+0

我添加了拋出IOException到主方法。同樣的錯誤。 – makadus

+0

我會添加到第41行。我的歉意。 – makadus

+0

我嘗試了你的兩個建議。我收到了同樣的錯誤: WatchService watcher = FileSystems.getDefault()。newWatchService(); \t \t 公共靜態無效的主要(字串[] args)拋出IOException異常{ \t \t嘗試{ \t \t \t MDMButtons窗口=新MDMButtons( 「MDM中心測試版1.0」); \t \t \t window.setSize(400,200); \t \t \t window.setVisible(true); \t \t} catch(IOException e8){ \t \t \t e8.printStackTrace(); \t \t} \t} – makadus