0

So I made a quiz that have 14 question each one have 3 radiobuttons as options, the quiz is in 4 forms, each form has 4 questions except the last one it has 2 questions..Now I need to sum all the selected answers..How can I do that??

public class page1 extends JFrame {
	
	
	int[] results = new int[14];
	public page1() {
		initComponents();
	}

	private void button2ActionPerformed(ActionEvent e) {
		
		
		if(!radioButton4.isSelected()&& !radioButton5.isSelected()&& !radioButton6.isSelected()){

 Alertq1 frame = new Alertq1();
 frame.setVisible(true);
}else if(!radioButton7.isSelected()&& !radioButton8.isSelected()&& !radioButton9.isSelected()){
Alertq2 frame = new Alertq2();
 frame.setVisible(true);
} else if(!radioButton10.isSelected()&& !radioButton11.isSelected()&& !radioButton12.isSelected()){
Alertq3 frame = new Alertq3();
frame.setVisible(true);
} else if(!radioButton13.isSelected()&& !radioButton14.isSelected()&& !radioButton15.isSelected()){
Alertq4 frame = new Alertq4();
frame.setVisible(true);
}
else{

	page2 frame = new page2(results);
           		frame.setVisible(true);
           		this.setVisible(false);
}

	}


	private void initComponents() {
	
		label2 = new JLabel();
		label3 = new JLabel();
		label4 = new JLabel();
		label5 = new JLabel();
		radioButton4 = new JRadioButton();
		radioButton5 = new JRadioButton();
		radioButton6 = new JRadioButton();
		radioButton7 = new JRadioButton();
		radioButton8 = new JRadioButton();
		radioButton9 = new JRadioButton();
		radioButton10 = new JRadioButton();
		radioButton11 = new JRadioButton();
		radioButton12 = new JRadioButton();
		radioButton13 = new JRadioButton();
		radioButton14 = new JRadioButton();
		radioButton15 = new JRadioButton();
		button2 = new JButton();
		label1 = new JLabel();

		//======== this ========
		setIconImage(new ImageIcon("D:\\Documents and Settings\\test1.jpg").getImage());
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- label2 ----
		label2.setText("1. Do you take the risk when doing things ?");
		label2.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
		label2.setForeground(Color.red);
		label2.setBackground(UIManager.getColor("Button.disabledShadow"));
		contentPane.add(label2);
		label2.setBounds(new Rectangle(new Point(140, 40), label2.getPreferredSize()));

		//---- label3 ----
		label3.setText("2. Can you be a leader?");
		label3.setBackground(Color.white);
		label3.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
		label3.setForeground(new Color(255, 153, 0));
		contentPane.add(label3);
		label3.setBounds(new Rectangle(new Point(140, 135), label3.getPreferredSize()));

		//---- label4 ----
		label4.setText("3. Are you self-confident?");
		label4.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
		label4.setForeground(new Color(102, 204, 0));
		contentPane.add(label4);
		label4.setBounds(new Rectangle(new Point(140, 230), label4.getPreferredSize()));

		//---- label5 ----
		label5.setText("4.\t Do you see yourself creative?");
		label5.setFont(new Font("Comic Sans MS", Font.PLAIN, 18));
		label5.setForeground(new Color(204, 102, 255));
		contentPane.add(label5);
		label5.setBounds(140, 325, 300, label5.getPreferredSize().height);

		//---- radioButton4 ----
		radioButton4.setText("Yes I like to challenge myself to the limit.");
		radioButton4.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		radioButton4.setBackground(Color.white);
		radioButton4.addActionListener(new ActionListener() {
			@Override
		public void actionPerformed(ActionEvent e){
					System.out.println("Selected value = " +  e.getActionCommand());
					results[0] = 5;
					}
			});

		contentPane.add(radioButton4);
		radioButton4.setBounds(new Rectangle(new Point(155, 60), radioButton4.getPreferredSize()));

		//---- radioButton5 ----
		radioButton5.setText("I will see how dangerous is that risk");
		radioButton5.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		radioButton5.setBackground(Color.white);
		radioButton5.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("Selected value = " +  e.getActionCommand());
				results[1] = 3;
			}
		});
		contentPane.add(radioButton5);
		radioButton5.setBounds(new Rectangle(new Point(155, 85), radioButton5.getPreferredSize()));

		//---- radioButton6 ----
		radioButton6.setText("No I don\u2019t like taking risks");
		radioButton6.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		radioButton6.setBackground(Color.white);
		radioButton6.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			System.out.println("Selected value = " +  e.getActionCommand());
			results[2] = 1;
			}
		});
		contentPane.add(radioButton6);
		radioButton6.setBounds(new Rectangle(new Point(155, 110), radioButton6.getPreferredSize()));

		//---- radioButton7 ----
		radioButton7.setText("Yes");
		radioButton7.setBackground(Color.white);
		radioButton7.addActionListener(new ActionListener() {
			@Override
		public void actionPerformed(ActionEvent e){
					System.out.println("Selected value = " +  e.getActionCommand());
					results[3] = 5;
					}
			});
		radioButton7.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		contentPane.add(radioButton7);
		radioButton7.setBounds(new Rectangle(new Point(155, 155), radioButton7.getPreferredSize()));

		//---- radioButton8 ----
		radioButton8.setText("If I have too");
		radioButton8.setBackground(Color.white);
		radioButton8.addActionListener(new ActionListener() {
			@Override
		public void actionPerformed(ActionEvent e){
					System.out.println("Selected value = " +  e.getActionCommand());
					results[4] = 3;
					}
			});
		radioButton8.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		contentPane.add(radioButton8);
		radioButton8.setBounds(new Rectangle(new Point(155, 180), radioButton8.getPreferredSize()));

		//---- radioButton9 ----
		radioButton9.setText("No");
		radioButton9.setBackground(Color.white);
		radioButton9.addActionListener(new ActionListener() {
			@Override
		public void actionPerformed(ActionEvent e){
					System.out.println("Selected value = " +  e.getActionCommand());
					results[5] = 1;
					}
			});
		radioButton9.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		contentPane.add(radioButton9);
		radioButton9.setBounds(new Rectangle(new Point(155, 205), radioButton9.getPreferredSize()));

		//---- radioButton10 ----
		radioButton10.setText("Yes I am ");
		radioButton10.setBackground(Color.white);
		radioButton10.addActionListener(new ActionListener() {
			@Override
		public void actionPerformed(ActionEvent e){
					System.out.println("Selected value = " +  e.getActionCommand());
					results[6] = 5;
					}
			});
		radioButton10.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		contentPane.add(radioButton10);
		radioButton10.setBounds(new Rectangle(new Point(155, 250), radioButton10.getPreferredSize()));

		//---- radioButton11 ----
		radioButton11.setText("Sometime");
		radioButton11.setBackground(Color.white);
		radioButton4.addActionListener(new ActionListener() {
			@Override
		public void actionPerformed(ActionEvent e){
					System.out.println("Selected value = " +  e.getActionCommand());
					results[7] = 3;
					}
			});
		radioButton11.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		contentPane.add(radioButton11);
		radioButton11.setBounds(new Rectangle(new Point(155, 275), radioButton11.getPreferredSize()));

		//---- radioButton12 ----
		radioButton12.setText("No ");
		radioButton12.setBackground(Color.white);
		radioButton12.addActionListener(new ActionListener() {
			@Override
		public void actionPerformed(ActionEvent e){
					System.out.println("Selected value = " +  e.getActionCommand());
					results[8] = 1;
					}
			});
		radioButton12.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		contentPane.add(radioButton12);
		radioButton12.setBounds(new Rectangle(new Point(155, 300), radioButton12.getPreferredSize()));

		//---- radioButton13 ----
		radioButton13.setText("Yes I have done many creative things");
		radioButton13.setBackground(Color.white);
		radioButton13.addActionListener(new ActionListener() {
			@Override
		public void actionPerformed(ActionEvent e){
					System.out.println("Selected value = " +  e.getActionCommand());
					results[9] = 5;
					}
			});
		radioButton13.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		contentPane.add(radioButton13);
		radioButton13.setBounds(new Rectangle(new Point(155, 345), radioButton13.getPreferredSize()));

		//---- radioButton14 ----
		radioButton14.setText("Maybe if I have to");
		radioButton14.setBackground(Color.white);
			radioButton4.addActionListener(new ActionListener() {
			@Override
		public void actionPerformed(ActionEvent e){
					System.out.println("Selected value = " +  e.getActionCommand());
					results[10] = 3;
					}
			});
		radioButton14.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		contentPane.add(radioButton14);
		radioButton14.setBounds(new Rectangle(new Point(155, 370), radioButton14.getPreferredSize()));

		//---- radioButton15 ----
		radioButton15.setText("No");
		radioButton15.setBackground(Color.white);
		radioButton15.addActionListener(new ActionListener() {
			@Override
		public void actionPerformed(ActionEvent e){
					System.out.println("Selected value = " +  e.getActionCommand());
					results[11] = 1;
					}
			});
		radioButton15.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		contentPane.add(radioButton15);
		radioButton15.setBounds(new Rectangle(new Point(155, 395), radioButton15.getPreferredSize()));

		//---- button2 ----
		button2.setText("NEXT");
		button2.setForeground(Color.orange);
		button2.setFont(button2.getFont().deriveFont(button2.getFont().getStyle() | Font.BOLD));
		contentPane.add(button2);
		button2.setBounds(new Rectangle(new Point(370, 580), button2.getPreferredSize()));
		button2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				button2ActionPerformed(e);
			}
		});

		//---- label1 ----
		label1.setIcon(new ImageIcon("D:\\Documents and Settings\\U2701592\\Desktop\\semester 7\\capston\\test-ema\\test-ema\\test2.jpg"));
		contentPane.add(label1);
		label1.setBounds(new Rectangle(new Point(0, -5), label1.getPreferredSize()));

		{ // compute preferred size
			Dimension preferredSize = new Dimension();
			for(int i = 0; i < contentPane.getComponentCount(); i++) {
				Rectangle bounds = contentPane.getComponent(i).getBounds();
				preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
				preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
			}
			Insets insets = contentPane.getInsets();
			preferredSize.width += insets.right;
			preferredSize.height += insets.bottom;
			contentPane.setMinimumSize(preferredSize);
			contentPane.setPreferredSize(preferredSize);
		}
		pack();
		setLocationRelativeTo(getOwner());

		//---- buttonGroup1 ----
		ButtonGroup buttonGroup1 = new ButtonGroup();
		buttonGroup1.add(radioButton4);
		buttonGroup1.add(radioButton5);
		buttonGroup1.add(radioButton6);

		//---- buttonGroup2 ----
		ButtonGroup buttonGroup2 = new ButtonGroup();
		buttonGroup2.add(radioButton7);
		buttonGroup2.add(radioButton8);
		buttonGroup2.add(radioButton9);

		//---- buttonGroup3 ----
		ButtonGroup buttonGroup3 = new ButtonGroup();
		buttonGroup3.add(radioButton10);
		buttonGroup3.add(radioButton11);
		buttonGroup3.add(radioButton12);

		//---- buttonGroup4 ----
		ButtonGroup buttonGroup4 = new ButtonGroup();
		buttonGroup4.add(radioButton13);
		buttonGroup4.add(radioButton14);
		buttonGroup4.add(radioButton15);
		
	}

	private JLabel label2;
	private JLabel label3;
	private JLabel label4;
	private JLabel label5;
	private JRadioButton radioButton4;
	private JRadioButton radioButton5;
	private JRadioButton radioButton6;
	private JRadioButton radioButton7;
	private JRadioButton radioButton8;
	private JRadioButton radioButton9;
	private JRadioButton radioButton10;
	private JRadioButton radioButton11;
	private JRadioButton radioButton12;
	private JRadioButton radioButton13;
	private JRadioButton radioButton14;
	private JRadioButton radioButton15;
	private JButton button2;
	private JLabel label1;
	
	
	public static void main (String[] args) {
		
		page1 frame = new page1();
	frame.setVisible(true);
	
	
		
	}
}

Edited by mansoor_1: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by stultuske
0

set value for each correct answer to 1, for each false answer to 0 and set at each time you enter a sollution:

int total += answerValue;

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.