我正在使用twitch api wrapper和JavaFx进行Twitch聊天。但是我在聊天中收到消息时传递参数有问题。我做了一个帖子。它应该像这样工作:
问题是我无法理解如何将消息字符串从onMessage方法传递给Controller,因此我可以更改标签的文本。
以下是代码:
Bot类:
public class Bot extends TwitchBot {
public Bot() {
}
public static String message2;
@Override
public void onMessage(User user, Channel channel, String message) throws IOException {
System.out.println("Message is:" + message);
setMessage2(message);
}
private void setMessage2(String message) {
message2 = message;
}
}
Controller.java
public class Controller implements Initializable {
@FXML private Label labelChat;
@FXML private TextArea textArea;
@FXML private GridPane gridPane;
@Override
public void initialize(URL location, ResourceBundle resources) {
System.out.println("MADE");
}
public void messagePrint(String message) {
System.out.println("LABEL!!!");
labelChat.setText(message);
}
}
Main.java
public class Main extends Application {
public static Controller c;
@Override
public void start(Stage primaryStage) throws Exception{
System.out.println("Launched");
// Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent root = loader.load();
Controller c = loader.getController();
Main.c = c;
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 450, 550));
primaryStage.show();
}
public static void main(String[] args) {
TwitchBot bot = new Bot();
bot.connect();
bot.joinChannel("#artek_tv");
Task task = new Task() {
@Override
protected Object call() throws Exception {
bot.start();
return null;
}
};
new Thread(task).start();
Task task2 = new Task() {
@Override
protected Object call() throws Exception {
c.messagePrint(Bot.message2);
return null;
}
};
new Thread(task2).start();
launch(args);
}
}
答案 0 :(得分:-1)
您正在尝试使用setMessage2()设置消息吗?
那么为什么你不能在该方法中访问Label labelChat
并使用Platform.runLater(Runnable)
设置文字