【发布时间】:2012-03-05 09:08:11
【问题描述】:
我遇到了一个问题,我在 arrayList 中丢失了数据。在调用 MPUComp 类中的 Refresh 方法后,我进入类 mpuChecker 并调用 updateTextArea。
通过这样做,我丢失了 MPUComp 中 arraylist 中存在的数据。我做错了什么。我认为这与我如何称呼班级有关。如何正确保存这些数据?
public class MPUComp extends JFrame {
{
private mpuChecker mC;
public ArrayList<String> oldTags = new ArrayList<String>();
public void menu()
{
//...
class MenuActionListener3 implements ActionListener {
public void actionPerformed(ActionEvent e)
{
mC = new mpuChecker();
mC.CheckMpu(path, textField.getText(),1);
setVisible(false);
}
}
class MenuActionListener4 implements ActionListener {
public void actionPerformed(ActionEvent e)
{
mC = new mpuChecker();
mC.CheckMpu(path2, textField_1.getText(),2);
setVisible(false);
}
}
public void refresh(String pane1) {
textArea_1.append(pane1 + "\n");
System.out.println(getOldTags().size());
System.out.println(oldTags.size());
//both print out zero when called second
}
public void updateTextArea(final String text) {
textArea_2.append(text + "\n");
oldTags.add(text);
System.out.println(oldTags.size());
//prints out the correct arraylist size
}
}
}
//second class which calls updateTextArea and refresh
public class mpuChecker {
private MPUComp mC = new MPUComp();
public void CheckMpu(String path, String searchToken, int form)
{
// Print the text to the appropriate text-area either 1 or 2
public void ary1(int path)
{
if(path == 1)
{
for(int l = 0; l < midTags.size(); l++)
{
mC.refresh(midTags.get(l));
}
}
if(path == 2)
{
for(int lk = 0; lk < midTags2.size(); lk++)
{
mC.updateTextArea(midTags2.get(lk));
}
}
}
}
【问题讨论】:
-
这不重要我删除它我试图使用getter/setter
-
这段代码中有一堆未定义的变量,例如checkmpu(..)中的midTags;你能补充一下这些是什么吗?我不是很理解你的代码。对于您没有打印相应方法的方法调用也是如此。
-
欢迎来到 SO!为了尽快获得更好的帮助,请发帖SSCCE。
-
请学习java命名约定并遵守它们
标签: java swing class user-interface arraylist