Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java';使用非静态方法创建执行器服务_Java_Methods_Lambda_Executorservice_Non Static - Fatal编程技术网

使用java';使用非静态方法创建执行器服务

使用java';使用非静态方法创建执行器服务,java,methods,lambda,executorservice,non-static,Java,Methods,Lambda,Executorservice,Non Static,我一直在写一个程序,它会打开一个窗口,如果你关闭它,它会打开两个新窗口。它工作正常,但我尝试使用executor服务更改所有窗口的颜色,该服务调用我的函数ColorChanger 问题是:如果我想使用executor服务,我需要一个静态方法,但是如果我想使用this.getContentPane().setBackground(Color.blue)命令,我必须使用非静态函数 如果您需要更多信息,请查看我的代码,所有内容都应通过自我解释: public class SplittingWindow

我一直在写一个程序,它会打开一个窗口,如果你关闭它,它会打开两个新窗口。它工作正常,但我尝试使用executor服务更改所有窗口的颜色,该服务调用我的函数ColorChanger

问题是:如果我想使用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  
}

fqct是,如果要使用hs类的方法而不使其成为静态,则需要实例化对象:)


试试这个,它应该会工作

为什么要创建两个
拆分窗口的实例
?@korolar,因为它是一个有趣的程序,每次关闭一个窗口时都会创建两个窗口
public static void main(String[] args) {
        SplittingWindow sp = new SplittingWindow();
        executorService.scheduleAtFixedRate(() -> sp.ColorChanger(), 0, 1, TimeUnit.SECONDS);
    }