|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package com.luckytaorem.quiz_application; |
| 7 | +import javax.swing.JFrame; |
| 8 | +import java.awt.event.ActionListener; |
| 9 | +import java.awt.event.ActionEvent; |
| 10 | +import javax.swing.ButtonGroup; |
| 11 | +import javax.swing.JButton; |
| 12 | +import javax.swing.JLabel; |
| 13 | +import javax.swing.JOptionPane; |
| 14 | +import javax.swing.JRadioButton; |
| 15 | +/** |
| 16 | + * |
| 17 | + * @author Taorem |
| 18 | + */ |
| 19 | +public class QuizTest extends JFrame implements ActionListener{ |
| 20 | + |
| 21 | + JLabel label; |
| 22 | + JRadioButton radioButtons[] = new JRadioButton[5]; |
| 23 | + JButton btnNext,btnResult; |
| 24 | + ButtonGroup bg; |
| 25 | + int count = 0, current = 0, x=1,y=1,now=0; |
| 26 | + int m[]= new int[10]; |
| 27 | + |
| 28 | + |
| 29 | + QuizTest(String s){ |
| 30 | + super(s); |
| 31 | + label = new JLabel(); |
| 32 | + add(label); |
| 33 | + bg = new ButtonGroup(); |
| 34 | + for(int i=0;i<5;i++){ |
| 35 | + radioButtons[i]= new JRadioButton(); |
| 36 | + add(radioButtons[i]); |
| 37 | + bg.add(radioButtons[i]); |
| 38 | + |
| 39 | + } |
| 40 | + btnNext = new JButton("Next"); |
| 41 | + btnResult = new JButton("Result"); |
| 42 | + btnResult.setVisible(false); |
| 43 | + btnResult.addActionListener(this); |
| 44 | + btnNext.addActionListener(this); |
| 45 | + add(btnNext); |
| 46 | + add(btnResult); |
| 47 | + setData(); |
| 48 | + label.setBounds(30,40,450,20); |
| 49 | + radioButtons[0].setBounds(50,80,450,20); |
| 50 | + radioButtons[1].setBounds(50,110,200,20); |
| 51 | + radioButtons[2].setBounds(50,140,200,20); |
| 52 | + radioButtons[3].setBounds(50,170,200,20); |
| 53 | + btnNext.setBounds(100,240,100,30); |
| 54 | + btnResult.setBounds(270,240,100,30); |
| 55 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 56 | + setLayout(null); |
| 57 | + setLocation(250,100); |
| 58 | + setVisible(true); |
| 59 | + setSize(600,350); |
| 60 | + } |
| 61 | + |
| 62 | + void setData(){ |
| 63 | + radioButtons[4].setSelected(true); |
| 64 | + if(current==0){ |
| 65 | + label.setText("Question 1: Correct Radio Button is A"); |
| 66 | + radioButtons[0].setText("A"); |
| 67 | + radioButtons[1].setText("B"); |
| 68 | + radioButtons[2].setText("C"); |
| 69 | + radioButtons[3].setText("D"); |
| 70 | + } |
| 71 | + if(current==1){ |
| 72 | + label.setText("Question 2: Correct Radio Button is B"); |
| 73 | + radioButtons[0].setText("A"); |
| 74 | + radioButtons[1].setText("B"); |
| 75 | + radioButtons[2].setText("C"); |
| 76 | + radioButtons[3].setText("D"); |
| 77 | + } |
| 78 | + label.setBounds(30,40,450,20); |
| 79 | + for(int i=0,j=0;i<=90;i+=30,j++){ |
| 80 | + radioButtons[j].setBounds(50,80+i,200,20); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + boolean checkAns(){ |
| 85 | + if(current==0){ |
| 86 | + return(radioButtons[0].isSelected()); |
| 87 | + } |
| 88 | + if(current==1){ |
| 89 | + return(radioButtons[1].isSelected()); |
| 90 | + } |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + public static void main(String[] args){ |
| 95 | + new QuizTest("Testing for Quiz Application"); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void actionPerformed(ActionEvent e) { |
| 100 | + if(e.getSource()==btnNext){ |
| 101 | + if(checkAns()) |
| 102 | + count = count +1; |
| 103 | + current++; |
| 104 | + setData(); |
| 105 | + if(current==1){ |
| 106 | + btnNext.setEnabled(false); |
| 107 | + btnResult.setVisible(true); |
| 108 | + btnResult.setText("Result"); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if(e.getActionCommand().equals("Result")){ |
| 113 | + if(checkAns()) |
| 114 | + count = count +1; |
| 115 | + current++; |
| 116 | + JOptionPane.showMessageDialog(this,"Correct Answer are "+count); |
| 117 | + System.exit(0); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments