0% found this document useful (0 votes)
3 views24 pages

Java AWT and Swing GUI Examples

The document contains a series of Java programs demonstrating various AWT and Swing components, including forms, layouts, menus, event handling, and database connectivity. Each program showcases different functionalities such as creating user interfaces, handling user input, and performing operations like addition and multiplication. Additionally, it includes examples of using networking classes to retrieve IP addresses and URL details.

Uploaded by

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

Java AWT and Swing GUI Examples

The document contains a series of Java programs demonstrating various AWT and Swing components, including forms, layouts, menus, event handling, and database connectivity. Each program showcases different functionalities such as creating user interfaces, handling user input, and performing operations like addition and multiplication. Additionally, it includes examples of using networking classes to retrieve IP addresses and URL details.

Uploaded by

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

1.

Design an application to create form using


TextField, TextArea, Button and Label.

import [Link].*;

public class BasicAWT {

public static void main(String args[]) {

Frame f = new Frame();

[Link](400,400);

[Link](true);

[Link](new FlowLayout()

);

Label l1 = new Label();

[Link]("Enter Your Name

");

TextField tf = new TextField("Atharva");

Label l2 = new Label("Address");

TextArea ta = new TextArea("",3,40);

Button b = new Button("Submit");

[Link](l1); [Link](tf); [Link](l2); [Link](ta);


[Link](b);
}

[Link] a program to select multiple languages


know to user (Marathi ,Hindi ,English
,Sanskrit) and gender of the user.
import [Link].*;

public class Prac1 {

public static void main(String[] args) {

Frame f=new Frame();

[Link](true);

[Link](400,400);

[Link](new FlowLayout());

Label l1=new Label();

[Link]("Select the

Subject");

Checkbox c1=new Checkbox("hindi");

Checkbox c2=new Checkbox("Marathi"


);
Checkbox c3=new Checkbox("English"
);
Checkbox c4=new Checkbox("Gujrati"
);

Label l2=new Label();

[Link]("Enter the Gender");

CheckboxGroup cg=new

CheckboxGroup();

Checkbox c5=new Checkbox("male",cg,true);

Checkbox c6=new

Checkbox("female",cg,true); [Link](l1);

[Link](c1);

[Link](c2);

[Link](c3);

[Link](c4);

[Link](l2);

[Link](c5);

[Link](c6);
}
}

[Link] an applet/application to select multiple names of news Papers using


List.

package kulsum;

import [Link].*;

public class news {

public static void main(String[] args)

{ Frame f=new Frame();

[Link](true);

[Link](400,400);

[Link](new FlowLayout());

Label l1=new Label("Select

newpaper"); List l=new List();

[Link](false);

[Link](l1);

[Link]("The mumbai Times");

[Link]("times of india");

[Link]("mumbai mirrors");

[Link]("hindustan times");

[Link](l);

[Link] an applet using List components to add names of 10 different cities.

package kulsum;

import [Link].*;
public class news {

public static void main(String[] args)

{ Frame f=new Frame();

[Link](true);

[Link](400,400);

[Link](new FlowLayout());

Label l1=new Label("Select cities");

List l=new List();

[Link](false);

[Link](l1);

[Link]("dehli");

[Link]("mumbai");

[Link]("Hydrabad");

[Link]("banglore");

[Link]("bihar");

[Link]("up");

[Link]("bhopal");

[Link]("Kashmir");

[Link]("Andhrapradesh");

[Link]("GUjrat");

[Link]("Ahmedabad");

[Link](l);

[Link] to demonstrate the use of Border layout .

package kulsum;
import [Link].*;

public class layout {

public static void main(String[]

args) { Frame f=new Frame();

[Link](true);

[Link](400,400);

[Link](new

BorderLayout());

Button b1=new Button("north");

Button b2=new Button("South");

Button b3=new Button("East");

Button b4=new Button("West");

Button b5=new Button("Center")


;

[Link](b1,[Link]);

[Link](b2,[Link]);

[Link](b3,[Link]);

[Link](b4,[Link]);

[Link](b5,[Link]);

[Link] to demonstrate the use of Grid layout .

package kulsum;

import [Link].*;

public class layout {


public static void main(String[]

args) { Frame f=new Frame();

[Link](true);

[Link](400,400);

[Link](new GridLayout());

Label b[]=new Label[25];

for(int i=0;i<25;i++) {

String

s=[Link](i+1);

b[i]=new Label(s);

[Link](b[i]);

[Link] a program which creates Menu of different colors and disable menu item
for Black color.

package
kulsum;

import [Link].*;

public class menueg

{ menueg(){

Frame f=new Frame();

[Link](true);

[Link](400,400);

[Link](new FlowLayout());

MenuBar mr=new MenuBar();


Menu m=new Menu("color");

MenuItem m1=new

MenuItem("Blue"); MenuItem

m2=new MenuItem("black");

[Link](false);

MenuItem m3=new

MenuItem("red"); MenuItem

m4=new MenuItem("Green");

[Link](m1);

[Link](m2);

[Link](m3);

[Link](m4);

[Link](m);

[Link](mr)

public static void main(String[] args)

{ menueg e=new menueg();

[Link] a program to perform addition of two nos using event.

package kulsum;

import [Link].*;

import [Link].*;

public class addition {

public static void main(String[]


args){ Frame f=new Frame();
[Link](400,400);

[Link](true);

[Link](new FlowLayout());

Label l1=new Label("Enter no");

[Link](l1);

TextField tf=new TextField();

[Link](tf);

Label l2=new Label("Eter 2nd no");

[Link](l2);

TextField tf2=new TextField();

[Link](tf2);

Label l3=new

Label("sum"); [Link](l3);

Button b=new Button("Submit");

[Link](b);

[Link](new ActionListener() {

public void actionPerformed(ActionEvent e) {

int

a=[Link]([Link]());

int

b=[Link]([Link]());

int c=a+b;

[Link](""+[Link](c));

});

}
[Link] using AWT to create a menubar in an applet window.

[Link] a program using swing to display a JcomboBox in an applet

package kulsum;

import [Link].*;

import [Link].*;

public class combo {

public static void main(String[] args)

{ JFrame jf=new JFrame();

[Link](300,300);

[Link](true);

[Link](new FlowLayout());

String

States[]={"bihar","manipur","mu,bai","dehli"};

JComboBox jc=new JComboBox(States);

[Link](jc);

[Link] a Jtree program to show root directory and its subFolders of your
System.

X1
package Practical7;
import [Link];
import [Link].*;
public class EX1{
public static void main(String[] args) {
JFrame frame = new
JFrame("directory");
[Link](true);
[Link](600, 400);
DefaultMutableTreeNode pc = new DefaultMutableTreeNode("This PC");
DefaultMutableTreeNode desktop = new DefaultMutableTreeNode("Desktop");
DefaultMutableTreeNode downloads = new DefaultMutableTreeNode("Downloads");
DefaultMutableTreeNode eDrive = new DefaultMutableTreeNode("USB Drive (E:)");
DefaultMutableTreeNode eclipse = new DefaultMutableTreeNode("Eclipse");
DefaultMutableTreeNode as = new DefaultMutableTreeNode("Android Studio");
DefaultMutableTreeNode image = new DefaultMutableTreeNode("[Link]");
DefaultMutableTreeNode pdf = new DefaultMutableTreeNode("[Link]");
DefaultMutableTreeNode COALAB203 = new DefaultMutableTreeNode("COALAB203");
DefaultMutableTreeNode Public = new DefaultMutableTreeNode("Public");
DefaultMutableTreeNode movie = new DefaultMutableTreeNode("Movie");
DefaultMutableTreeNode webSeires = new DefaultMutableTreeNode("Web Series");
[Link](desktop);
[Link](downloads);
[Link](eDrive);
[Link](eclipse);
[Link](as);
[Link](image);
[Link](pdf);
[Link](movie);
[Link](webSeires);
JTree tree = new
JTree(pc); [Link](tree);
}}

[Link] to create a table of name of student, percentage and grade of 10


students using JTable.

package arkp;

import [Link].*;

public class PR8_1 {

public static void main(String[] args) {

JFrame frame=new JFrame("JTable Demo");

[Link](700,500);

[Link](true);

String colHeads[]= {"ID","NAME","SALARY"};

Object data[][]=
{{101,"Amit",670000},{102,"Jay",780000},{103,"Sachin",700000}};

JTable table=new JTable(data,colHeads);

JScrollPane scroller=new JScrollPane(table);

[Link](scroller);
// TODO Auto-generated method stub

[Link] to display the key pressed, key typed and key released event on Applet
Window.

package kulsum;

import [Link].*;

import [Link].*;

public class keyaction implements KeyListener { // Implement KeyListener

keyaction() {

Frame f = new Frame();

[Link](true);

[Link](300, 400);

[Link](new FlowLayout());

TextField ta = new TextField(20); // Added a width for clarity

[Link](this); // Attach the KeyListener

[Link](ta);

}
// Implement KeyListener methods

@Override

public void keyPressed(KeyEvent e) {

[Link]("Key pressed: " +

[Link]());

@Override

public void keyTyped(KeyEvent e) {

[Link]("Key typed: " +

[Link]());

@Override

public void keyReleased(KeyEvent e) {

[Link]("Key released: " +

[Link]());

public static void main(String[] args) {

new keyaction();

15. Develop a program to accept two numbers and display product of two numbers
when user presses “Multiply” button.

package kulsum;

import [Link].*;

import

[Link].*;

public class mul {


public static void main(String[]

args) { Frame f=new Frame();


[Link](400,400);

[Link](true);

[Link](new FlowLayout());

TextField t1=new TextField();

TextField t2=new TextField();

Label l=new Label();

Button b=new Button("mul");

[Link](new ActionListener(){

public void actionPerformed(ActionEvent

e) { int

a=[Link]([Link]());

int

b=[Link]([Link]());

int c=a*b;

[Link]([Link](c));

});

[Link](t1);

[Link](t2);

[Link](b);

[Link](l);

[Link] a program using JPasswordField and JTextField to demonstrate the use of


user authentication.

import [Link].*;

import

[Link].*;

import [Link].*;
public class pr8 implements ActionListener {
JFrame frame = new JFrame("User

Authentication"); JLabel l1 = new

JLabel("UserName:");

JTextField jt = new

JTextField(20); JLabel l2 = new

JLabel("Password:");

JPasswordField jp = new JPasswordField(20);

JLabel l3 = new JLabel();

JButton b1 = new JButton("Login");

pr8() {

[Link](400, 300); // Set a reasonable frame size

[Link](new FlowLayout([Link], 10, 20)); // Use


FlowLayout with spacing

[Link](true);

// Add components to the frame

[Link](l1);

[Link](jt

);

[Link](l2

);

[Link](jp

);

[Link](b1

);

[Link](l3

);

// Customize password field

[Link]('#');
[Link](this);

// Close operation for the frame

[Link](JFrame.EXIT_ON_CLOSE);

}
public static void main(String[] args)

{ new pr8();

@Override

public void actionPerformed(ActionEvent e)

{ String Username = "abc";

String Password = "abc123";

String user = [Link]();

String pass = new String([Link]()); // Use getPassword() for


JPasswordField

if ([Link](user) && [Link](pass) && [Link]() > 6)

{ [Link]("Login Successfully");

} else {

[Link]("<html>Invalid Username/Password<br>Password must be > 6


characters</html>");

17. Write a program to count the number of clicks performed by the user in a
frame.

import [Link].*;

import [Link].*;

import [Link].*;

public class Pr11_1 extends JFrame implements

MouseListener{ Panel p;

public Pr11_1() {
setVisible(true);

setSize(400,600);

p=new Panel();

add(p);

[Link](Color.LIGHT_GRAY);

[Link](this);

@Override

public void mouseClicked(MouseEvent e) {

@Override

public void mousePressed(MouseEvent e) {

[Link]([Link]);

@Override

public void mouseReleased(MouseEvent e) {

[Link]([Link]);

@Override

public void mouseEntered(MouseEvent e) {

@Override

public void mouseExited(MouseEvent e) {

}
public static void main(String args[])

{ Pr11_1 p=new Pr11_1();

18. Develop a program using InetAddress class to retrieve IP address of computer


when hostname is entered by the user.

package kulsum;

import [Link].*;

import [Link].*;

public class hostaddress {

public static void main(String[] args) {

Scanner sc=new Scanner([Link]);

[Link]("enter the

host"); String host=[Link]();

try {

InetAddress in= [Link](host);

[Link]("Ip address"+[Link]());

catch(Exception e) {}

}
19. Write a program using URL class to retrieve the host, protocol, port and the
file of URL

[Link]

package kulsum;

import [Link];

import [Link].*;

public class url {

public static void main(String[] args) throws

MalformedURLException{ URL u=new URL(

"[Link] );

[Link]("host"+[Link]());

[Link]("file"+[Link]());

[Link]("port"+[Link]());

[Link]("protocol"+[Link]());

20. Write a program to create a Student table in database and insert a record in the
table.

import [Link].*;

public class pr18 {

public static void main(String[] args) {

try {

[Link]("[Link]");

Connection
conn=[Link]("jdbc:oracle:thin:@localhost:1521:xe","system","Admin");

[Link]("Database Connected");

Statement smt=[Link]();
int i=[Link](create table student0(s_id number,s_name
varchar(13),percentage number);

[Link]("Table created Succefully");

catch(Exception e) {

[Link](e);

You might also like