Results 1 to 10 of 10
  1. #1
    Gorchev is offline Member
    Join Date
    Jun 2015
    Posts
    2
    Rep Power
    0

    Default Help needed with making a Frame with text field, radio buttons, buttons!

    Hello, I'm new in java programing and I need help with my project.

    Project:
    1. I have to make a Frame with text field, radio buttons and buttons.
    2. In the text field there have to be a question and from the radio buttons you have to choose your're answer.
    3. There should be two buttons at the bottom of the Frame - one "Next" and one "Close". When you choose you're answer click "Next" and then it opens a new Frame with the next question. I need to have 10 questions. From the "Close" button the frame should be closed.

    Can someone help me with this task? I made this so far:

    1. This is the Frame with the question, but I need to have 10 questions:
    Java Code:
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.AbstractButton;
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JRadioButton;
    
    public class demo2 {
    
      public static void main(String[] args) {
        // Some choices
        JRadioButton choice1, choice2, choice3, choice4;
        choice1 = new JRadioButton("Dividend");
        choice1.setActionCommand("Dividend");
        choice2 = new JRadioButton("Qoutient");
        choice2.setActionCommand("Qoutient");
        choice3 = new JRadioButton("Divisor");
        choice3.setActionCommand("Divisor");
        choice4 = new JRadioButton("Remainder");
        choice4.setActionCommand("Remainder");
    
        // A group, to ensure that we only vote for one.
        final ButtonGroup group = new ButtonGroup();
        group.add(choice1);
        group.add(choice2);
        group.add(choice3);
        group.add(choice4);
    
        // A simple ActionListener, showing each selection using the ButtonModel
        class VoteActionListener implements ActionListener {
          public void actionPerformed(ActionEvent ev) {
            String choice = group.getSelection().getActionCommand();
            System.out.println("ACTION Choice Selected: " + choice);
          }
        }
    
        // A simple ItemListener, showing each selection and deselection
        class VoteItemListener implements ItemListener {
          public void itemStateChanged(ItemEvent ev) {
            boolean selected = (ev.getStateChange() == ItemEvent.SELECTED);
            AbstractButton button = (AbstractButton) ev.getItemSelectable();
            System.out.println("ITEM Choice Selected: " + selected
                + ", Selection: " + button.getActionCommand());
          }
        }
    
        // Add listeners to each button
        ActionListener alisten = new VoteActionListener();
        choice1.addActionListener(alisten);
        choice2.addActionListener(alisten);
        choice3.addActionListener(alisten);
        choice4.addActionListener(alisten);
    
        ItemListener ilisten = new VoteItemListener();
        choice1.addItemListener(ilisten);
        choice2.addItemListener(ilisten);
        choice3.addItemListener(ilisten);
        choice4.addItemListener(ilisten);
    
        // Throw everything together
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container c = frame.getContentPane();
        c.setLayout(new GridLayout(0, 1));
        c.add(new JLabel("The modulus operator % is used in division to return the"));
        c.add(choice1);
        c.add(choice2);
        c.add(choice3);
        c.add(choice4);
        frame.pack();
        frame.setVisible(true);
      }
    }
    2. This is the Frame with the button "Next" which on clicking opens the new Frame but the old one stays open too. And i don't have a "Close" button.
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class demo  {
    static JFrame jf;
    public static void main(String a[])
    {
        JButton b=new JButton("NEXT");
        JPanel jp=new JPanel();
        jf=new JFrame();
        jf.setVisible(true);
        jf.setSize(400,400);
        jf.add(jp);
        jp.add(b);
    
        b.addActionListener( new ActionListener(){
        	public void actionPerformed(ActionEvent e)
        {
         JButton b=new JButton("NEXT");
         JPanel jp=new JPanel();
         JFrame f= new JFrame();
         f.setSize(400,400);
         jf=new JFrame(); 
         jf.setVisible(true);
         jf.add(jp);
         jp.add(b);
         
         b.addActionListener( new ActionListener(){
        	    public void actionPerformed(ActionEvent e)
        	    {
        	     JButton b=new JButton("NEXT");
        	     JPanel jp=new JPanel();
        	     JFrame f= new JFrame();
        	     f.setSize(400,400);
        	     jf=new JFrame(); 
        	     jf.setVisible(true);
        	     jf.add(jp);
        	     jp.add(b);
        	     
        	     b.addActionListener( new ActionListener(){
        	    	    public void actionPerformed(ActionEvent e)
        	    	    {
        	    	     JButton b=new JButton("NEXT");
        	    	     JPanel jp=new JPanel();
        	    	     JFrame f= new JFrame();
        	    	     f.setSize(400,400);
        	    	     jf=new JFrame(); 
        	    	     jf.setVisible(true);
        	    	     jf.add(jp);
        	    	     jp.add(b);
         
        	    	     b.addActionListener( new ActionListener(){
        	    	    	    public void actionPerformed(ActionEvent e)
        	    	    	    {
        	    	    	     JButton b=new JButton("NEXT");
        	    	    	     JPanel jp=new JPanel();
        	    	    	     JFrame f= new JFrame();
        	    	    	     f.setSize(400,400);
        	    	    	     jf=new JFrame(); 
        	    	    	     jf.setVisible(true);
        	    	    	     jf.add(jp);
        	    	    	     jp.add(b);
        }
        });
        
        }
        });
    }
    });
    }
    });
    }
    }
    Last edited by JosAH; 06-27-2015 at 04:50 PM. Reason: added [code] ... [/code] tags

  2. #2
    mlkike is offline Member
    Join Date
    Jun 2015
    Posts
    4
    Rep Power
    0

    Default Re: Help needed with making a Frame with text field, radio buttons, buttons!

    Why don't you create just one JFrame with:

    1x JTextField
    your JRadioButtons
    2x JButtons

    After clicking Next the Text in the JTextField changes so you don't have to create a new JFrame for each question.

    To your question:
    To close the "old" JFrame I would use dispose() or setVisibile().

    Close Button:

    System.exit(0);

  3. #3
    Gorchev is offline Member
    Join Date
    Jun 2015
    Posts
    2
    Rep Power
    0

    Default Re: Help needed with making a Frame with text field, radio buttons, buttons!

    Fine but can you write me the code because I don't know how to do it?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Help needed with making a Frame with text field, radio buttons, buttons!

    Quote Originally Posted by Gorchev View Post
    Fine but can you write me the code because I don't know how to do it?
    Quite a few people know how to write code here, but we're not a code factory; the rules of the game are: you attempt to write some code and when you're stuck, you show us what you have done, ask a relevant question and we'll try to help you.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    mlkike is offline Member
    Join Date
    Jun 2015
    Posts
    4
    Rep Power
    0

    Default Re: Help needed with making a Frame with text field, radio buttons, buttons!

    I copied your first code and added some things that should help you :)

    I did NOT finish the project but this could help you solving your problem if you understand the written code.
    I added comments above my pieces of code.

    kind regards,
    Mike

    Java Code:
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.AbstractButton;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JRadioButton;
    
    public class QuestionGame {
    
    	public static void main(String[] args) {
    
    		// Add boolean for answer
    		boolean answer = false;
    
    		// Some choices
    		JRadioButton choice1, choice2, choice3, choice4;
    		choice1 = new JRadioButton("Dividend");
    		choice1.setActionCommand("Dividend");
    		choice2 = new JRadioButton("Qoutient");
    		choice2.setActionCommand("Qoutient");
    		choice3 = new JRadioButton("Divisor");
    		choice3.setActionCommand("Divisor");
    		choice4 = new JRadioButton("Remainder");
    		choice4.setActionCommand("Remainder");
    
    		// A group, to ensure that we only vote for one.
    		final ButtonGroup group = new ButtonGroup();
    		group.add(choice1);
    		group.add(choice2);
    		group.add(choice3);
    		group.add(choice4);
    
    		// A simple ActionListener, showing each selection using the ButtonModel
    		class VoteActionListener implements ActionListener {
    			public void actionPerformed(ActionEvent ev) {
    				String choice = group.getSelection().getActionCommand();
    				System.out.println("ACTION Choice Selected: " + choice);
    			}
    		}
    
    		// A simple ItemListener, showing each selection and deselection
    		class VoteItemListener implements ItemListener {
    			public void itemStateChanged(ItemEvent ev) {
    				boolean selected = (ev.getStateChange() == ItemEvent.SELECTED);
    				AbstractButton button = (AbstractButton) ev.getItemSelectable();
    				System.out.println("ITEM Choice Selected: " + selected
    						+ ", Selection: " + button.getActionCommand());
    			}
    		}
    
    		// Add listeners to each button
    		ActionListener alisten = new VoteActionListener();
    		choice1.addActionListener(alisten);
    		choice2.addActionListener(alisten);
    		choice3.addActionListener(alisten);
    		choice4.addActionListener(alisten);
    
    		ItemListener ilisten = new VoteItemListener();
    		choice1.addItemListener(ilisten);
    		choice2.addItemListener(ilisten);
    		choice3.addItemListener(ilisten);
    		choice4.addItemListener(ilisten);
    
    		// Throw everything together
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container c = frame.getContentPane();
    		c.setLayout(new GridLayout(0, 1));
    
    		// Add JLabel for questions
    		JLabel questions = new JLabel("Question 1");
    
    		c.add(questions);
    		c.add(choice1);
    		c.add(choice2);
    		c.add(choice3);
    		c.add(choice4);
    
    		// Add JButtons
    		JButton next = new JButton("Next");
    		c.add(next);
    		JButton close = new JButton("Close");
    		c.add(close);
    
    		// ActionListener for JButton: Next
    		next.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO If choice is correct then change JLabel text ask new question
    
    				if (answer == true) {
    					questions.setText("Question 2");
    
    				} else {
    					JOptionPane.showMessageDialog(frame,
    							"Think about your answer!");
    				}
    
    			}
    		});
    
    		// ActionListener for JButton: Close
    		close.addActionListener(new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    
    				System.exit(0);
    			}
    		});
    
    		frame.pack();
    		frame.setVisible(true);
    	}
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Help needed with making a Frame with text field, radio buttons, buttons!

    @mlkike, read my reply #4 again.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    mlkike is offline Member
    Join Date
    Jun 2015
    Posts
    4
    Rep Power
    0

    Default Re: Help needed with making a Frame with text field, radio buttons, buttons!

    Quote Originally Posted by JosAH View Post
    @mlkike, read my reply #4 again.

    kind regards,

    Jos
    Is it not allowed to write code?
    If so I'm sorry then I missed it

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help needed with making a Frame with text field, radio buttons, buttons!

    Quote Originally Posted by mlkike View Post
    Is it not allowed to write code?
    If so I'm sorry then I missed it
    I hope the intent is to help. The code you posted does not explain the how, when and why you made the changes - you're not explaining anything at all. But the OP can just take your code, turn it in, get credit for it and is back tomorrow to offer you the next batch of code that you can fix for him.

    If you still want to do that then that's fine - its your own time and your own patience. But you shouldn't do it here where it is frowned upon.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    mlkike is offline Member
    Join Date
    Jun 2015
    Posts
    4
    Rep Power
    0

    Default Re: Help needed with making a Frame with text field, radio buttons, buttons!

    Sure the intent was to help and the code I posted is useless if the OP doesn't work on it.
    I did not explain it here because I added comments in the code itself but next time I'll explain it.

    Sorry for that!

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Help needed with making a Frame with text field, radio buttons, buttons!

    Quote Originally Posted by mlkike View Post
    Sorry for that!
    No need to apologize; we urge people to find solutions with probably hints (not especially code) from us, so they actually learn something. Some people are beyond help and only want to copy/paste spoon fed solutions; but sometimes it works ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Radio buttons to open list of more radio buttons
    By mcconnelln89 in forum New To Java
    Replies: 1
    Last Post: 11-16-2013, 02:31 AM
  2. Replies: 2
    Last Post: 09-29-2012, 10:25 AM
  3. Help help.radio buttons
    By 10e5x in forum New To Java
    Replies: 3
    Last Post: 01-04-2011, 07:10 PM
  4. Help with Radio Buttons
    By miguelms19 in forum AWT / Swing
    Replies: 3
    Last Post: 06-29-2010, 07:49 PM
  5. How to use Radio Buttons
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:26 PM

Posting Permissions

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