How can I display the questions in the JLabel
from the txtfile?
Here's my code:
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);
}
}
You're calling the wrong setVisible(true) method. Content Pane containers are by default always visible, you need to make the JFrame visible.
Instead of
con.setVisible(true);
you want
this.setVisible(true);
because "this" refers to the JFrame, which you never set visible. The content pane will automatically be visible then.
Also, there are some standard operations for JFrames you should know about:
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"
This will surely make your quiz window display. And it looks like you've added the JLabels properly, so those will show up too. But you really should do what the comments recommend and make separate classes because this class is just too big and complicated. It tries to handle JFrames, JPanels, JLabels, questions/answers, events, and even file input! No wonder you are confused!
More information on making JFrames is at
http://download.oracle.com/javase/tutorial/uiswing/components/frame.html