0% found this document useful (0 votes)
22 views7 pages

21BCM075 Csi0403 Practical 4

The document describes a Java program that creates a menu-based GUI with 4 options: 1) read a text file, 2) save content to a text file, 3) copy a file, and 4) delete a file. The program uses AWT classes like Frame, Button, Label, and TextArea to build the GUI interface and menu. It implements event handlers for each button that open new windows to prompt for file operations like reading, writing, copying and deleting files.

Uploaded by

Chirag Saraf
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)
22 views7 pages

21BCM075 Csi0403 Practical 4

The document describes a Java program that creates a menu-based GUI with 4 options: 1) read a text file, 2) save content to a text file, 3) copy a file, and 4) delete a file. The program uses AWT classes like Frame, Button, Label, and TextArea to build the GUI interface and menu. It implements event handlers for each button that open new windows to prompt for file operations like reading, writing, copying and deleting files.

Uploaded by

Chirag Saraf
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
You are on page 1/ 7

ROLL NUMBER : 21BCM075

COURSE CODE : CSI0403


PRACTICAL : 4
Write a java program to create a menu-based GUI
program using AWT class. Various menu options should
be: 1) Read a text file 2) Save content to a text file 3)
Copy a file 4) Delete a file
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class prac4 extends Frame implements ActionListener {


prac4() {

Button b1 = new Button("READ");


Button b2 = new Button("SAVE");
Button b3 = new Button("COPY");
Button b4 = new Button("DELETE");

setVisible(true);

add(b1);
add(b2);
add(b3);
add(b4);

setSize(400, 400);

setLayout(null);

setBackground(Color.pink);

setTitle(" 21BCM075 MENU");


b1.setBounds(160, 100, 80, 30);
b2.setBounds(160, 140, 80, 30);
b3.setBounds(160, 180, 80, 30);
b4.setBounds(160, 220, 80, 30);

b1.setActionCommand("b1");
b2.setActionCommand("b2");
b3.setActionCommand("b3");
b4.setActionCommand("b4");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);

@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "b1") {
Frame f = new Frame();
Button B5 = new Button("LOAD");
B5.setActionCommand("B5");

f.add(B5);
B5.setBounds(250, 90, 80, 30);
f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
f.setBackground(Color.red);
Label l = new Label("ENTER FILE NAME ");
Label m1 = new Label("CONTENT OF THE FILE");
f.add(m1);
m1.setBounds(100, 170, 150, 30);
f.add(l);
l.setBounds(100, 60, 150, 30);
f.setTitle("READ A TEXT FILE");
TextField t = new TextField();
t.setBounds(100, 90, 150, 30);
f.add(t);
TextArea ta = new TextArea();
ta.setBounds(100, 200, 300, 280);
f.add(ta);
B5.addActionListener(new ActionListener() {
String s = "";
int a;

@Override
public void actionPerformed(ActionEvent e) {
try {
InputStream i = new FileInputStream(t.getText());
while ((a = i.read()) != -1) {
s = s + (char) a;
}
} catch (Exception m) {
ta.setText("FILE NOT FOUND!");
}
ta.setText(s);
}
});
}
if (e.getActionCommand() == "b2") {
Frame f2 = new Frame();
Button B6 = new Button("SAVE");
B6.setActionCommand("B6");
f2.add(B6);
f2.setSize(500, 500);
f2.setLayout(null);
f2.setVisible(true);
f2.setBackground(Color.cyan);
B6.setBounds(320, 100, 80, 30);
TextArea t1 = new TextArea();
t1.setBounds(100, 200, 300, 250);
f2.add(t1);
f2.setTitle("SAVE CONTENT TO A TEXT FILE");
Label ta1 = new Label();
Label za1 = new Label();
za1.setText("ENTER FILE NAME");
f2.add(za1);
ta1.setBounds(100, 170, 250, 30);
za1.setBounds(100, 70, 130, 30);
f2.add(ta1);
ta1.setText("ENTER TEXT TO BE SAVED IN THE FILE");
TextField z1 = new TextField();
f2.add(z1);
z1.setBounds(100, 100, 150, 30);

B6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
char[] str = new char[100];
String s = t1.getText();
int l = s.length();
str = s.toCharArray();
String file = z1.getText();
try {
OutputStream o = new FileOutputStream(file);
for (int i = 0; i < l; i++) {
o.write(str[i]);
}
} catch (Exception E) {
}
}
});
}
if (e.getActionCommand() == "b3") {
Frame f3 = new Frame();
Button B7 = new Button("COPY");
B7.setActionCommand("B6");
f3.add(B7);
f3.setSize(500, 500);
f3.setLayout(null);
f3.setVisible(true);
f3.setTitle("COPY A FILE");
f3.setBackground(Color.orange);
B7.setBounds(320, 350, 80, 30);
TextField t2 = new TextField();
t2.setBounds(100, 130, 190, 30);
f3.add(t2);
Label ta2 = new Label();
Label xa2 = new Label();
f3.add(xa2);
xa2.setBounds(100, 220, 250, 30);
ta2.setBounds(100, 100, 250, 30);
f3.add(ta2);
ta2.setText("ENTER SOURCE NAME OF FILE");
TextField x1 = new TextField();
xa2.setText("ENTER DESTINATION NAME OF FILE");
f3.add(x1);
x1.setBounds(100, 250, 220, 30);

B7.addActionListener(new ActionListener() {
String s = "";
int a;

@Override
public void actionPerformed(ActionEvent e) {
try {
InputStream i = new
FileInputStream(t2.getText());
OutputStream o = new
FileOutputStream(x1.getText());
while ((a = i.read()) != -1) {
o.write(a);
}
} catch (Exception E) {
System.out.println(E);
}
}
});
}
if (e.getActionCommand() == "b4") {
Frame f4 = new Frame();
Button B8 = new Button("DELETE");
B8.setActionCommand("B6");
f4.add(B8);
f4.setSize(500, 500);
f4.setLayout(null);
f4.setVisible(true);
f4.setTitle("DELETE A FILE");
f4.setBackground(Color.magenta);
B8.setBounds(300, 200, 80, 30);
TextField t3 = new TextField();
t3.setBounds(100, 130, 220, 30);
f4.add(t3);
Label ta3 = new Label();
ta3.setBounds(100, 100, 250, 30);
f4.add(ta3);
ta3.setText("ENTER FILE YOU WANT TO DELETE");

B8.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File f = new File(t3.getText());
f.delete();
}
});
}
}

public static void main(String[] args) {


prac4 p = new prac4();
}
}

OUTPUT :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy