[英]Trouble sending a char array in one class to an observable list in another in Java
I am new to Java and I am trying to send a string / array generated in one class to another to be used in an observable list. 我是Java的新手,我正在尝试将在一个类中生成的字符串/数组发送给另一个,以便在可观察列表中使用。 Basically I want the string in one class sent to the other class as a char array to an observable list so the letters can be presented in a tile like format in the GUI.
基本上,我希望将一类中的字符串作为char数组发送到另一类中的可观察列表,以便可以在GUI中以类似瓦片的格式显示字母。
This is my first class: 这是我的第一堂课:
public class Phrase {
String gamePhrase = "Keep moving forwards";
String guessPhrase = gamePhrase.replaceAll("[a-zA-Z0-9]", "*");
char[] array = guessPhrase.toCharArray();
}
This is my second class where I Am trying to add add the characters to: 这是我第二堂课,我尝试在其中添加以下字符:
private ObservableList<Node> phraseList;
private Phrase p1 = new Phrase();
private void start() {
phraseList.clear();
for (p1.toCharArray()) {
phraseList.add(new LetterBoxes(c));
}
}
private static class LetterBoxes extends StackPane {
private Rectangle bg = new Rectangle(40, 60);
private Text text;
public LetterBoxes(char x) {
bg.setFill(x == ' ' ? Color.DARKSEAGREEN : Color.WHITE);
bg.setStroke(Color.BLUE);
text = new Text(String.valueOf(x).toUpperCase());
text.setFont(DEFAULT_FONT);
text.setVisible(false);
setAlignment(Pos.CENTER);
getChildren().addAll(bg, text);
}
}
I am not sure how to get the array in correctly and am having difficulty moving forward. 我不确定如何正确放置阵列,并且前进时遇到困难。 Any help would be amazing!
任何帮助都将是惊人的!
for (p1.toCharArray()) {
is wrong for 2 reasons: 错误有两个原因:
Phrase
class does not provide a toCharArray
method. Phrase
类不提供toCharArray
方法。 for
loop is wrong. for
循环的语法错误。 you probably need a getter for the array in the Phase
class and use a enhanced for
loop: 您可能需要
Phase
类中的数组的吸气剂,并使用增强的for
循环:
public class Phrase {
public char[] getArray() {
return array;
}
...
}
for (char c : p1.getArray()) {
I think you are french no? 我认为你是法国人吗? Ce tombe bien, moi aussi !
Ce tombe bien,莫伊·奥斯西! But something are really wrong I guess like that:
但是我猜确实有些错误:
for(p1.toCharArray())
I don't know what you want to do but I don't think it works. 我不知道您想做什么,但我认为它没有用。
So first, you have to pass your variable in the class "phrase" in private, and build some getters-setters: 因此,首先,您必须私下在“短语”类中传递变量,并构建一些getter-setter方法:
public class Phrase {
private String gamePhrase = "Keep moving forwards";
private String guessPhrase = gamePhrase.replaceAll("[a-zA-Z0-9]", "*");
private char[] array = guessPhrase.toCharArray();
public void setGamePhrase(String s){
gamePhrase = s;
}
public void setGuessPhrase(String s){
guessPhrase = s;
}
public void setArray(Char[] s){
array = s;
}
public String getGamePhrase(){
return gamePhrase;
}
public String getGuessPhrase(){
return guessPhrase;
}
public char[] getArray(){
return array;
}
}
With that you will be able to set and get your data correctly. 这样,您将能够正确设置和获取数据。 Then, on your other class, you can do
然后,在另一堂课上,您可以
private Phrase p1 = new Phrase();
private void start() {
phraseList.clear();
for (int i=0; i<p1.getArray().size(); i++) { //Perhaps it's something else than .size(), but the idea is that you take the size of array
phraseList.add(new LetterBoxes(p1.getArray().get(i))); // Here is the same thing, perhaps it's not get(i), but the idea is that you take the value which is associate to the rank i in array.
}
}
public LetterBoxes(char x) {...}//don't change this function
Hope it will help you, 希望对您有帮助,
don't hesitate to answer me 不要犹豫,回答我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.