Jump to content

JDialog Convert to Jframe (2) part...

- - - - -

  • Please log in to reply
No replies to this topic

#1
an2kenz_143

an2kenz_143

    Newbie

  • Members
  • PipPip
  • 12 posts
Hey.... guys i just convert now the JDialog to JFrame.... but i used multiple frame... to do that...because it hard for me to do 1 frame and using array in JFrame... conflict in my code.... can u figure out my problem??? in JFrame

This is my code in JDialog that i've already done... i want to covert to JFrame because i want my Quiz game to have a Timer ... so suppose i've code in Jdialog... it impossible to code the timer over that ... heres my Jdialog


import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.ImageIcon;

import javax.swing.JRadioButton;

 

public class Questions extends JFrame{

	JLabel labelawt = new JLabel();

	static int questions = 10;

 

	

	

		public Questions()

		{

			

			setTitle("Java Brain Twister");

			setSize(600,600);

		    setLayout(null);

			setResizable(false);

			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

			

			ImageIcon LBackG = new ImageIcon(getClass().getResource("awt.jpg"));

			JLabel QBackGround = new JLabel( LBackG);

			QBackGround.setBounds(0, 0, 600, 600);

			add(QBackGround);

			setVisible(true);

			

			

			labelawt.setBounds(30,30,100,100);

			add(labelawt);

			

			JRadioButton radio = new JRadioButton();

			

            

			String question[] = {

				

				"What is Java (in regard to Computer Science) ?",

				"What's the difference between an Applet and an application ?",

				"What shortcut we use for importing objects in 'Eclipse' IDE?",

				"What shortcut we use to compile & run a source code in 'Eclipse' IDE?",

				"What is the current version for 'JDK' Kit?",

				"Which option is not an IDE 'Integrated Development Environment'?",

				"Which option is not a feature of JAVA?",

				"What is the name of the component that we use to create dialog box?",

				"What should be the name of the class, if the file name is Hello.java?",

				"Which one is not a method of JOptionPane class?"

				

				

				

			};

			String choices[][] = 

		        {

					

				{"A-) A type of Coffee","B-) Object Oriented Programming","C-) InterActive Website","D-) None Of the Above"},	

				{"A-) An application is only available on Windows","B-) Applets can paint words, applications cannot.","C-) Applets are run over the web.","D-) None Of the Above"},

				{"A-) CTRL + O","B-) ALT + O","C-) CTRL + SHIFT + O","D-) ALT + SHIFT + O"},

				{"A-) ALT + SHIFT + X and J","B-) CTRL + SHIFT + J and X","C-) CTRL + ALT + J and Q","D-)ALT + CTRL + Q"},

				{"A-) JDK 1.4 Update 1","B-) JDK 1.5 Update 10","C-) JDK 1.6 Update 9","D-) JDK 1.6 Update 3"},

				{"A-) Eclipse","B-) JIDE","C-) JCreator","D-) NetBeans"},

				{"A-) Cross Platform","B-) Memory Management","C-) Easy GUI Creation","D-) Object - Oriented Programming"},

				{"A-) JtextField","B-) JOptionPane","C-) JtextPanel","D-) JTextArea"},

				{"A-) _HeLLO","B-) hello","C-) HELLO","D-) Hello"},

				{"A-) showInputDialog","B-) showOptionDialog","C-) showInformDialog","D-) showMessageDialog"},

				

				

		        

		        };

			int QandA []={

					1,2,2,0,3,1,2,1,3,2,2,

			};

 

				

			int a =0;

			int b =0;

			int quest = 0;

			int ans =0;

			int total;

 

				int input;

				float totalrate=0,yes=0,no=0;

				for (int i=0; i<10; i++){

 

					total = i+1;

 

					

					

                input = JOptionPane.showOptionDialog(null, question[i], "Question " +total, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, choices[i], choices[i][0]);	

 

				if (input ==-1){

					int exit=JOptionPane.showConfirmDialog(null,"Are you sure you want to give up quiz?","Quiz", JOptionPane.YES_NO_OPTION);

 

					if (exit==JOptionPane.YES_OPTION){

 

					break;}

 

				} 

				else {  

					if (input == QandA[i]) {

						yes = +1;

						quest=quest+1;

						} else {

							no=no+1;

							ans=ans+1;

 

						}

					}b= i+1;		

			}

				totalrate= (quest*100)/b ;

 

 

 

				JOptionPane.showMessageDialog(

						null,"You Have Answered "+questions+" Question.\n"

						+quest+" of them are correct.\n"+ans+" of them are incorrect.\n" +"Your Success Rate is =% "+totalrate ,

						"Results",JOptionPane.INFORMATION_MESSAGE);

 

		}

public static void main(String[] args) 

				{

					

					

					new Questions();

					

					

					

				}

 

 

	 }



	 

		

	






now this is my code.... that i convert.... to Jframe... my code imports a picture.. so just comment the picture path to use it... now .... i can do the frame.. add question, next button..... heres my problem ... try to figure out my code... how can i trace now... the correct answer, or how can i say that the question is correct, ? can u figure out????



package Games;


import javax.swing.*;



import java.awt.*;

import java.awt.event.*;


public class QandASample extends JWindow implements ActionListener

{

JWindow window = new JWindow();

JFrame que1 = new JFrame();

JFrame que2 = new JFrame();

JFrame que3 = new JFrame();

JFrame que4 = new JFrame();

JFrame que5 = new JFrame();

JFrame res1 = new JFrame();


JLabel Q1= new JLabel("1.What is Java (in regard to Computer Science)?");

JLabel Q2= new JLabel("2.What's the difference between an Applet and an application ?");

JLabel Q3= new JLabel("3.What shortcut we use for importing objects in 'Eclipse' IDE?");

JLabel Q4= new JLabel("4.What shortcut we use to compile & run a source code in 'Eclipse' IDE?");

JLabel Q5= new JLabel("5.What is the current version for 'JDK' Kit?");

JLabel label96= new JLabel("");

JLabel label97= new JLabel("");

JLabel label98= new JLabel("");

JLabel label99= new JLabel("");

JLabel label934= new JLabel("");

JLabel TotalQuestion = new JLabel("");

JLabel labelCorrect = new JLabel("");

JLabel labelInCorrect = new JLabel("");

JLabel TotalCorrect = new JLabel("");


JRadioButton q1a =new JRadioButton("a. A type of Coffee");

JRadioButton q1b =new JRadioButton("b. Object Oriented Programming");

JRadioButton q1c =new JRadioButton("c. InterActive Website");

JRadioButton q1d =new JRadioButton("d. None Of the Above ");


JRadioButton q2a =new JRadioButton("a. An application is only available on Windows");

JRadioButton q2b =new JRadioButton("b. Applets can paint words, applications cannot");

JRadioButton q2c =new JRadioButton("c. Applets are run over the web");

JRadioButton q2d =new JRadioButton("d. None Of the Above");


JRadioButton q3a =new JRadioButton("a. CTRL + O");

JRadioButton q3b =new JRadioButton("b. ALT + O");

JRadioButton q3c =new JRadioButton("c. CTRL + SHIFT + O");

JRadioButton q3d =new JRadioButton("d. ALT + SHIFT + O");


JRadioButton q4a =new JRadioButton("a. ALT + SHIFT + X and J");

JRadioButton q4b =new JRadioButton("b. CTRL + SHIFT + J and X");

JRadioButton q4c =new JRadioButton("c. CTRL + ALT + J and Q");

JRadioButton q4d =new JRadioButton("d. ALT + CTRL + Q");


JRadioButton q5a =new JRadioButton("a. JDK 1.4 Update 1");

JRadioButton q5b =new JRadioButton("b. JDK 1.5 Update 10");

JRadioButton q5c =new JRadioButton("c. JDK 1.6 Update 9");

JRadioButton q5d =new JRadioButton("d. JDK 1.6 Update 3");


JButton q1button=new JButton("NEXT");

JButton q2button=new JButton("NEXT");

JButton q3button=new JButton("NEXT");

JButton q4button=new JButton("NEXT");

JButton q5button=new JButton("NEXT");

JButton button3=new JButton("Click to submit your score");

ButtonGroup g=new ButtonGroup();  


public QandASample()

{

	



que1.setSize(600,550);


que1.setUndecorated(true);

que1.setLayout(null);

que1.setAlwaysOnTop(false);

que1.setResizable(false);

que1.setTitle("Question1");

Q1.setBounds(200,200,500,30);

que1.add(Q1);

q1a.setBounds(50,300,500,30);

que1.add(q1a);

q1b.setBounds(50,340,500,30);

que1.add(q1b);

q1c.setBounds(50,380,500,30);

que1.add(q1c);

q1d.setBounds(50,420,500,30);

que1.add(q1d);

q1button.setBounds(80,460,90,30);

que1.add(q1button);

que1.setLocationRelativeTo(null);


ImageIcon imgBackG = new ImageIcon(getClass().getResource("arg.jpg"));

JLabel lblBackG = new JLabel(imgBackG);

lblBackG.setBounds(0, 0, 600, 600);

que1.add(lblBackG);

que1.setVisible(true);



q1a.addActionListener(this);

q1b.addActionListener(this);

q1c.addActionListener(this);

q1button.addActionListener(this);


g.add(q1a);

g.add(q1b);

g.add(q1c);

g.add(q1d);




que2.setUndecorated(true);

que2.setSize(600,550);

que2.setLayout(null);

que2.setResizable(false);

que2.setTitle("Question2");

Q2.setBounds(200,200,500,30);

que2.add(Q2);

q2a.setBounds(50,300,500,30);

que2.add(q2a);

q2b.setBounds(50,340,500,30);

que2.add(q2b);

q2c.setBounds(50,380,500,30);

que2.add(q2c);

q2d.setBounds(50,420,500,30);

que2.add(q2d);

q2button.setBounds(80,460,90,30);

que2.add(q2button);

que2.setLocationRelativeTo(null);



ImageIcon imageq2 = new ImageIcon(getClass().getResource("arg.jpg"));

JLabel lblBackq2 = new JLabel(imageq2);

lblBackq2.setBounds(0, 0, 600, 600);

que2.add(lblBackq2);

que2.setVisible(false);



q2a.addActionListener(this);

q2b.addActionListener(this);

q2c.addActionListener(this);

q2d.addActionListener(this);

q2button.addActionListener(this);


g.add(q2a);

g.add(q2b);

g.add(q2c);

g.add(q2d);


que3.setUndecorated(true);

que3.setSize(600,550);

que3.setLayout(null);

que3.setResizable(false);

que3.setTitle("Question 3");

Q3.setBounds(200,200,500,30);

que3.add(Q3);

q3a.setBounds(50,300,500,30);

que3.add(q3a);

q3b.setBounds(50,340,500,30);

que3.add(q3b);

q3c.setBounds(50,380,500,30);

que3.add(q3c);

q3d.setBounds(50,420,500,30);

que3.add(q3d);

q3button.setBounds(80,460,90,30);

que3.add(q3button);

que3.setLocationRelativeTo(null);



ImageIcon imageq3 = new ImageIcon(getClass().getResource("arg.jpg"));

JLabel lblBackq3 = new JLabel(imageq3);

lblBackq3.setBounds(0, 0, 600, 600);

que3.add(lblBackq3);

que3.setVisible(false);


q3a.addActionListener(this);

q3b.addActionListener(this);

q3c.addActionListener(this);

q3d.addActionListener(this);

q3button.addActionListener(this);


g.add(q3a);

g.add(q3b);

g.add(q3c);

g.add(q3d);


que4.setUndecorated(true);

que4.setSize(600,550);

que4.setLayout(null);

que4.setResizable(false);

que4.setTitle("Question4");

Q4.setBounds(200,200,500,30);

que4.add(Q4);

q4a.setBounds(50,300,500,30);

que4.add(q4a);

q4b.setBounds(50,340,500,30);

que4.add(q4b);

q4c.setBounds(50,380,500,30);

que4.add(q4c);

q4d.setBounds(50,420,500,30);

que4.add(q4d);

q4button.setBounds(80,460,90,30);

que4.add(q4button);

que4.setLocationRelativeTo(null);



ImageIcon imageq4 = new ImageIcon(getClass().getResource("arg.jpg"));

JLabel lblBackq4 = new JLabel(imageq4);

lblBackq4.setBounds(0, 0, 600, 600);

que4.add(lblBackq4);

que4.setVisible(false);


q4a.addActionListener(this);

q4b.addActionListener(this);

q4c.addActionListener(this);

q4d.addActionListener(this);

q4button.addActionListener(this);


g.add(q4a);

g.add(q4b);

g.add(q4c);

g.add(q4d);


que5.setUndecorated(true);

que5.setSize(600,550);

que5.setLayout(null);

que5.setResizable(false);

que5.setTitle("Question5");

Q5.setBounds(200,200,500,30);

que5.add(Q5);

q5a.setBounds(50,300,500,30);

que5.add(q5a);

q5b.setBounds(50,340,500,30);

que5.add(q5b);

q5c.setBounds(50,380,500,30);

que5.add(q5c);

q5d.setBounds(50,420,500,30);

que5.add(q5d);

q5button.setBounds(80,460,90,30);

que5.add(q5button);

que5.setLocationRelativeTo(null);



ImageIcon imageq5 = new ImageIcon(getClass().getResource("arg.jpg"));

JLabel lblBackq5 = new JLabel(imageq5);

lblBackq5.setBounds(0, 0, 600, 600);

que5.add(lblBackq5);

que5.setVisible(false);



q5a.addActionListener(this);

q5b.addActionListener(this);

q5c.addActionListener(this);

q5d.addActionListener(this);

q5button.addActionListener(this);


g.add(q5a);

g.add(q5b);

g.add(q5c);

g.add(q5d);



res1.setSize(600,550);

res1.setLayout(null);

res1.setResizable(false);

res1.setTitle("Result");

label99.setBounds(20,50,500,30);

res1.add(label99);

label98.setBounds(20,100,500,30);

res1.add(label98);

label97.setBounds(20,150,500,30);

res1.add(label97);

TotalQuestion.setBounds(20,200,500,30);

res1.add(TotalQuestion);

labelCorrect.setBounds(20,250,500,30);

res1.add(labelCorrect);

labelInCorrect.setBounds(20,300,500,30);

res1.add(labelInCorrect);

TotalCorrect.setBounds(20,350,500,30);

res1.add(TotalCorrect);

label96.setBounds(20,400,500,30);

res1.add(label96);

button3.setBounds(30,450,400,30);

res1.add(button3);

res1.setLocationRelativeTo(null);


ImageIcon imageresult = new ImageIcon(getClass().getResource("awt.jpg"));

JLabel lblresult = new JLabel(imageresult);

lblresult.setBounds(0, 0, 600, 600);

res1.add(lblresult);


que1.setVisible(false);


button3.setActionCommand("button3");

button3.addActionListener(this);

que1.setVisible(true);

que5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}




public void actionPerformed(ActionEvent e)

{

Object source = e.getSource();


int que1a=0,que1b=0,que1c=0,que1d = 0;

int que2a=0,que2b=0,que2c=0,que2d = 0;

int que3a=0,que3b=0,que3c=0,que3d = 0;

int que4a=0,que4b=0,que4c=0,que4d = 0;

int que5a=0,que5b=0,que5c=0,que5d = 0;



if (q1a.isSelected())

    

    {

    que1a++;

    }

else if(q1b.isSelected())

    {

    que1b++;

    }


else if(q1c.isSelected())

    {


    que1c++;

    

    }

else if(q1d.isSelected())

{


    que1d++;


}


if(q2a.isSelected())

    {

    que2a++;

    }

else if(q2b.isSelected())

    {


    que2b++;

    }


else if(q2c.isSelected())

    {


    que2c++;

    

    }

else if(q2d.isSelected())

{


    que2d++;


}


if(q3a.isSelected())

{

    que3a++;

}

else if(q3b.isSelected())

{


    que3b++;

}


else if(q3c.isSelected())

{


    que3c++;


}

else if(q3d.isSelected())

{


    que3d++;


}


if(q4a.isSelected())

{

    que4a++;

}

else if(q4b.isSelected())

{


    que4b++;

}


else if(q4c.isSelected())

{


    que4c++;


}

else if(q4d.isSelected())

{


    que4d++;


}


if(q5a.isSelected())

{

    que5a++;

}

else if(q5b.isSelected())

{


    que5b++;

}


else if(q5c.isSelected())

{


    que5c++;


}

else if(q4d.isSelected())

{


    que5d++;


}



int totala=que1a + que2a +que3a;

int totalb=que1b + que2b +que3b;

int totalc=que1c + que2c +que3c;

int totaltotal=(totala*3)+(totalb*2)+totalc;


if(source==q1button)

    {

        que1.setVisible(false);

        que2.setVisible(true);

        que3.setVisible(false);

        que4.setVisible(false);

        que5.setVisible(false);

    }


if(source==q2button)

    {

		que1.setVisible(false);

        que2.setVisible(false);

        que3.setVisible(true);

        que4.setVisible(false);

        que5.setVisible(false);

    }

if(source==q3button)

	{

		que1.setVisible(false);

		que2.setVisible(false);

    	que3.setVisible(false);

    	que4.setVisible(true);

    	que5.setVisible(false);

    	res1.setVisible(false);

	}

if(source==q4button)

{

	que1.setVisible(false);

	que2.setVisible(false);

	que3.setVisible(false);

    que4.setVisible(false);

    que5.setVisible(true);

    res1.setVisible(false);

}


if(source==q5button)

{

	que1.setVisible(false);

	que2.setVisible(false);

	que3.setVisible(false);

	que4.setVisible(false);

    que5.setVisible(false);

    res1.setVisible(true);

}

	label99.setText("Total number of As selected: " + totala);

	label98.setText("Total number of Bs selected: " + totalb);

	label97.setText("Total number of Cs selected: " + totalc);

	TotalQuestion.setText("Total Question :  5");

	labelCorrect.setText("Total number of Correct");

	labelInCorrect.setText("Total number of InCorrect");

	TotalCorrect.setText("Total Percent : ");


	totaltotal=(totala*3)+(totalb*2)+totalc;


	if(source==button3)

    {

        label96.setText("Your total Score:" + totaltotal);

        label934.setText(""+ totaltotal);

        

    }


        

}

public static void main(String[] args)

{    

    new QandASample();

}

}





and lastly... how to compute my last result.., where in it indicates total number of A, B, C, D, and also Total Question, Total correction, Total Incorrect, the percentage and also the total score?.... can u edit my code?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users