Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By sunde887
  • 1 Post By pbrockway2
  • 1 Post By Junky

Thread: Swing java quiz

  1. #1
    htuy is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default Swing java quiz

    I am trying to make a simple quiz in java with swing. I want the user to open the application and see a screen with four button and a label, the label asking a question and the buttons each containing a possible answer. I fthe user clicks the right button, they will be advanced to a page telling them that they are right, and if they get it wrong they will be brought to a page telling them they are wrong. From the right or wrong pages they will be able to click a button to advance to the next question.

    In short, I am looking to create a JFrame and be able to replace its content pane with different JPanels over and over again based on user input.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

  3. #3
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,571
    Rep Power
    12

    Default

    In short, I am looking to create a JFrame and be able to replace its content pane with different JPanels over and over again based on user input.
    I agree with using a CardLayout to move between the component that displays a question and the one that gives feedback. But you don't necessarily need a new component for each question if they are being asked one at a time. You can just set the data (the text etc) on the component and reuse it.

    Make sure that the classes that represent the basic elements of your program - the quiz and its questions - are written and working before you move on to the gui.
    htuy likes this.

  4. #4
    htuy is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Thank you for the help. I have ine more question though. Where should I put the action performed event listeners if I am doing this in multiple classes?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You will need to attach Listeners to your Buttons, therefore in whatever class your Buttons are.
    htuy likes this.

  6. #6
    htuy is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Ok. I guess maybe I dont understand how listeners work. But if I put them in the class with the buttons, which is seperate from the class with my programs main methods, won't they not run? Because my understanding was that they were seperate methods. Or do I have to call the somehow from within the main class?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    When something happens to a Component an Event is fired. All Listeners attached to that Component are called. You have a Button with a Listener. When the Button is clicked then the actionPerformed method of that Button is called. It doesn't matter where you write the code just as long as it is there and attached to a Button that is visible on your GUI*. If you have the Listener code in one class but want something to happen in another class then your actionPerformed method will need to call a method in the other class. Alternatively you could have a controller class (see MVC pattern).

    * Just wanted to emphasise this as a common mistake is to create 2 Buttons. One is visible on the GUI but doesn't have a Listener attached. The other has a Listener but is not placed on the GUI. Then the person wonders why nothing happens when they click the button

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    When you click the button it does whatever is in the listener attached to it. The event is fired to the buttons listener, which do something with event. The buttons will somehow need to access the other class. Here is a quick example that I hope will help clarify some concepts.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class MyFrame extends JFrame{
    	private MyPanel p;
    	private JButton b1;
    	private JLabel l1;
    
    	public MyFrame(){
    		p = new MyPanel();
    		b1 = new JButton("Click me!");
    		l1 = new JLabel("Label");
        
    		b1.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				l1.setText(p.getInput());
    			}
    		});
    		
    		setLayout(new BorderLayout());
    		
    		add(BorderLayout.NORTH, l1);
    		add(BorderLayout.CENTER, p);
    		add(BorderLayout.SOUTH, b1);
    		
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		pack();
    		setVisible(true);
    	}
    	public static void main(String[] args){
    		MyFrame f = new MyFrame();
    	}
    }
    
    class MyPanel extends JPanel{
    	private JTextField input;
    	private JLabel inputLabel;
    	
    	public MyPanel(){
    		setLayout(new FlowLayout());
    		inputLabel = new JLabel("Input:");
    		input = new JTextField(10);
    		add(inputLabel);
    		add(input);
    	}
    	
    	public String getInput(){
    		return input.getText();
    	}
    }

Similar Threads

  1. Quiz Time
    By rjuyal in forum Advanced Java
    Replies: 1092
    Last Post: 06-23-2012, 09:55 AM
  2. Help with "Head First Java" quiz
    By TheWhopper in forum New To Java
    Replies: 6
    Last Post: 06-04-2011, 07:09 AM
  3. Help with quiz program
    By askinne2 in forum New To Java
    Replies: 1
    Last Post: 04-16-2010, 11:49 PM
  4. Help with quiz
    By khanzaman in forum AWT / Swing
    Replies: 10
    Last Post: 01-19-2009, 08:45 AM
  5. Help pls with a quiz
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 07:09 AM

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
  •