0% found this document useful (0 votes)
2 views8 pages

Gen 4

Uploaded by

shwetharoopesh25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Gen 4

Uploaded by

shwetharoopesh25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

General Programs using Swing Components

1. Simple Label and Button

import [Link].*;
import [Link].*;

public class ButtonExample {


public static void main(String[] args) {
JFrame f = new JFrame("Button Example");

JLabel l = new JLabel("Click the button");


[Link](100,50,200,30);

JButton b = new JButton("Click");


[Link](100,100,100,30);

[Link](new ActionListener(){
public void actionPerformed(ActionEvent e){
[Link]("Button Clicked!");
}
});

[Link](l);
[Link](b);

[Link](300,250);
[Link](null);
[Link](true);
}
}

2. Addition of Two Numbers

import [Link].*;
import [Link].*;

public class AddNumbers {


public static void main(String[] args) {

JFrame f = new JFrame("Addition");

JTextField t1 = new JTextField();


JTextField t2 = new JTextField();
JLabel result = new JLabel("Result");

JButton b = new JButton("Add");

[Link](50,50,100,30);

1
[Link](50,90,100,30);
[Link](50,130,100,30);
[Link](50,170,200,30);

[Link](e -> {
int a = [Link]([Link]());
int b1 = [Link]([Link]());
[Link]("Sum = " + (a+b1));
});

[Link](t1); [Link](t2); [Link](b); [Link](result);

[Link](300,300);
[Link](null);
[Link](true);
}
}

3. CheckBox Example

import [Link].*;
import [Link].*;

public class CheckBoxExample {


public static void main(String[] args) {

JFrame f = new JFrame("Hobbies");

JCheckBox c1 = new JCheckBox("Reading");


JCheckBox c2 = new JCheckBox("Music");

JButton b = new JButton("Show");

[Link](100,50,100,30);
[Link](100,80,100,30);
[Link](100,120,100,30);

[Link](e -> {
String s = "Selected: ";
if([Link]()) s += "Reading ";
if([Link]()) s += "Music ";
[Link](f,s);
});

[Link](c1); [Link](c2); [Link](b);

[Link](300,250);

2
[Link](null);
[Link](true);
}
}

4. Radio Button Example

Select gender.

import [Link].*;

public class RadioExample {


public static void main(String[] args) {

JFrame f = new JFrame("Gender");

JRadioButton r1 = new JRadioButton("Male");


JRadioButton r2 = new JRadioButton("Female");

ButtonGroup bg = new ButtonGroup();


[Link](r1);
[Link](r2);

JButton b = new JButton("Submit");

[Link](100,50,100,30);
[Link](100,80,100,30);
[Link](100,120,100,30);

[Link](e -> {
if([Link]())
[Link](f,"Male Selected");
else
[Link](f,"Female Selected");
});

[Link](r1); [Link](r2); [Link](b);

[Link](300,250);
[Link](null);
[Link](true);
}
}

3
5. ComboBox Example

Select a course.

import [Link].*;

public class ComboExample {


public static void main(String[] args) {

JFrame f = new JFrame("Courses");

String courses[] = {"BCA","BSc","BCom"};

JComboBox cb = new JComboBox(courses);


JButton b = new JButton("Show");

[Link](100,50,100,30);
[Link](100,100,100,30);

[Link](e -> {
String s = (String)[Link]();
[Link](f,s);
});

[Link](cb);
[Link](b);

[Link](300,200);
[Link](null);
[Link](true);
}
}

4
6. TextArea Example

Displays entered text.

import [Link].*;

public class TextAreaExample {


public static void main(String[] args) {

JFrame f = new JFrame("Feedback");

JTextArea ta = new JTextArea();


JButton b = new JButton("Submit");

[Link](50,50,200,100);
[Link](100,170,100,30);

[Link](e -> {
[Link](f, [Link]());
});

[Link](ta);
[Link](b);

[Link](300,300);
[Link](null);
[Link](true);
}
}

7. Password Field Example

import [Link].*;

public class PasswordExample {


public static void main(String[] args) {

JFrame f = new JFrame("Login");

JLabel l = new JLabel("Password:");


JPasswordField p = new JPasswordField();
JButton b = new JButton("Login");

[Link](50,50,100,30);
[Link](150,50,100,30);
[Link](100,100,100,30);

[Link](e -> {
String pass = new String([Link]());
[Link](f,"Password: "+pass);

5
});

[Link](l);
[Link](p);
[Link](b);

[Link](300,200);
[Link](null);
[Link](true);
}
}

8. Menu Example

import [Link].*;

public class MenuExample {


public static void main(String[] args) {

JFrame f = new JFrame("Menu");

JMenuBar mb = new JMenuBar();

JMenu m = new JMenu("File");


JMenuItem i1 = new JMenuItem("New");
JMenuItem i2 = new JMenuItem("Exit");

[Link](i1);
[Link](i2);
[Link](m);

[Link](mb);

[Link](300,200);
[Link](true);
}
}

6
9. List Example

import [Link].*;

public class ListExample {


public static void main(String[] args) {

JFrame f = new JFrame("Languages");

String lang[] = {"Java","Python","C","C++"};

JList list = new JList(lang);


[Link](100,50,100,80);

[Link](list);

[Link](300,250);
[Link](null);
[Link](true);
}
}

10. Simple Login Form

import [Link].*;

public class LoginForm {


public static void main(String[] args) {

JFrame f = new JFrame("Login");

JLabel l1 = new JLabel("Username");


JLabel l2 = new JLabel("Password");

JTextField t1 = new JTextField();


JPasswordField p1 = new JPasswordField();

JButton b = new JButton("Login");

[Link](50,50,100,30);
[Link](50,90,100,30);
[Link](150,50,100,30);
[Link](150,90,100,30);
[Link](100,140,100,30);

[Link](l1); [Link](l2);
[Link](t1); [Link](p1);
[Link](b);

7
[Link](300,250);
[Link](null);
[Link](true);
}
}

You might also like