2017-05-20 6 views
0

私は、ウィンドウを開くプログラムを作成しています。ウィンドウを開くと、それを閉じると2つの新しいウィンドウが開きます。うまくいきましたが、私はColorChangerという関数を呼び出すExecutorサービスを使ってすべてのウィンドウの色を変更しようとしました。非静的メソッドでjavaのexecutorサービスを使用する

問題です:私はエグゼキュータのサービスを利用したい場合は、私は、静的メソッドを必要とするが、私はthis.getContentPane().setBackground(Color.blue);コマンドを使用したい場合は、私は非静的関数を使用する必要があります。

あなたはさらに情報が必要な場合は、私のコードを見てください、すべてが自己説明でなければならない:

public class SplittingWindow extends JFrame implements WindowListener,KeyListener { 

    int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(); 
    int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight(); 
    Random rand = new Random(); 
    String Input = new String(); 
    static ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); 

    public static void main(String[] args) { 
     executorService.scheduleAtFixedRate(() -> ColorChanger(), 0, 1, TimeUnit.SECONDS); 
     new SplittingWindow(); 
    } 

    SplittingWindow(){ 
     addWindowListener(this); 
     addKeyListener(this); 
     setResizable(false); 
     setSize(100,100); 
     setVisible(true); 
     setLocation(rand.nextInt(width-150),rand.nextInt(height-200)+50); 
    } 

    public void ColorChanger(){ 
     getContentPane().setBackground(Color.blue); 
    } 

    public void windowClosing(WindowEvent arg0) { 
     System.out.println("closing"); 
     new SplittingWindow(); 
     new SplittingWindow(); 
     dispose(); 
    } 

    public void keyTyped(KeyEvent e) { 
     Input = Input + e.getKeyChar(); 
     if(Input.contains("JayJay")==true){ 
      System.exit(0); 
     } 
    } 


    // Removed various interface methods 
} 

答えて

0

fqctあなたはHSクラスのメソッドを使用したい場合は、オブジェクトをインスタンス化する必要がありますそれは、静的せずに:)

public static void main(String[] args) { 
     SplittingWindow sp = new SplittingWindow(); 
     executorService.scheduleAtFixedRate(() -> sp.ColorChanger(), 0, 1, TimeUnit.SECONDS); 
    } 

これを試してみて、それが

+0

は、次の2つを作成しているなぜそれが完璧に – JayJay

+0

の作品は非常に多くの をお願いします動作するはずです'SplittingWindow'のインスタンス? – korolar

+0

@korolarそれはあなたが1つを閉じるたびに2つのウィンドウを作成する楽しいプログラムです – JayJay

関連する問題