2011-02-24 7 views
-1

こんにちは!テキストファイルから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); 
} 

} 
:ここ

は私のコードです..私はquizzerを作成していますし、私はあなたが私のコードをチェックしてくださいすることができ、私はtxtfileからJLabelの中の質問を表示することができ、私のcode..Howでいくつかの問題がありますか?

ありがとうございました!!!!

+1

でいるのですか?あなたのコードに関連する行を指摘できますか?エラーがある場合は、スタックトレースも投稿してください。 –

+4

まず、あなたのメインメソッドは空です。 – mre

+0

私は私のメインクイズq = new quiz()に追加しました。 JOptionPaneだけが「あなたはクイズを取ることを選んだ」と言っていて、フレームを表示していません...ラベルに質問を表示する方法を最初に知りたいです。 – steph22

答えて

1

あなたは間違ったsetVisible(true)メソッドを呼び出しています。コンテンツペインコンテナは、デフォルトで常に表示されます.JFrameを表示させる必要があります。

代わりの

con.setVisible(true); 

"これは" あなたが見え設定されていないのJFrame、を意味しているため、あなたが

this.setVisible(true); 

をしたいです。コンテンツペインが自動的に表示されます。

はまた、あなたが知っておくべきJFramesのためのいくつかの標準的な操作があります

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" 

これは確かにあなたのクイズウィンドウの表示を行います。 JLabelsを正しく追加したようですので、それらも表示されます。しかし、このクラスはあまりにも大きくて複雑なので、コメントが推奨することを実際に行い、別のクラスを作成する必要があります。 JFrames、JPanels、JLabels、質問/回答、イベント、さらにはファイル入力まで扱います!あなたが混乱しているのも不思議ではない!作っJFrames上

詳しい情報は正確に問題は何

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

+0

.great !!それは混乱しないように私は彼らの提案を行います。 – steph22

関連する問題