Skip to content

Commit a70210a

Browse files
committed
Implemented GUI in java
1 parent 3172c59 commit a70210a

File tree

7 files changed

+281
-0
lines changed

7 files changed

+281
-0
lines changed

LAB8 - GUI/EventDemo.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import javax.swing.*;
4+
import java.awt.event.*;
5+
6+
public class EventDemo extends JFrame implements ActionListener {
7+
JButton b;
8+
JTextField tf;
9+
10+
EventDemo(){
11+
b=new JButton("Click Me");
12+
tf=new JTextField(20);
13+
b.setBounds(20,80,120,30);
14+
tf.setBounds(30, 40,260,30);
15+
add(b);
16+
add(tf);;
17+
b.addActionListener(this);
18+
setLayout(null);
19+
setSize(300,300);
20+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21+
setVisible(true);
22+
}
23+
24+
public void actionPerformed(ActionEvent ae){
25+
tf.setText("Welcome");
26+
}
27+
28+
public static void main(String[] args) {
29+
EventDemo ed=new EventDemo();
30+
}
31+
}

LAB8 - GUI/EventDemoo.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.awt.*;
2+
import javax.swing.*;
3+
import java.awt.event.*;
4+
public class EventDemoo extends JFrame implements ActionListener {
5+
JButton b;
6+
JLabel l;
7+
int count=0;
8+
EventDemoo(){
9+
b=new JButton("Click Here");
10+
l=new JLabel();
11+
b.setBounds(20,80,120,30);
12+
l.setBounds(30, 40,260,30);
13+
add(b);
14+
add(l);;
15+
b.addActionListener(this);
16+
setLayout(null);
17+
setSize(300,300);
18+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19+
setVisible(true);
20+
}
21+
22+
public void actionPerformed(ActionEvent ae){
23+
l.setText(String.valueOf(++count));
24+
}
25+
26+
public static void main(String[] args) {
27+
EventDemoo ed=new EventDemoo();
28+
}
29+
}

LAB8 - GUI/GUIDemo.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.swing.*;
2+
import java.awt.*;
3+
4+
public class GUIDemo{
5+
public static void main(String[] args){
6+
7+
8+
JFrame f=new JFrame('Registration');
9+
10+
JLabel lHeader=new JLabel("Student Registration");
11+
lHeader.setBounds(260,10,120,20);
12+
f.add(lHeader);
13+
14+
JLabel lName = new JLabel("Name:");
15+
lName.setBounds(30,50,70,10);
16+
f.add(lName);
17+
18+
JTextField tName=new JTextField();
19+
tName.setBounds(100,45,120,20);
20+
f.add(tName);
21+
22+
String[] age={"17","18","19","20"};
23+
JLabel lAge= new JLabel("Age");
24+
lAge.setBounds(30,80,70,20);
25+
f.add(lAge);
26+
27+
JcomboBox cAge=new JcomboBox(age);
28+
cAge.setBounds(100,100,70,20);
29+
f.add(
30+
31+
)
32+
33+
f.setLayout(null);
34+
f.setSize(800,600);
35+
f.setVisible(true);
36+
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
37+
}
38+
}

LAB8 - GUI/Main.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
System.out.println("Hello world!");
4+
}
5+
}

LAB8 - GUI/form.java

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
public class form {
4+
public static void main(String[] args) {
5+
JFrame f=new JFrame("Registration");
6+
7+
JLabel lHeader=new JLabel("STUDENT'S REGISTRATION FORM");
8+
lHeader.setBounds(200,20,300,30);
9+
f.add(lHeader);
10+
11+
JLabel lName= new JLabel("Name : ");
12+
lName.setBounds(100,60,100,30);
13+
f.add(lName);
14+
15+
JTextField tName=new JTextField(100);
16+
tName.setBounds(200,60,300,30);
17+
f.add(tName);
18+
19+
JLabel lAge=new JLabel("Age : ");
20+
lAge.setBounds(100,100,100,30);
21+
f.add(lAge);
22+
23+
/* JTextField tAge=new JTextField(80);
24+
tAge.setBounds(300,100,100,30);
25+
f.add(tAge);
26+
*/
27+
String age[]={"17","18","19","20","21"};
28+
JComboBox cAge=new JComboBox(age);
29+
cAge.setBounds(200,100,300,30);
30+
f.add(cAge);
31+
32+
JLabel lGender=new JLabel("Gender : ");
33+
lGender.setBounds(100,140,100,30);
34+
f.add(lGender);
35+
36+
JRadioButton rMale=new JRadioButton("Male");
37+
rMale.setBounds(200,140,100,30);
38+
f.add(rMale);
39+
40+
JRadioButton rFemale=new JRadioButton("Female");
41+
rFemale.setBounds(300,140,100,30);
42+
f.add(rFemale);
43+
44+
JRadioButton rOthers=new JRadioButton("Others");
45+
rOthers.setBounds(400,140,100,30);
46+
f.add(rOthers);
47+
48+
JLabel lHobbies = new JLabel("Hobbies : ");
49+
lHobbies.setBounds(100,180,100,30);
50+
f.add(lHobbies);
51+
52+
JCheckBox cb1=new JCheckBox("Playing");
53+
cb1.setBounds(200,180,100,30);
54+
f.add(cb1);
55+
56+
JCheckBox cb2=new JCheckBox("Singing");
57+
cb2.setBounds(300,180,100,30);
58+
f.add(cb2);
59+
60+
JCheckBox cb3=new JCheckBox("Coding");
61+
cb3.setBounds(400,180,100,30);
62+
f.add(cb3);
63+
64+
JLabel lCountries =new JLabel("Countries : ");
65+
lCountries.setBounds(100,220,100,30);
66+
f.add(lCountries);
67+
68+
String [] countries={"Nepal","India","USA","England"};
69+
JComboBox tb=new JComboBox(countries);
70+
tb.setBounds(200,220,300,30);
71+
f.add(tb);
72+
73+
JLabel lComments =new JLabel("Comments : ");
74+
lComments.setBounds(100,260,100,30);
75+
f.add(lComments);
76+
77+
JTextArea tComments=new JTextArea();
78+
tComments.setBounds(200,260,300,90);
79+
f.add(tComments);
80+
81+
JButton btn1=new JButton("SUBMIT");
82+
JButton btn2=new JButton("CANCEL");
83+
btn1.setBounds(220,400,100,30);
84+
btn2.setBounds(380,400,100,30);
85+
f.add(btn1);
86+
f.add(btn2);
87+
88+
f.setLayout(null);
89+
f.setSize(600,600);
90+
f.setVisible(true);
91+
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
92+
}
93+
}

LAB8 - GUI/keyDemo.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.awt.*;
2+
import javax.swing.*;
3+
import java.awt.event.*;
4+
5+
public class keyDemo extends JFrame implements KeyListener{
6+
JLabel l;
7+
JTextArea ta;
8+
keyDemo(){
9+
l=new JLabel("Key here");
10+
ta=new JTextArea();
11+
l.setBounds(70,70,150,30);
12+
ta.setBounds(30,120,150,30);
13+
add(l);
14+
add(ta);
15+
ta.addKeyListener(this);
16+
setSize(500,500);
17+
setTitle("Key Example");
18+
setLayout(null);
19+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20+
setVisible(true);
21+
}
22+
23+
public static void main(String[] args) {
24+
keyDemo k=new keyDemo();
25+
}
26+
27+
public void keyTyped(KeyEvent ke){
28+
l.setText("Key Typed !☺ ");
29+
}
30+
31+
public void keyPressed(KeyEvent ke){
32+
l.setText("Key Pressed ! ☻");
33+
}
34+
35+
public void keyReleased(KeyEvent ke){
36+
l.setText("key Released ! ☹");
37+
}
38+
}

LAB8 - GUI/mouseDemo.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import javax.swing.*;
2+
import java.awt.event.*;
3+
4+
public class mouseDemo extends JFrame implements MouseListener{
5+
JLabel l;
6+
JTextArea ta;
7+
mouseDemo(){
8+
l=new JLabel("Mouse here");
9+
ta=new JTextArea();
10+
l.setBounds(70,70,150,30);
11+
ta.setBounds(30,120,350,80);
12+
add(l);
13+
add(ta);
14+
ta.addMouseListener(this);
15+
setSize(500,500);
16+
setTitle("Mouse Events Example");
17+
setLayout(null);
18+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19+
setVisible(true);
20+
}
21+
22+
public static void main(String[] args) {
23+
mouseDemo m=new mouseDemo();
24+
}
25+
26+
public void mouseClicked(MouseEvent me){
27+
l.setText("Mouse Clicked !☺ ");
28+
}
29+
30+
public void mouseEntered(MouseEvent me){
31+
l.setText("Mouse Entered ! ☻");
32+
}
33+
34+
public void mouseExited(MouseEvent me){
35+
l.setText("Mouse Exited ! ☹");
36+
}
37+
38+
39+
public void mousePressed(MouseEvent me){
40+
l.setText("Mouse Pressed ! ☹");
41+
}
42+
43+
44+
public void mouseReleased(MouseEvent me){
45+
l.setText("Mouse Released ! ☹");
46+
}
47+
}

0 commit comments

Comments
 (0)