从文本文件获取信息到JLabel

时间:2011-02-24 14:19:41

标签: java swing jframe jlabel

祝大家好日子!我正在创建一个quizzer,我的代码中有一些问题。如何在txtfile中显示JLabel中的问题?请检查我的代码..

这是我的代码:

package splashdemo;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class quiz extends JFrame implements ActionListener{
        private int[] aNumbers=new int[100];
    private String[] logicQ=new String[50];
    private  String[] logicA=new String[50];
    private String[] logicB=new String[50];
    private String[] logicC=new String[50];
    private String[] logicD=new String[50];
    private char[] logicAns=new char[50];
        private char strAns;
        private char cAns;
        private int score=0;
    public Scanner den= new Scanner(System.in);
        public JLabel no= new JLabel();
        public JLabel q= new JLabel();    
        public JLabel q1= new JLabel();
        public JLabel q2= new JLabel();
        public JLabel q3= new JLabel();
        public JLabel q4= new JLabel();
        //public JLabel a= new JLabel();
        JTextField ans=new JTextField(12);
        JButton button= new JButton("Continue");
        public Container con= getContentPane();
   public quiz(){  
       super("Quiz");
        con.setVisible(true);      
       logicReader();
   }
   public static void main(String[] args){


   }

public void logicReader(){
                //con x=new con();
        File oFile=new File("C:\\Users\\NEO\\Documents\\NetBeansProjects\\SplashDemo\\logic.txt");
        FileInputStream fis=null;
        BufferedInputStream bis=null;
        DataInputStream dis=null;
                score=0;
        int iIndex=0;
        String strTemp;
        try{
            fis=new FileInputStream(oFile);
            bis=new BufferedInputStream(fis);
            dis=new DataInputStream(bis);

        while(dis.available()!=0){

            logicQ[iIndex]=dis.readLine();
            logicA[iIndex]=dis.readLine();
            logicB[iIndex]=dis.readLine();
            logicC[iIndex]=dis.readLine();
            logicD[iIndex]=dis.readLine();                     
            strTemp=dis.readLine();
            logicAns[iIndex]=strTemp.charAt(0);
            iIndex++;
        }
                boolean blnFound=false;
                int iSlot=0;
                for(int g=0; g<=99; g++)
                aNumbers[g]=Generator(4);
                int[] aUnique=new int[100];
                    for(int a=0; a<aNumbers.length; a++){
                    blnFound=false;
                        if(a==0){
                            aUnique[iSlot]=aNumbers[a];iSlot++;
                        } else {
                            for(int b=0; b<iSlot ;b++){
                            if(aNumbers[a]==aUnique[b]){
                    blnFound=true; break;
                    }
               }
              if(blnFound==false){
              aUnique[iSlot]=aNumbers[a]; iSlot++;
            }
            }
            }
                JOptionPane.showMessageDialog(null,"\t\tYou have chosen to take the Quiz");
            for(int a=0; a<=4; a++){
                    int iIdx=aUnique[a];
        //System.out.print("\t\t"+(a+1) + ". " + logicQ[iIdx] + "\n" + logicA[iIdx] + "\n" + logicB[iIdx] + "\n" + logicC[iIdx] + "\n" + logicD[iIdx] + "\nAnswer:");
        //strAns=den.nextLine();
                 no.setText(Integer.toString(a+1));
                 q.setText(logicQ[iIdx]);
                 con.add(q);
                 q1.setText(logicA[iIdx]);
                 con.add(q1);
                 q2.setText(logicB[iIdx]);
                 con.add(q2);
                 q3.setText(logicC[iIdx]);
                 con.add(q3);
                 q4.setText(logicD[iIdx]);
                 con.add(q4);
                 con.add(ans);
                 con.add(button);
                 button.addActionListener(this);
                 ans.addActionListener(this);
                 strAns=logicAns[iIdx];
                 con.setLayout(new FlowLayout());
                 con.setVisible(true);
        //cAns=strAns.charAt(0);

            /*if(logicAns[iIdx]==Character.toUpperCase(cAns)){
                                score++;
                Display("Correct You are qualified to the next round!");
                                Display("Your Score is: "+score+"/15");
                                 if(a==14){
                                Display("\t\t\tCongratulations!! You earn 100,000.00\n\t\t\tYou got a Perfect Score!!");
                                }
            }else{
                Display("Sorry! YOu have inputted a Wrong Answer!");
                                System.out.println("The correct answer is: "+logicAns[iIdx]);
                                break;
            }*/
        }

        fis.close();
        bis.close();
        dis.close();


        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
public void actionPerformed(ActionEvent c){
    String name= ans.getText();
    cAns= name.charAt(0);
    q.removeAll();
    q1.removeAll();
    q2.removeAll();
    q3.removeAll();
    q4.removeAll();
    //String greet="Hello, "+name;
    //question.setText("Thank YOU");
    //p.setText("Done");
    if(strAns==Character.toUpperCase(cAns)){
                         score++;
             q.setText("Your answer is correct!");

            }else{
                //Display("Sorry! YOu have inputted a Wrong Answer!");
                                //System.out.println("The correct answer is: "+logicAns[iIdx]);
                                //break;
            }
    //NewJFrame j= new NewJFrame();
    //j.setVisible(true);
}
public static int Generator(int iNum){
    int iRandomnum=(int) (iNum * Math.random())+1;
    return iRandomnum;
}
public static void Display(String Mes){
    System.out.println(Mes);
}
public static void Display(int Mes){
    System.out.println(Mes);
}

}

先谢谢!!!!

1 个答案:

答案 0 :(得分:1)

你正在调用错误的setVisible(true)方法。默认情况下,内容窗格容器始终可见,您需要使JFrame可见。

而不是

con.setVisible(true);

你想要

this.setVisible(true);

因为“this”指的是你从未设置过的JFrame。然后,内容窗格将自动显示。

此外,您应该了解JFrame的一些标准操作:

JFrame.setSize(width, height); 
//sets the size of the window in pixels

JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//turns on window closing via the "x"

这肯定会让你的测验窗口显示出来。看起来你已经正确添加了JLabel,所以这些也会出现。但你真的应该做评论推荐的内容并制作单独的类,因为这个类太大而且太复杂了。它试图处理JFrame,JPanels,JLabels,问题/答案,事件,甚至文件输入!难怪你感到困惑!

有关制作JFrame的更多信息,请访问

http://download.oracle.com/javase/tutorial/uiswing/components/frame.html

相关问题