从私有actionPerformed方法访问类变量

时间:2012-10-03 19:32:07

标签: methods null private

在下面的代码中,我不知道为什么从jButton1ActionPerformed方法访问时变量uNomba和list的值为NULL。关于如何成功执行“new NewPlayer(uNomba,count,check,list).load();”我将非常感谢您的帮助。这样所有的值都传递给NewPlayer类。谢谢。

第一个类 - 即NewPlayer类

package mysound;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class NewPlayer extends JPanel implements KeyListener, Runnable{
boolean isUpPressed, isDownPressed, isSpacePressed, isDone;
static JFrame f;
int spacebars=0;
boolean within;
public List spacebarLogMs = new ArrayList(); 
public List numSbar = new ArrayList();
Calendar cal = Calendar.getInstance();
LogResult logNow = new LogResult();
String directory;
String tabname; //table name used in the database connection
String bdir;
private int uNomba; //user number obtained from NewSound class
private String target;
private int incr;
private int userno;
private boolean moveon=true;
private List randlist;
private List numlist;

public void load() {
    f = new JFrame();
    f.setSize(600,300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(this);
    f.setVisible(true);     
    setFocusable(true);
    addKeyListener(this);
    new Thread(this).start();
}

public NewPlayer() {   

}

public NewPlayer(int UNOMBA, List NUMLIST){
    this.uNomba = UNOMBA; //user number obtained from NewSound class
    this.numlist=NUMLIST;
}    
public NewPlayer(int USERNO, int INCR, boolean MOVEON, List NUMLIST){
    this.userno=USERNO;
    this.incr=INCR;
    this.moveon=MOVEON;
    this.numlist=NUMLIST;
}

public void keyTyped(KeyEvent ke) {
}

public void keyPressed(KeyEvent ke) {
    switch(ke.getKeyCode()) {
        case KeyEvent.VK_UP: isUpPressed = true; break;
        case KeyEvent.VK_DOWN: isDownPressed = true; break;
        case KeyEvent.VK_SPACE: isSpacePressed = true; 

        numSbar.add(System.currentTimeMillis());
            System.out.println("That was a spacebar. "+spacebars++);
            System.out.println("Current time: "+numSbar);
            break;
    }
}

public void keyReleased(KeyEvent ke) {
    switch(ke.getKeyCode()) {
        case KeyEvent.VK_UP: isUpPressed = false; break;
        case KeyEvent.VK_DOWN: isDownPressed = false; break;
        case KeyEvent.VK_SPACE: isSpacePressed = false; break;
    }
}

public void closePrj(){
    f.dispose();
}

public void run() { //introduce a target sound

   String targetChoice;
   int tIndex;       
   int i;
   bdir="C:\\Users\\Abiodun\\Desktop\\testdata\\main\\zero\\atext\\"; //dir for text files


   MainPlayer items =  new MainPlayer (uNomba);

   i=incr;
   while(moveon){
       System.out.println("Counter i: "+i+" Numlist: "+numlist);
       if (i<numlist.size()){
            int num = (int) numlist.get(i);
            System.out.println("Num :"+num);
            items.selectTarget(num);           
            items.selectChallenge(num);
            items.playChallenge();
            new WriteTime(bdir).tagTime(numSbar);
            items.dataLogger();
            moveon=false;
            new Continue (uNomba, i, moveon, numlist).load();
       }

   }
}
}

第二类,即继续课

public class Continue extends javax.swing.JDialog {
    private int count;
    private int usernumb;
    private boolean check;
    private int uNomba;
    private String cdirectory;
    private String cbdir;
    private String ctabname;
    private String ctarget;
    private List list;

/**
 * Creates new form Continue
 */
public Continue(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
}

public Continue(int CUNOMBA, int COUNT, boolean CHECK, List NLIST){
    this.uNomba = CUNOMBA; //user number obtained from NewSound class
    this.count=COUNT;
    this.check=CHECK;
    this.list=NLIST;
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    new NewPlayer().setVisible(false);//closePrj();
    count++;
    check=true;
    new NewPlayer(uNomba, count, check, list).load();        
            System.out.println("Continue: UserNumber: "+uNumb+", Count: "+count+", Check: "+check+", nList"+lst);
    this.setVisible(false);         
}
谢谢sgroh。这是我刚刚添加的内容:我创建了以下内容 在NewPlayer类中:      继续ct = new继续(新的NewPlayer(uNomba,i,moveon,numlist));

在继续课堂上,     私人NewPlayer np;

public Continue (NewPlayer npy){
    this.npy=np;
}

回顾一下,我遇到的主要问题是我无法访问从Continue类传递给NewPlayer类的值。我在Continue类中测试了以下构造函数中的值,但在Continue类中没有测试其他任何地方。

public Continue(int CUNOMBA, int COUNT, boolean CHECK, List NLIST){
    this.uNomba = CUNOMBA; //user number obtained from NewSound class
    this.count=COUNT;
    this.check=CHECK;
    this.nlist=NLIST;


   System.out.println("Continue-constructor - uNomba: "+uNomba+", nList: "+list); //works fine! but not outside this constructor.
}

2 个答案:

答案 0 :(得分:0)

这段代码甚至可以编译,你没有构造函数默认值(没有字段)。 这样:

public Continue(java.awt.Frame parent, boolean modal) {

和这:

public Continue(int CUNOMBA, int COUNT, boolean CHECK, List NLIST){

这个小编译:

Continue ctn = new Continue();

答案 1 :(得分:0)

您必须使用正确的构造函数创建Continue对象,或者创建Default构造函数。

您还需要在System.out.println中打印不存在的变量uNumb。