My Quiz Program in Java

Java programming topics
Post Reply
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

My Quiz Program in Java

Post by Nipuna » Fri Mar 23, 2012 12:34 pm

Hi friends.

I made a simple program to gain my knowledge and test my knowledge.

it's kinda like a MCQ paper. I only added one question to it :) I used Netbeans but I only used it as a text editor because I like it. I didn't use any other feature of it. Because if I use them I would forget so many important stuff because I am a student not a professional programmer. :)

Here is a screenshot of the program 8-) Not very complicated or advance one just a simple one ;)
Screen_shot.jpg
Screen_shot.jpg (30.93 KiB) Viewed 4047 times
Here is the source.

GUI.java

Code: Select all

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

public class GUI extends JFrame implements ActionListener {
    
   private JLabel display,ans1,ans2;
   private JTextField input;
   private JButton actionButton;
   private String location[] = {"q.jpg","ans1.jpg","ans2.jpg"};
   private Icon images[] = {new ImageIcon(getClass().getResource(location[0])),
                            new ImageIcon(getClass().getResource(location[1])),
                            new ImageIcon(getClass().getResource(location[2]))
                           };
   private JPanel leftPanel,rightPanel,bottomPanel;
   
   
   public GUI(){
       
       super("Quiz Paper");
       setSize(350,240);
       
       display = new JLabel();
       display.setIcon(images[0]);
       
       leftPanel = new JPanel();
       leftPanel.setBackground(Color.BLUE);
       leftPanel.add(display, BorderLayout.CENTER);
       add(leftPanel, BorderLayout.WEST);
       
       ans1 = new JLabel(images[1]);
       ans2 = new JLabel(images[2]);
       
       rightPanel = new JPanel();
       rightPanel.add(ans1);
       rightPanel.add(ans2);
       rightPanel.setBackground(Color.BLUE);
       add(rightPanel, BorderLayout.CENTER);
       
       input = new JTextField("Type the answer 1 or 2");
       actionButton = new JButton("Click to check the answer");
       actionButton.addActionListener(this);
       
       bottomPanel = new JPanel();
       bottomPanel.add(input);
       bottomPanel.add(actionButton);
       add(bottomPanel, BorderLayout.SOUTH);
   }
   
   public void actionPerformed(ActionEvent event){
       
       try{
           int answer = Integer.parseInt(input.getText());

           if(answer == 1)
               JOptionPane.showMessageDialog(null,"Your Answer is Correct!!!" ,"Your Status" ,JOptionPane.PLAIN_MESSAGE);
           else if(answer == 2)
               JOptionPane.showMessageDialog(null, "Your Answer is Wrong!!", "Your Status",JOptionPane.PLAIN_MESSAGE);
           else
               JOptionPane.showMessageDialog(null, "Try Again!!", "Your Status",JOptionPane.PLAIN_MESSAGE);
       }
       catch(Exception e){
           
       }
   }
}
Quiz_Paper_Main.java

Code: Select all

import javax.swing.JFrame;

public class Quiz_Paper_Main{

    public static void main(String args[]){
        
        GUI gui = new GUI();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        
    }
}
There must be so many places where I didn't use Java best practices, Like adding comments and etc.. :) But I only see not adding comments as a bad practice and don't see any other. ;) You know I am a beginner :)

Oh almost forgot, I am adding the source too, So it maybe useful to somebody else. (It has, Source files, Images and .class files)


Thanks
Quiz_Paper.zip
(10.25 KiB) Downloaded 464 times
PS. I like to know your ideas about this :)
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: My Quiz Program in Java

Post by Neo » Mon Mar 26, 2012 12:08 am

Very nice Nipuna. Keep up the good work!
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: My Quiz Program in Java

Post by Nipuna » Mon Mar 26, 2012 1:01 am

Thank you :)
Post Reply

Return to “Java Programming”