gpt4 book ai didi

来自静态类的 Java 引用

转载 作者:行者123 更新时间:2023-12-02 03:17:04 25 4
gpt4 key购买 nike

抱歉,这个问题之前可能已经被问过,但我在上下文中找不到任何适合我的问题的答案,以便我应用该解决方案。

无论如何,我正在开发一个使用文件的程序。当该文件更新时,我希望它将 File 变量替换为当前变量。我设置了一个将处理该文件的主类,然后设置了另一个具有不同线程的类来监听文件更新。当文件更新时,我希望主类中的变量也被更新。

这意味着更新监听器类必须具有主类的实例,但是当我尝试在更新监听器类启动期间发送它时,会出现一条警告,指出无法从静态上下文中引用主类。

代码如下:

主类

package me.xeyler;

import com.sun.media.jfxmedia.logging.Logger;

import javax.swing.*;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;

import static java.nio.file.StandardWatchEventKinds.*;

/**
* Created by Brigham on 10/19/2016.
*/
public class ViewerMain {

static FileHandler fileHandler;
static File skinFile;

public static void main(String[] args) {

boolean bool = false;

fileHandler = new FileHandler(this);
fileHandler.start();
while(true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(bool);
}

}

public void setSkinFile(File skinFile) {
this.skinFile = skinFile;
}
}

文件监听器类

package me.xeyler;

import com.sun.media.jfxmedia.logging.Logger;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;

import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;

/**
* Created by Brigham on 10/19/2016.
*/
public class FileHandler implements Runnable {

private Thread fileThread;
private String threadName;
WatchService watcher = null;
private ViewerMain main;

public FileHandler(ViewerMain main) {

this.main = main;
this.threadName = "FileThread";

}

public void watchFile(Path path) {

}

public void watchFile(File file) {

watchFile(Paths.get(file.getPath()));

}

public void close() {
try {
watcher.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void start () {
if (fileThread == null) {
System.out.println("Starting new thread...");
fileThread = new Thread (this, threadName);
fileThread.start();
System.out.println("Started thread: " + threadName);
}
}

@Override
public void run() {

System.out.println("Running thread...");

Path dir = Paths.get(System.getProperty("user.home"),"documents");
try {
watcher = FileSystems.getDefault().newWatchService();
WatchKey key = dir.register(watcher,
ENTRY_MODIFY);
} catch (IOException x) {
x.printStackTrace();
}

for (;;) {

// wait for key to be signaled
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException x) {
return;
}

for (WatchEvent<?> event: key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();

// The filename is the
// context of the event.
WatchEvent<Path> ev = (WatchEvent<Path>)event;
Path filename = ev.context();

if (filename.endsWith("text.txt")) {
System.out.println("File has changed");
//TODO: Update File variable in ViewerMain
main.setSkinFile(filename.toFile());
}

}

// Reset the key -- this step is critical if you want to
// receive further watch events. If the key is no longer valid,
// the directory is inaccessible so exit the loop.
boolean valid = key.reset();
if (!valid) {
// TODO: Handle inaccessible directory
break;
}
}

}
}

我怀疑答案确实很明显,但感谢您的耐心!

最佳答案

如果我理解正确,您需要一个 ViewerMain 类的实例。

this 不能应用于静态上下文。

public static void main(String[] args) {

ViewerMain viewer = new ViewerMain(); // an instance
fileHandler = new FileHandler(viewer);

skinFile相同

public File skinFile; // Remove static

public void setSkinFile(File skinFile) {
this.skinFile = skinFile;
}

关于来自静态类的 Java 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40140325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com