Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2011
    Rep Power

    Unhappy Need Help With JScrollPane Please

    Hello everyone! :)

    I'm working on an assignment in my computer science class and I'm stuck with how to get a JScrollPane to work on my Quiz page. I'm using JCreator and I'd appreciate the help I can get. This my code:

    Java Code:
    import java.awt.*;
     import java.awt.event.*;
     import javax.swing.*;
    public class Quiz implements ActionListener
    	// ------------------
     	JFrame jf;		// delcare JFrame
     	JPanel jp;		// declare JPanel
    	Container cp;		// declare content pane (Container)
    	JScrollPane sp;		// declare JScrollPane
    	JRadioButton jrb1, jrb2, jrb3, jrb4, jrb5, jrb6, jrb7, jrb8, jrb9, jrb10, jrb11, jrb12,
    	jrb13, jrb14, jrb15, jrb16, jrb17, jrb18, jrb19, jrb20, jrb21, jrb22, jrb23, jrb24, jrb25,
    	jrb26, jrb27, jrb28, jrb29, jrb30, jrb31, jrb32, jrb33, jrb34, jrb35, jrb36;		// declare radio buttons
    	ButtonGroup bg1, bg2, bg3, bg4, bg5, bg6, bg7, bg8, bg9, bg10, bg11, bg12;		// declare radiobutton group
    	JButton jb;
    	JLabel correct, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12;
    	String message1 = "Nothing Selecetd!";
    	String message2 = "Correct!";
    	int correctCount;
        // MAIN METHOD
    	// -----------
        public static void main(String[] args)
    		new Quiz ();
        // CONSTRUCTOR
    	// -----------
        public Quiz ()
        	correctCount =0;
        	// instantiate and setup JFrame and content
    		// ----------------------------------------
    		jf = new JFrame ("Quiz");
    		jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    		jf.setBounds (50,50,708,480);
    		jf.setResizable (true);
    		jf.setUndecorated (false);
    		// get the content pane from the window and set it up
    		// --------------------------------------------------
    		cp = jf.getContentPane();
    		cp.setBackground( );
    		// create, instantiate, and set up JScrollPane
    		// ----------------------------------
    		sp = new JScrollPane(q5);
    		sp.setBounds (0,50, 708, 430);
    		// add JScrollPane to content pane
    		// -------------------------------
    		//cp.add (sp);
    		// create the labels
    		// -----------------
    		q1 = new JLabel("", JLabel.CENTER);
            // set the bounds of the labels and add them to the content pane
            // -------------------------------------------------------------
            q1.setBounds (0,0,300,40);
    		// create the radio buttons and the button group object to which they will belong
    		// ------------------------------------------------------------------------------
    		bg1 = new ButtonGroup();
    		// buttons for question 1
    		// ----------------------
    		jrb1 = new JRadioButton ("James Watson & Francis Crick");
    		jrb2 = new JRadioButton ("Friedrich Miescher");
    		jrb3 = new JRadioButton ("Alfred Hershey & Martha Chase");
    		jrb1.addActionListener (this);
    		jrb2.addActionListener (this);
    		jrb3.addActionListener (this);
    		// add the radio buttons to the button group (no visual adding)
    		// -----------------------------------------------------------
    		// set the bounds of the radio buttons and add them to the content pane
    		jrb1.setBounds (50,30,600,50);
    		jrb2.setBounds (50,65,600,50);
    		jrb3.setBounds (50,100,600,50);
    		// create and add a JButton for submitting answers
    		// -----------------------------------------------
    		jb = new JButton ("Submit");
    		jb.setBounds (300,1700,120,40);
    		jb.addActionListener (this);
    		jf.setVisible (true);
        } // end constructor
        public void actionPerformed (ActionEvent e)
    		// if a radio button is selected, do the following
    		if (e.getSource() == jrb1)
    			message1= "You got this many questions correct: " + correctCount;
    		else if (e.getSource() == jrb2)
    			message1= "Incorrect";
    		else if (e.getSource() == jrb3)
    			message1= "Incorrect";
    		else if (e.getSource() == jb)
    			JOptionPane.showMessageDialog (null, message1, "Molecular Genetics Quiz", JOptionPane.ERROR_MESSAGE);
    		} // end of if structure
    	} // end actionPerformed method
    } // end of Quiz
    How do I add JLabels and Radiobuttons to scrollpane?

    Moderator Edit: quote tags changed to code tags
    Last edited by Fubarable; 01-30-2011 at 10:02 PM. Reason: Moderator Edit: quote tags changed to code tags

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    Please edit your post and use "code" tags instead of "quote" tags for readability reasons.

    kind regards,

    I have the stamina of a seal; I lie on the beach instead of running on it.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    Quote Originally Posted by MemorableSmile View Post
    How do I add JLabels and Radiobuttons to scrollpane?
    You don't. You add JLabels and JRadioButtons to a JPanel, and then add the JPanel to a JScrollPane. You'll also want to avoid using absolute layout, especially if you're adding things that will be held in a JScrollPane, but instead use some of the user-friendly layout managers.

    Oh, and I changed your quote tags to code tags, and I hope you'll notice how it maintains your code formatting allowing us to better read your code.

    Good luck, and please ask if anything is still unclear!

  4. #4
    Join Date
    Jan 2011
    Rep Power


    Thanks so much!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

Similar Threads

  1. Help with jScrollPane
    By weikang in forum SWT / JFace
    Replies: 1
    Last Post: 02-04-2011, 04:30 AM
  2. JScrollPane
    By hiddenpremise in forum AWT / Swing
    Replies: 2
    Last Post: 12-25-2010, 06:15 AM
  3. JScrollPane
    By UJJAL DHAR in forum New To Java
    Replies: 12
    Last Post: 08-17-2010, 06:47 PM
  4. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 09:39 AM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 07:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts