2016-05-12 64 views
0

用于测验的代码是在一个名为tviewSetvisible()将不起作用。错误的位置:QUIZIQ

package tview.quiz; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 
import java.util.*; 

class Quiz extends JFrame implements ActionListener{ 
     JPanel panel; 
     JPanel panelresult; 
     JRadioButton choice1; 
     JRadioButton choice2; 
     JRadioButton choice3; 
     JRadioButton choice4; 
     ButtonGroup bg; 
     JLabel lblmess; 
     JButton btnext; 
     String[][] qpa; 
     String[][] qca; 
     int qaid; 
     HashMap<Integer, String> map; 

     Quiz(){ 
        initializedata(); 
        setTitle("Quiz Program"); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
        setSize(430,350); 
        setLocation(300,100); 
        setResizable(false); 
        Container cont=getContentPane(); 
        cont.setLayout(null);   
        cont.setBackground(Color.GRAY); 
       bg=new ButtonGroup();  
       choice1=new JRadioButton("Choice1",true); 
       choice2=new JRadioButton("Choice2",false); 
       choice3=new JRadioButton("Choice3",false); 
       choice4=new JRadioButton("Choice4",false); 
       bg.add(choice1); 
       bg.add(choice2); 
       bg.add(choice3); 
       bg.add(choice4); 
       lblmess=new JLabel("Choose a correct anwswer"); 
       lblmess.setForeground(Color.BLUE); 
       lblmess.setFont(new Font("Arial", Font.BOLD, 11)); 
       btnext=new JButton("Next"); 
       btnext.setForeground(Color.GREEN);     
       btnext.addActionListener(this); 
       panel=new JPanel(); 
       panel.setBackground(Color.LIGHT_GRAY); 
       panel.setLocation(10,10); 
       panel.setSize(400,300); 
       panel.setLayout(new GridLayout(6,2)); 
       panel.add(lblmess); 
       panel.add(choice1); 
       panel.add(choice2); 
       panel.add(choice3); 
       panel.add(choice4); 
       panel.add(btnext); 
       cont.add(panel); 
       setVisible(true); 
       qaid=0; 
       readqa(qaid); 

     } 

    public void actionPerformed(ActionEvent e){ 

        if(btnext.getText().equals("Next")){ 
           if(qaid<9){ 

              map.put(qaid,getSelection()); 
              qaid++; 
              readqa(qaid); 
              } 
           else { 
              map.put(qaid,getSelection()); 
              btnext.setText("Show answers"); 

             } 
           } 
        else if(btnext.getText().equals("Show answers")) 
           new Report(); 


     } 



    public void initializedata(){ 
        //qpa stores pairs of question and its possible answers 
        qpa=new String[10][5]; 

        qpa[0][0]="Test"; 
        qpa[0][1]="A"; 
        qpa[0][2]="B"; 
        qpa[0][3]="C"; 
        qpa[0][4]="D"; 

        //qca stores pairs of question and its correct answer 
        qca=new String[10][2]; 

        //create a map object to store pairs of question and selected answer 
        map=new HashMap<Integer, String>(); 

        } 
    public String getSelection(){ 
        String selectedChoice=null; 
        Enumeration<AbstractButton> buttons=bg.getElements(); 
        while(buttons.hasMoreElements()) 
        { 
        JRadioButton temp=(JRadioButton)buttons.nextElement(); 
        if(temp.isSelected()) 
           { 
              selectedChoice=temp.getText(); 
           } 
        } 
        return(selectedChoice); 
     } 


    public void readqa(int qid){ 
        lblmess.setText(" "+qpa[qid][0]); 
        choice1.setText(qpa[qid][1]); 
        choice2.setText(qpa[qid][2]); 
        choice3.setText(qpa[qid][3]); 
        choice4.setText(qpa[qid][4]); 
        choice1.setSelected(true); 
     } 
    public void reset(){ 
        qaid=0; 
        map.clear(); 
        readqa(qaid); 
        btnext.setText("Next"); 
        } 
    public int calCorrectAnswer(){ 
        int qnum=10; 
        int count=0; 
        for(int qid=0;qid<qnum;qid++) 
           if(qca[qid][1].equals(map.get(qid))) count++; 
        return count; 
     } 

    public class Report extends JFrame{ 
        Report(){ 
           setTitle("Answers"); 
           setSize(850,550); 
           setBackground(Color.WHITE); 
           addWindowListener(new WindowAdapter(){ 
                 public void windowClosing(WindowEvent e){ 
                    dispose(); 
                    reset(); 
                 } 
              }); 
           Draw d=new Draw();         
           add(d); 
           setVisible(true); 
           } 


       class Draw extends Canvas{ 
           public void paint(Graphics g){ 
              int qnum=10; 
              int x=10; 
              int y=20; 
              for(int i=0;i<qnum;i++){ 
                 //print the 1st column 
                 g.setFont(new Font("Arial",Font.BOLD,12));           
                 g.drawString(i+1+". "+qca[i][0], x,y); 
                 y+=30;   
                 g.setFont(new Font("Arial",Font.PLAIN,12));        
                 g.drawString("  Correct answer:"+qca[i][1], x,y); 
                 y+=30; 
                 g.drawString("  Your answer:"+map.get(i), x,y); 
                 y+=30; 
                 //print the 2nd column 
                 if(y>400) 
                 {y=20; 
                  x=450; 
                 } 

              } 
              //Show number of correct answers 
              int numc=calCorrectAnswer(); 
              g.setColor(Color.BLUE); 
              g.setFont(new Font("Arial",Font.BOLD,14)); 
              g.drawString("Number of correct answers:"+numc,300,500); 


           } 
        } 

     } 




} 


public class QuizIQ{ 

     public static void main(String args[]){ 

     Quiz qz = new Quiz(); 

     JFrame frame = new JFrame(); 
     frame.setSize(300, 300); // Set the size of the window 
     frame.add(qz); 
     frame.setVisible(true); 

     } 


} 

接着一个的JavaSource包我有一个按钮被点击时,我的主包内调用接口的JFrame应该显示竞猜型的变量QZ 。

private void quizBttnActionPerformed(java.awt.event.ActionEvent evt)  
{           
    new QuizIQ().setVisible(true); 
} 

我不断收到,说找不到符号错误, 符号:方法调用setVisible(布尔) 位置:类型的变量QZ QUIZIQ

可能有人请帮助我这个错误出来吗?

+0

Java GUI必须使用不同语言环境中使用不同PLAF的不同OS,屏幕大小,屏幕分辨率等。因此,它们不利于像素的完美布局。请使用布局管理器或[它们的组合](http://stackoverflow.com/a/5630271/418556)以及[white space]的布局填充和边框(http://stackoverflow.com/a/17874718/ 418556)。 –

+0

1)为了更快地获得更好的帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。 2)使用合乎逻辑的一致形式缩进代码行和块。缩进旨在使代码的流程更易于遵循! 3)源代码中的单个空白行是需要的。 '{'之后或'}'之前的空行通常也是多余的。 4)请参阅[使用多个JFrames,好/坏实践?](http://stackoverflow.com/q/9554636/418556) –

回答

1

测验类包含您的JFrame。您通过实例化Quiz来使测验可见。

Quiz qz = new Quiz(); 

它扩展了JFrame并将自行显示。看看它的管理者。它已经调用setVisible()。

您的类QuizIQ改为不扩展JFrame,并且没有方法setVisible()。

+0

按钮单击事件是在一个单独的JForm中,测验似乎不是点击按钮后显示。 –

+0

我试过这个,但它没有工作tview.quiz.QuizIQ qz = new tview.quiz.QuizIQ(); –

+0

你不能在另一个JFrame中使用JFrame,看到这篇文章:http://stackoverflow.com/questions/17287248/jframe-inside-another-jframe在JFrame中使用JFrame作为实际的窗口和JPane。 – Fuzzzzel