如何主动将 JLabels 从 ArrayList 设置到 JPanel?
How to I actively set JLabels from an ArrayList to the JPanel?
我正在尝试制作 Frogger 游戏。
这是一个带有 JLabel 的简单 JPanel。
public class PlayField extends JPanel implements KeyListener{
ArrayList<JLabel> rows = new ArrayList<JLabel>();
Random g = new Random();
public PlayField(int length){ //length is the number of rows
generateField(length);
for(JLabel label : rows){
add(label); //here is where I add all of the labels to the panel
}
}
public void generateField(int length){ //generates our JLabels in the ArrayList..
for(int i = 0; i < length; i++){
JLabel lbl = new JLabel();
int obstaclePos = g.nextInt(5);
switch (obstaclePos){
case 0: lbl.setText(" X — — — —");
break;
case 1: lbl.setText(" — X — — —");
break;
case 2: lbl.setText(" — — X — —");
break;
case 3: lbl.setText(" — — — X —");
break;
case 4: lbl.setText(" — — — — X");
break;
}
lbl.setBounds(0,20*i,80,20);
rows.add(lbl); //adding the JLabels to my arraylist
lbl.setName("row" + i);
}
}
public void push(){ //scrolls text one to the right and puts last character at the beginning
for(JLabel label : rows){
label.setText(rotate(label.getText()));
}
}
public String rotate(String str){ //cycles text one to the right, and wraps last character
String first = str.substring(0, str.length());
String second = str.substring(str.length());
return second+first;
}
很难准确表达我的问题,但我基本上希望在调用 push() 函数时更改和更新添加到 JPanel 的 JLabel。
你的旋转方式不对...
String first = str.substring(0, str.length());
String second = str.substring(str.length());
应该更像
String first = str.substring(0, str.length() - 1);
String second = str.substring(str.length() - 1);
first
是除最后一个字符以外的所有字符,second
是最后一个字符。
但是,请记住,您的 String
中有 "spaces",这可能会使过程稍微复杂一些
我正在尝试制作 Frogger 游戏。
这是一个带有 JLabel 的简单 JPanel。
public class PlayField extends JPanel implements KeyListener{
ArrayList<JLabel> rows = new ArrayList<JLabel>();
Random g = new Random();
public PlayField(int length){ //length is the number of rows
generateField(length);
for(JLabel label : rows){
add(label); //here is where I add all of the labels to the panel
}
}
public void generateField(int length){ //generates our JLabels in the ArrayList..
for(int i = 0; i < length; i++){
JLabel lbl = new JLabel();
int obstaclePos = g.nextInt(5);
switch (obstaclePos){
case 0: lbl.setText(" X — — — —");
break;
case 1: lbl.setText(" — X — — —");
break;
case 2: lbl.setText(" — — X — —");
break;
case 3: lbl.setText(" — — — X —");
break;
case 4: lbl.setText(" — — — — X");
break;
}
lbl.setBounds(0,20*i,80,20);
rows.add(lbl); //adding the JLabels to my arraylist
lbl.setName("row" + i);
}
}
public void push(){ //scrolls text one to the right and puts last character at the beginning
for(JLabel label : rows){
label.setText(rotate(label.getText()));
}
}
public String rotate(String str){ //cycles text one to the right, and wraps last character
String first = str.substring(0, str.length());
String second = str.substring(str.length());
return second+first;
}
很难准确表达我的问题,但我基本上希望在调用 push() 函数时更改和更新添加到 JPanel 的 JLabel。
你的旋转方式不对...
String first = str.substring(0, str.length());
String second = str.substring(str.length());
应该更像
String first = str.substring(0, str.length() - 1);
String second = str.substring(str.length() - 1);
first
是除最后一个字符以外的所有字符,second
是最后一个字符。
但是,请记住,您的 String
中有 "spaces",这可能会使过程稍微复杂一些