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