Online-Academy

Look, Read, Understand, Apply

Menu

JPanel_Multiple_questions

This program is about creating multiple choice questions. A JPanel, JRadioButtons, JButton, JLabel are used in the JFrame container to create multiple choice questions. As button is clicked new question is choices are shown.

Event on the button is handled with nameless ActionListener object.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class l33 extends JFrame{
    JPanel p1;
    JButton btn,btn1;
    JLabel ltext;
    JRadioButton r1,r2;
    ButtonGroup gg;
    public l33(){
        p1 = new JPanel();
        r1 = new JRadioButton("Male");
        r2 = new JRadioButton("FeMale");
        gg = new ButtonGroup();
        gg.add(r1);
        gg.add(r2);
        btn = new JButton("click");
        btn1 = new JButton("This is a problem");
        ltext = new JLabel("Specify your gender: ");
        p1.add(ltext);
        p1.add(r1);
        p1.add(r2);
        add(btn1,BorderLayout.NORTH);
        add(p1,BorderLayout.CENTER);
        add(btn,BorderLayout.SOUTH);
        setSize(400,300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        btn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent a){
                if(a.getActionCommand()=="click"){
                    btn.setBackground(Color.MAGENTA);
                }else{
                    //p2 = new JPanel();
                    r1.setText("Jungle");
                    r2.setText("Daka");
                    ltext.setText("Specify your ADdress: ");
                }
            }
        });
    }
    public static void main(String[] ppp){
        new l33();
    }
}