2016-09-13 9 views
-1

私はJavaを使い慣れていないので、単純で基本的なWatchServiceプログラムを作成しようとしています。私は、Eclipseを使用して、ここで問題のある行を示す部分のコードスニペットでいます:WatchServiceの宣言を扱うJavaのインポート

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

にIOExceptionをスロー可能性のあるコードをラップするいずれかの行に41
ですそれ。 – makadus

答えて

1

あなたのコードからスローされるIOExceptionを処理する必要があると言われています。
あなたのコードスニペットは実際にコンパイルエラーがある場所を表示しません。
それは私が修正することができ、なぜ私を伝えるために礼儀を持ってください、あなたの主なメソッドはIOExceptionがスローすることを宣言するか、あなたがダウンして私の質問にランク付けした場合

try{ 

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

メインメソッドにIOExceptionをスローしました。同じエラー。 – makadus

+0

私は41行目に追加します。謝罪します。 – makadus

+0

私は両方のあなたの提案を試みました。同じエラーが発生しています。 WatchService watcher = FileSystems.getDefault()。newWatchService(); \t \t公共の静的な無効メイン(文字列[]引数)は、IOExceptionがスロー{ \t \t試み{ \t \t \t MDMButtonsウィンドウ=新しいMDMButtons( "MDMダッシュボードベータv1.0を")。 \t \t \t window.setSize(400,200); \t \t \t window.setVisible(true); \t \t}キャッチ(のIOExceptionのE8){ \t \t \t e8.printStackTrace()。返事が遅れて申し訳ありません \t \t} \t} – makadus