[英]Trouble sending a char array in one class to an observable list in another in Java
我是Java的新手,我正在嘗試將在一個類中生成的字符串/數組發送給另一個,以便在可觀察列表中使用。 基本上,我希望將一類中的字符串作為char數組發送到另一類中的可觀察列表,以便可以在GUI中以類似瓦片的格式顯示字母。
這是我的第一堂課:
public class Phrase {
String gamePhrase = "Keep moving forwards";
String guessPhrase = gamePhrase.replaceAll("[a-zA-Z0-9]", "*");
char[] array = guessPhrase.toCharArray();
}
這是我第二堂課,我嘗試在其中添加以下字符:
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);
}
}
我不確定如何正確放置陣列,並且前進時遇到困難。 任何幫助都將是驚人的!
for (p1.toCharArray()) {
錯誤有兩個原因:
Phrase
類不提供toCharArray
方法。 for
循環的語法錯誤。 您可能需要Phase
類中的數組的吸氣劑,並使用增強的for
循環:
public class Phrase {
public char[] getArray() {
return array;
}
...
}
for (char c : p1.getArray()) {
我認為你是法國人嗎? Ce tombe bien,莫伊·奧斯西! 但是我猜確實有些錯誤:
for(p1.toCharArray())
我不知道您想做什么,但我認為它沒有用。
因此,首先,您必須私下在“短語”類中傳遞變量,並構建一些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;
}
}
這樣,您將能夠正確設置和獲取數據。 然后,在另一堂課上,您可以
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
希望對您有幫助,
不要猶豫,回答我
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.