禁用按钮并在ArrayList迭代结束时在文本字段中显示消息

时间:2013-12-10 02:44:54

标签: java netbeans swingbuilder

我正在编写一个测验程序,打开一个包含问题和答案的文本文件。问题和答案被放入数组中,并在按钮点击时放入文本字​​段。打开文件启用Q按钮。

页面底部的计数器显示文件中的问题总数以及每个问题的计数。

当您单击“Q”按钮时,问题将被放置在Q文本字段中,Q按钮被禁用,应答按钮(“A”)被启用,“currentQuestion”计数器被加1。单击A按钮时答案将显示在答案字段中,A按钮将被禁用并且Q按钮已启用。再次按Q按钮将清除上一个问题的答案文本,启用A按钮,并禁用自身。它像这样来回走,直到它到达arraylist中的最后一个问题和答案。

在显示最后一个问题和答案后,我需要再次按下Q按钮并显示文本“那是所有人!”在问题文本字段中,同时禁用Q和A按钮。

我设法使用方法getEndStatus()(这个布尔方法是赋值的要求)来检查是否已经达到最后一个问题。我可以使用它将文本放在问题文本字段中。但是,我似乎无法弄清楚如何禁用这两个按钮。我已经在Q按钮单击的形式的源代码中尝试了if语句(也尝试使用A按钮单击)如果getEndStatus()方法返回true则禁用该按钮但是在最终单击产生最终消息之前禁用该按钮。

我还需要在最后用于查找文件的同一文件夹中对JFileChooser的每次后续调用启动。我不知道如何去做这件事。

非常感谢任何帮助!提前谢谢!

这是来自班级建设者的来源代码:

package business;

import java.io.*;
import java.util.ArrayList;

public class Quiz {
private ArrayList<String> questions, answers;
private int qCount, qNumber;
private String errmsg, actmsg;

public Quiz(String fn){
    //fn = file name with full path
    this.qCount = 0;
    this.qNumber = 0;
    this.errmsg = "";
    this.actmsg = "";
    try{
        BufferedReader in = new BufferedReader(
                new FileReader(fn));
        questions = new ArrayList<String>();
        answers = new ArrayList<String>();

        String s = in.readLine();
        while (s != null){
            qCount++;
            questions.add(s); //put question in array list
            answers.add(in.readLine()); //read and post answer
            s = in.readLine(); //next question
        }//end while loop
        in.close();
    }catch (Exception e){
        errmsg = "Error opening file: " + e.getMessage();
    }//end try catch
    if (qCount > 0){
        qNumber =1;          
        actmsg = "Quiz file read. Next number = " + qNumber;
    }//end if statement
}//end of constructor

public int getQuizCount(){
    return this.qCount;
}//end getQuizCount()

public String getActionResult(){
    if (qNumber < qCount){
    actmsg = "Quiz file read. Next number = " + qNumber;
    }else if(getEndStatus()){
        actmsg = "That's all Folks!";
    }else{
    actmsg = "Quiz file read. Next number = " + qCount;
    }
    return this.actmsg;
}//end getActionResult()

public String getErrorMsg(){
    return this.errmsg;
}//end getErrorMsg()

public String getQuestion(){
    if (qNumber == 0){
        return "No Questions on File.";
    }else if(getEndStatus()){
        return "That's All Folks!";
    }else{    
    qNumber++;
    return this.questions.get(qNumber-2);
    }
}//end getQuestion()

public String getAnswer(){
    if(qNumber == 0){
        return "No Answers on File.";
    }
    return this.answers.get(qNumber-2); 
}

public int getCurrCount(){
    return qNumber-1;
}

public boolean getEndStatus(){
    if(qNumber <= qCount){
        return false;
    }
    return true;
}
}//end class()

以下是与表格视图相关的来源代码:

private void jmnuOpenActionPerformed(java.awt.event.ActionEvent evt) {                                         
statusMessageLabel.setText("");

JFileChooser f = new JFileChooser(".");
f.setDialogTitle("Select Quiz File");
FileNameExtensionFilter filter = 
        new FileNameExtensionFilter("Text Files (*.txt)","txt");
f.setFileFilter(filter);
JDialog dg = new JDialog();
int rval = f.showOpenDialog(dg);

if (rval == JFileChooser.CANCEL_OPTION){
    jtxtFile.setText("");
    statusMessageLabel.setText("Open Canceled.");
}else {
    jtxtFile.setText(f.getSelectedFile().getAbsolutePath());
    q = new Quiz(f.getSelectedFile().getAbsolutePath());
    if (q.getErrorMsg().isEmpty()){
        jtxtTotQ.setText(String.valueOf(q.getQuizCount()));
        jbtnQ.setEnabled(true);
        statusMessageLabel.setText(q.getActionResult());
    }else{
        statusMessageLabel.setText(q.getErrorMsg());
        jtxtTotQ.setText("");
        jtxtCurrQ.setText("");
    }//end if else    
}//end if else 
jtxtQ.setText("");
jtxtA.setText("");
}

private void jbtnQActionPerformed(java.awt.event.ActionEvent evt) {                                      
jtxtQ.setText(q.getQuestion());
jtxtCurrQ.setText(String.valueOf(q.getCurrCount()));
statusMessageLabel.setText(q.getActionResult());
jtxtA.setText("");
jbtnQ.setEnabled(false);
if (q.getEndStatus()){
    jbtnA.setEnabled(false);
}else{
    jbtnA.setEnabled(true);
}
}                                     

private void jbtnAActionPerformed(java.awt.event.ActionEvent evt) {                                      
jtxtA.setText(q.getAnswer());
jbtnA.setEnabled(false);
jbtnQ.setEnabled(true);
}                                                     

0 个答案:

没有答案