【发布时间】:2015-12-22 23:17:46
【问题描述】:
我的程序应该从用户那里获取单词和定义,并像闪存卡一样显示它们。我已经将所有单词分类到类中,现在我需要做的就是做到这一点,以便当我的应用程序按下按钮时,控制器类将执行一个方法,该方法将通过 Card 类的数组列表并显示单词,最后显示定义。
我的问题是我有一个包含所有卡片的阅读器类的对象,我希望能够在 getWordClick 方法中调用随机卡片。我不知道如何在另一个类中使用该对象。
public class Main extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Scene.fxml"));
primaryStage.setTitle("FlashCards");
Scene scene = new Scene(root, 600, 400, Color.GREY);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main (String[] args){
Reader r = new Reader();
//Initialises the Arraylist and reads the file adding them to arraylist
ArrayList<String> wordList = r.getWordList();
r.OpenFile();
r.readFile(wordList);
r.closeFile();
//Initialises the Definitions Arraylist and reads the file adding them
ArrayList<String> definitionList = r.getDefinitionsList();
r.OpenFile();
r.readFile(definitionList);
r.closeFile();
/* IGNORE IS FOR TESTING PURPOSES
//Wordlist is printed
for (String i : wordList){
System.out.println(i);
}
//Definitions list is printed
for (String i : definitionList){
System.out.println(i);
} */
//Card for each word and def is made
ArrayList<Card> c = r.getCardList();
Main m = new Main();
r.cardSetter(m.addTerms(c, wordList.size(), wordList, definitionList));
//Loops through and displays the word and defs
for (Card i : c){
System.out.printf("%s : %s\n",i.dispWord(),i.dispDef());
}
//Displays the window
launch(args);
}
public ArrayList<Card> addTerms(ArrayList<Card> c, int q, ArrayList<String> word, ArrayList<String> def){
for (int i = 0; i<q; i++){
c.add(new Card(word,def,i));
}
return c;
}
}
这里是阅读器类
public class Reader {
private Scanner x;
private Scanner sc;
//ArrayList to store the words
private ArrayList<String> wordList = new ArrayList<>();
//ArrayList to store the definitions
private ArrayList<String> definitionsList = new ArrayList<>();
//ArrayList to store the cards
private ArrayList<Card> cardList = new ArrayList<>();
//Simple scanner collects user input
public String getFileName(){
sc = new Scanner(System.in);
return sc.nextLine();
}
//Method to open the file and throw an exception if failed
public void OpenFile(){
try{
x = new Scanner(new File(getFileName()));
}
catch (Exception e){
System.out.println("could not find file");
}
}
//Assigns each line to a Array
public void readFile(ArrayList<String> e){
while(x.hasNext()){
e.add(x.nextLine());
}
}
//Closes file
public void closeFile(){
x.close();
}
//Returns the wordlist
public ArrayList<String> getWordList(){
return wordList;
}
//Returns Definitionlist
public ArrayList<String> getDefinitionsList(){
return definitionsList;
}
//Returns cardList
public ArrayList<Card> getCardList(){
return cardList;
}
public void cardSetter(ArrayList<Card> c){
c = cardList;
}
}
这里是卡片类
public class Card {
private String word;
private String definition;
public Card(ArrayList<String> Word,ArrayList<String> Definition, int i){
word = Word.get(i);
definition = Definition.get(i);
}
public String dispWord(){
return word;
}
public String dispDef(){
return definition;
}
}
终于来了控制器
public class Controller {
Random rand = new Random();
private int Random;
//Makes the rand instance variable int so that the def class can use it
public Button wordBox;
public Label defBox;
public void getWordClick(){
}
public void goExit(){
}
public void goRand(){
}
public void getDefClick(){
}
public void goNext(){
}
public void goPrev(){
}
}
对不起,我知道它很长,但代码仅供参考,我主要关心的是如何从Reader r 获取ArrayList<Card>,以便我可以在getWordClick() 方法中的控制器中使用它。从字面上看,任何帮助都会受到赞赏,我只需要有人在我被卡住时将我推向正确的方向。
更新:我现在编辑了控制器类,所以它看起来像这样 公共类控制器 {
Random rand = new Random();
private int Random;
//Makes the rand instance variable int so that the def class can use it
public Button wordBox;
public Label defBox;
private Reader mReader = null;
public Controller(Reader reader){
this.mReader = reader;
}
public Reader getReader(){
return this.mReader;
}
public void getWordClick(){
getReader();
}
public void goExit(){
}
public void goRand(){
}
public void getDefClick(){
}
public void goNext(){
}
public void goPrev(){
}
}
但现在的问题是,当 fxml 文件运行并查找控制器时,它将如何创建对象本身或将使用我创建的对象,因为我创建了一个对象,我在其中添加了阅读器作为构造函数.但是我不知道 fxml 文件将如何使用它来处理事件。
【问题讨论】:
-
我不认为我理解这个问题。您已经有一个方法
getCardList来检索Reader的卡片列表。难道你不能只有一个实例变量来存储Reader,然后在getWordClick中调用reader.getCardList()? -
您的问题是在处理 List 吗?看一篇教程:examples.javacodegeeks.com/core-java/util/arraylist/…。否则,您可以像任何对象变量一样传递管理它。
标签: java object arraylist javafx controller