从私有操作访问类变量执行方法



在下面的代码中,我不知道为什么从jButton1ActionPerformed方法访问变量uNomba和list的值为NULL。 我将不胜感激您的帮助,关于如何成功执行"new NewPlayer(uNomba,count,check,list).load();",以便将所有值传递给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);         
}

谢谢斯格罗。这是我刚刚添加的内容:我在在新玩家类中: Continue ct = new Continue (new NewPlayer(uNomba, i, moveon, numlist));

在继续类中, 私人新玩家 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.
}

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

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

而这:

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

这不编译:

Continue ctn = new Continue();

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

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

最新更新