[英]Use a reference inside an ArrayList to call a method, and change the current state of the referenced object?
也許這是不可能的,但我想要做的是同時使用ArrayList來動態地引用它們來改變GUI中多個元素的可見性。 對象是由另一種方法自己創建的。
oldScreen.setVisible(false); 和oldScreen <1> .setVisible(false); 陳述導致錯誤。 我有一種預感,我的想法不會很好。
這基本上是我所擁有的,我能以任何方式實現這一目標嗎?
private void initScreens() {
// I create some ArrayLists as "screensets" of sorts and put some GUI elements in there
ArrayList startScreen = new ArrayList();
ArrayList lostScreen = new ArrayList();
ArrayList playScreen = new ArrayList();
startScreen.add(startB);
startScreen.add(exitB);
lostScreen.add(yl1);
lostScreen.add(yl2);
lostScreen.add(yl3);
lostScreen.add(yl4);
lostScreen.add(yl5);
}
private void changeScreen(ArrayList oldScreen,ArrayList newScreen) {
// now i try to create a handy method to handle the length of the arrays itself, so if
i need to make changes to screens I just add them to there array. They are then easily
displayed, and hidden when told.
int os = oldScreen.size();
int ns = newScreen.size();
for (int i = os; i > 0; i--){
oldScreen<i>.setVisible(false);
oldScreen<1>.setVisible(false);
}
這是無效的語法。
你正在努力寫作
oldScreen.get(i)
您還應該使用泛型( ArrayList<Screen>
)來避免強制轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.