Swing

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

import javax.swing.

*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class QFrame extends JFrame implements ActionListener
{
JFrame fr;
JRadioButton rb1,rb2,rb3,rb4;
JButton b1,b2;
JLabel lb1,lb2;
ButtonGroup bg;
String ques[]={"What is the value of f(0) when f(x) = 32x+6?","If the
events have the same theoretical probability of happening, then they are
called","n(A-B) + n(A ∩ B ) =","The total value of pie is always equal to","The 6th
term of an AP is 6 and the 16th term is 14. What is the 27th term?","What is the
12th term of the sequence -2,-4,-6,...........-100?","Look at this series: 36, 34,
30, 28, …, 22 What number should come to fill in the blank space"};
String op1[]={"2","Mutually exclusive
events","n(A)","50%","114/5","-28","25"};
String op2[]={"4","Mutually exhaustive
events","n(B)","25%","118/5","-26","24"};
String op3[]={"6","Equally likely events","n(A U
B)","75%","22/5","-24","26"};
String op4[]={"8","Impossible events","n(B-
A)","100%","106/5","-20","None of These"};
String ans[]={"6","Equally likely
events","n(A)","100%","114/5","-24","24"};
int cn,crt=0,not=0;
QFrame()
{
fr=new JFrame();
fr.setLayout(null);
fr.setSize(600,600);
Container c=fr.getContentPane();
c.setBackground(Color.cyan);
lb1=new JLabel(ques[0]);
lb1.setBounds(50,50,300,30);
fr.add(lb1);
lb1.setFont(new Font("chiller",Font.BOLD,30));
rb1=new JRadioButton(op1[0]);
rb1.setBounds(100,120,100,30);
fr.add(rb1);
rb2=new JRadioButton(op2[0]);
rb2.setBounds(350,120,100,30);
fr.add(rb2);
rb3=new JRadioButton(op3[0]);
rb3.setBounds(100,200,100,30);
fr.add(rb3);
rb4=new JRadioButton(op4[0]);
rb4.setBounds(350,200,100,30);
fr.add(rb4);
bg =new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
bg.add(rb3);
bg.add(rb4);
rb1.addActionListener(this);
rb2.addActionListener(this);
rb3.addActionListener(this);
rb4.addActionListener(this);
b1=new JButton("Sumbit");
b1.setBounds(100,400,100,30);
fr.add(b1);
b2=new JButton("Next");
b2.setBounds(250,400,100,30);
fr.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
fr.setVisible(true);

}
public void actionPerformed(ActionEvent f)
{
JFrame frame;JLabel j;
if(f.getSource()==b1)
{
String en="";
if(rb1.isSelected())
en=rb1.getText();
if(rb2.isSelected())
en=rb2.getText();
if(rb3.isSelected())
en=rb3.getText();
if(rb4.isSelected())
en=rb4.getText();
if(en.equals(ans[cn]))
{
JOptionPane.showMessageDialog(null,"Right Answer");
crt++;
}
else
{
JOptionPane.showMessageDialog(null,"Wrong Answer");
not++;
}
}
if (f.getSource()==b2)
{
cn++;
lb1.setText(ques[cn]);
rb1.setText(op1[cn]);
rb2.setText(op2[cn]);
rb3.setText(op3[cn]);
rb4.setText(op4[cn]);
}
frame = new JFrame();
j = new JLabel();
j.setText("Score : \n" + "Correct : "+crt+"\n" + "Incorrect : " + not
+"\n" );
JPanel p = new JPanel();
p.add(j);
frame.add(p);
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
class MyFrame extends JFrame implements ActionListener{
JLabel name,age;JTextField name_tf,age_tf;JButton b;JPanel p;
int count = 0;
MyFrame()
{
name = new JLabel("Name");
age = new JLabel("Age");
name_tf = new JTextField(20);age_tf = new JTextField(7);
b = new JButton("OK");
p = new JPanel();
p.add(name);p.add(name_tf);p.add(age);p.add(age_tf);p.add(b);
Border br =
BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.ORAN
GE),"Login",TitledBorder.CENTER,TitledBorder.LEFT);
p.setBorder(br);
setLayout(new FlowLayout());
add(p);
b.addActionListener(this);

}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
String en = age_tf.getText();
int age = Integer.parseInt(en);
if(age <= 18)
{
final JPanel panel = new JPanel();
JOptionPane.showMessageDialog(panel, "Didn't Meet The Minimum
Age Requirement!", "Error", JOptionPane.ERROR_MESSAGE);
}
else
{
QFrame f = new QFrame();
f.setSize(500,500);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}

}
public class SwingAssignment
{
public static void main(String[] args)
{
MyFrame f = new MyFrame();
f.setSize(800,800);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

You might also like