【问题标题】:Get info from a text file to JLabel从文本文件获取信息到 JLabel
【发布时间】:2011-02-24 14:19:40
【问题描述】:

如何在 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);
}

}

【问题讨论】:

  • 到底是什么问题?您能否指出代码中的相关行?如果有错误,请同时发布堆栈跟踪。
  • 对于初学者来说,你的 main 方法是空的。
  • 我在我的主要测验中添加了 q= new quiz();并且只有 JOptionPane 说“您选择参加测验”并且它不显示我的框架...我想先知道如何在标签中显示问题..

标签: java swing jframe jlabel


【解决方案1】:

您调用了错误的 setVisible(true) 方法。 Content Pane 容器默认始终可见,您需要让 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"

这肯定会让你的测验窗口显示出来。看起来您已经正确添加了 JLabels,所以这些也会显示出来。但是你真的应该按照 cmets 的建议做单独的类,因为这个类太大太复杂了。它尝试处理 JFrame、JPanels、JLabels、问题/答案、事件,甚至文件输入!难怪你会糊涂!

更多关于制作 JFrames 的信息在

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多