Java Practical File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 44

Java Practical file

(University of Delhi)
Sem-2 B.Sc. CS hons

Index
Sno. Programs Pg.no.
1. Design a class Complex having a real part (x) and an imaginary part (y). 4
Provide methods to perform the following on complex numbers:
a) Add two complex numbers.
b) Multiply two complex numbers.
c) toString() method to display complex numbers in the form: x + i y

1
2. Create a class TwoDim which contains private members as x and y 6
coordinates in package P1. Define the default constructor, a parameterized
constructor and override toString() method to display the co-ordinates.
Now reuse this class and in package P2 create another class ThreeDim,
adding a new dimension as z as its private member. Define the constructors
for the subclass and override toString() method in the subclass also. Write
appropriate methods to show dynamic method dispatch. The main()
function should be in a package P.

3. Define an abstract class Shape in package P1. Inherit two more classes: 8
Rectangle in package P2 and Circle in package P3. Write a program to ask
the user for the type of shape and then using the concept of dynamic
method dispatch, display the area of the appropriate subclass. Also write
appropriate methods to read the data. The main() function should not be in
any package.

4. Create an exception subclass UnderAge, which prints “Under Age” along 11


with the age value when an object of UnderAge class is printed in the catch
statement. Write a class exceptionDemo in which the method test() throws
UnderAge exception if the variable age passed to it as argument is less than
18. Write main() method also to show working of the program

5. Write a program to implement stack. Use exception handling to manage 13


underflow and overflow conditions.

6. Write a program that copies content of one file to another. Pass the names 16
of the files through command-line arguments.

7. Write a program to read a file and display only those lines that have the 18
first two characters as '//' (Use try with resources).

8. Write a program to create a frame using AWT. Implement mouseClicked(), 20


mouseEntered() and mouseExited() events such that: a) Size of the frame
should be tripled when mouse enters it. b) Frame should reduce to its
original size when mouse is clicked in it. c) Close the frame when mouse
exits it.

9. Using AWT, write a program to display a string in frame window with pink 23
color as background.

10. Using AWT, write a program to create two buttons named “Red” and 25
“Blue”. When a button is pressed the background color should be set to the
color named by the button’s label.

11. Using AWT, write a program using appropriate adapter class to display the 28
message (“Typed character is: ”) in the frame window when user types any
key.

12. Using AWT, write a program to create two buttons labelled ‘A’ and ‘B’. 31
When button ‘A’ is pressed, it displays your personal information (Name,
Course, Roll No, College) and when button ‘B’ is pressed, it displays your

2
CGPA in previous semester.

13. Rewrite all the above GUI programs using Swing. 35

Practical-1

Q1) Design a class Complex having a real part (x) and an imaginary part (y). Provide methods
to perform the following on complex numbers:
a) Add two complex numbers.
b) Multiply two complex numbers.
c) toString() method to display complex numbers in the form: x + i y

3
Code:

package java_practical;

public class Complex {


private int x;
private int y;

public Complex(int x, int y) {


this.x=x;
this.y=y;
}
public void sum_of_complex(Complex c2) {
int real= this.x+ c2.x;
int img= this.y+ c2.y;
System.out.println("Sum of complex numbers is: ");
toString(real,img);
}
public void product_of_complex(Complex c2) {
int real=(this.x*c2.x)-(this.y*c2.y);
int img=(this.x*c2.y)+(this.y*c2.x);
System.out.println("Product of complex numbers is: ");
toString(real,img);
}
public void toString(int a, int b) {
System.out.println(a+" + i "+b);
}
public static void main(String[] args) {
Complex c1= new Complex(3,2);
Complex c2= new Complex(1,4);
c1.sum_of_complex(c2);
c1.product_of_complex(c2);

}
Output:

4
Practical-2

5
Q2) Create a class TwoDim which contains private members as x and y coordinates in
package P1. Define the default constructor, a parameterized constructor and override
toString() method to display the co-ordinates. Now reuse this class and in package P2 create
another class ThreeDim, adding a new dimension as z as its private member. Define the
constructors for the subclass and override toString() method in the subclass also. Write
appropriate methods to show dynamic method dispatch. The main() function should be in a
package P.

Code:
package p1;

public class TwoDim {


private int x;
private int y;

//default constructor
public TwoDim() {
this.x=0;
this.y=0;
}

//parameterized constructor
public TwoDim(int x, int y) {
this.x=x;
this.y=y;
}

public void tostring() {


System.out.println("x coordinate:"+x+"\ny coordinate:"+y);
}
}

package p2;
import p1.TwoDim;

public class ThreeDim extends TwoDim{


private int z;

//default constructor
public ThreeDim() {
super(0,0);
this.z=0;
}

6
//parameterized constructor
public ThreeDim(int x, int y, int z) {
super(x,y);
this.z=z;
}

@Override
public void tostring() {
super.tostring();
System.out.println("z coordinate:"+z);
}
}

package p;
import p1.TwoDim;
import p2.ThreeDim;

public class Main {


public static void main(String[] args) {
TwoDim t = new ThreeDim(9,5,8); //dynamic method dispatch
t.tostring();
}

Output:

Practical-3

7
Q3) Define an abstract class Shape in package P1. Inherit two more classes: Rectangle in
package P2 and Circle in package P3. Write a program to ask the user for the type of shape
and then using the concept of dynamic method dispatch, display the area of the appropriate
subclass. Also write appropriate methods to read the data. The main() function should not
be in any package.

Code:
package p1;

public abstract class Shape {


public abstract void get_data(double ...args);
public abstract double area();
}

package p2;
import p1.Shape;

public class Rectangle extends Shape{


private double length;
private double breadth;
public void get_data(double ...args) {
this.length=args[0];
this.breadth=args[1];
}
public double area() {
double area=length*breadth;
return area;
}

package p3;
import p1.Shape;

public class Circle extends Shape{


private double radius;
public void get_data(double ...args) {
this.radius=args[0];
}
public double area() {
double area=3.14*radius*radius;
return area;
}
}
8
import java.util.Scanner;
import p1.Shape;
import p2.Rectangle;
import p3.Circle;

public class Prac3 {

public static void main(String[] args) {


Scanner sc= new Scanner(System.in);
System.out.println("Enter shape Rectangle/Circle:");
String ch= sc.nextLine();
if (ch.equalsIgnoreCase("Rectangle")) {
Shape s= new Rectangle();
System.out.println("Enter length of rectangle");
double l= sc.nextDouble();
System.out.println("Enter breadth of rectangle");
double b= sc.nextDouble();
s.get_data(l,b);
System.out.println("Area of rectangle: "+s.area());
}
else if (ch.equalsIgnoreCase("Circle")) {
Shape s= new Circle();
System.out.println("Enter radius of circle");
double r= sc.nextDouble();
s.get_data(r);
System.out.println("Area of circle: "+s.area());
}
else {
System.out.println("Invalid shape!!");
}
}

Output:

9
10
Practical-4

Q4) Create an exception subclass UnderAge, which prints “Under Age” along with the age value
when an object of UnderAge class is printed in the catch statement. Write a class exceptionDemo in
which the method test() throws UnderAge exception if the variable age passed to it as argument is
less than 18. Write main() method also to show working of the program.

Code:
package java_practical;
import java.util.Scanner;
class UnderAge extends Exception{
private int age;
public UnderAge(int age) {
this.age=age;
}
@Override
public String toString() {
return "Under Age! "+age;
}

}
public class ExceptionDemo {

public static void test(int age_in) throws UnderAge{


if (age_in<18) {
throw new UnderAge(age_in);
}
else {
System.out.println("Not underAged!!");
}

}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter your age");
int a=sc.nextInt();
try {
test(a);
}
catch(UnderAge e) {
System.out.println(e.toString());
}
finally {

11
sc.close();
}

Output:

12
Practical-5

Q5) Write a program to implement stack. Use exception handling to manage underflow and
overflow conditions.

Code:

class Stack
{
int arr[]=new int[3];
int top=0;

public void push(int val)


{
if (top==3) {
System.out.println("Overflow! Stack is full");
}
else {
arr[top]=val;
top++;
}
}

public void pop() {


int val=0;
if (isEmpty()) {
System.out.println("Underflow! Stack is empty");

}
else {
top--;
val=arr[top];
arr[top]=0;
System.out.println(val);
}
}

public void display()


{
if (isEmpty()) {

System.out.println("Stack is empty, No elements to display.");


}

13
else{
System.out.print("Elemnts in stack :");
for (int e : arr) {
System.out.print(e+" ");
}
System.out.println();
}
}

public void peek(){


if (isEmpty()) {
System.out.println("Stack is empty, No element to peek");
}
else {
System.out.println("top element is:"+arr[top-1]);
}
}

public Boolean isEmpty(){


return top<=0;
}

}
public class Ques5
{
public static void main(String[] args) throws Exception {
Stack stk = new Stack();
stk.push(8);
stk.push(4);
stk.peek();
stk.push(5);
stk.display();
stk.push(12);
stk.pop();
stk.pop();
stk.peek();
stk.pop();
stk.pop();
stk.display();

}
}

Output:

14
15
Practical-6

Q6) Write a program that copies content of one file to another. Pass the names of the files
through command-line arguments.

Code:
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;

public class File_prac {

public static void main(String[] args) throws IOException{


File myFile = new File(args[0]);
FileWriter fileWriter = new FileWriter(args[1]);
try {
Scanner sc= new Scanner(myFile);
while(sc.hasNextLine()) {
String line = sc.nextLine();
try {
fileWriter.write(line+"\n");
}
catch(IOException e1) {
e1.printStackTrace();
}
}
sc.close();

16
}
catch(FileNotFoundException e){
System.out.println("unable to create the file");
e.printStackTrace();
}
finally {
fileWriter.close();
}
}
}

Output:

17
Practical-7

Q7) Write a program to read a file and display only those lines that have the first two
characters as '//' (Use try with resources).

Code:
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;

public class FileQues7 {

public static void main(String[] args)throws IOException {


File myFile = new File("file3.txt");
try(BufferedReader br = new BufferedReader(new FileReader(myFile))) {
String line;
while((line=br.readLine()) != null){
if (Character.toString(line.charAt(0)).equals("/")) {
System.out.println(line);
}
}
br.close();
}
catch (FileNotFoundException e) {
System.out.println("unable to read the file");
e.printStackTrace();
}

18
}

Output:

19
Practical-8

Q8) Write a program to create a frame using AWT. Implement mouseClicked(),


mouseEntered() and mouseExited() events such that:
a) Size of the frame should be tripled when mouse enters it.
b) Frame should reduce to its original size when mouse is clicked in it.
c) Close the frame when mouse exits it.

Code:
import java.awt.*;
import java.awt.event.*;

public class Pract8 extends Frame implements MouseListener{


Frame frame;
Label label;
public Pract8() {

setSize(new Dimension(200,200));
setLayout(null);
setVisible(true);

label = new Label();


label.setBackground(Color.black);
label.setForeground(Color.white);
label.setBounds(20,60,160,30);

add(label);

addMouseListener(this);
addWindowListener(new MyWindowAdapter());

//abstract methods in MouseListener interface


public void mousePressed(MouseEvent me) {
label.setText("mousePressed");
setSize(200,200);
}
public void mouseClicked(MouseEvent me) {
label.setText("mouseClicked");
setSize(600,600);
}
public void mouseReleased(MouseEvent me) {
label.setText("mouseReleased");

20
setSize(600,600);
}
public void mouseEntered(MouseEvent me) {
label.setText("mouseEntered");
setSize(600,600);
}
public void mouseExited(MouseEvent me) {
label.setText("mouseExited");
System.exit(0);
}

class MyWindowAdapter extends WindowAdapter{


public void windowClosing(WindowEvent we) {
System.exit(0);
}
}

public static void main(String[] args) {


new Pract8();
}

Output:

Before mouse is entered

21
After mouse is entered

22
Practical-9

Q9) Using AWT, write a program to display a string in frame window with pink color as
background.

Code:
import java.awt.*;
import java.awt.event.*;

public class Pract9 extends Frame{


Frame frame;
Label label;
String msg="Don't Settle";
public Pract9() {
setLayout(null);
setSize(400,400);
setVisible(true);
setBackground(Color.pink);

addWindowListener(new MyWindowAdapter());

class MyWindowAdapter extends WindowAdapter{


public void windowClosing(WindowEvent we) {
System.exit(0);
}
}

public void paint(Graphics g) {


g.setColor(Color.black);
g.drawString(msg, 30, 60);
}

public static void main(String[] args) {


Pract9 p=new Pract9();
p.setTitle("Practical-9");
}

Output:

23
24
Practical-10

Q10) Using AWT, write a program to create two buttons named “Red” and “Blue”. When a
button is pressed the background color should be set to the color named by the button’s
label.

Code:
import java.awt.*;
import java.awt.event.*;

public class Pract10 extends Frame implements ActionListener{


Frame frame;
Label label;
Button button1,button2;

public Pract10() {
setLayout(null);
setSize(400,400);
setVisible(true);

button1 = new Button("Red");


button1.setBounds(30,60,50,30);

button2 = new Button("Blue");


button2.setBounds(120,60,60,30);

add(button1);
add(button2);

button1.addActionListener(this);
button2.addActionListener(this);

25
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});

public void actionPerformed(ActionEvent ae) {


String str = ae.getActionCommand();
if (str.equals("Red"))
setBackground(Color.red);
else if (str.equals("Blue"))
setBackground(Color.blue);
}

public static void main(String[] args) {


Pract10 p=new Pract10();
p.setTitle("Practical-10");

Output:

26
This is default when red button is pressed when blue button is pressed

27
Practical-11

Q11) Using AWT, write a program using appropriate adapter class to display the message
(“Typed character is: ”) in the frame window when user types any key.

Code:
import java.awt.*;
import java.awt.event.*;

public class Pract11 extends Frame{


Frame frame;
Label label1,label2;
String msg="Typed character is: ";

public Pract11() {
setLayout(null);
setSize(300,300);
setVisible(true);

label1 = new Label();


label1.setBackground(Color.black);
label1.setForeground(Color.white);
label1.setBounds(140,50,20,30);
add(label1);

addKeyListener(new MyKeyAdapter());

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we) {

28
System.exit(0);
}
});

public void paint(Graphics g) {


g.drawString(msg, 30, 70);
}

class MyKeyAdapter extends KeyAdapter{


public void keyPressed(KeyEvent ke) {
char c=ke.getKeyChar();
label1.setText(Character.toString(c));
}
}
class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
public static void main(String[] args) {
Pract11 p=new Pract11();
p.setTitle("Practical-11");

29
Output:

When key ‘p’ is pressed

30
Practical-12

Q12) Using AWT, write a program to create two buttons labelled ‘A’ and ‘B’. When button
‘A’ is pressed, it displays your personal information (Name, Course, Roll No, College) and
when button ‘B’ is pressed, it displays your CGPA in previous semester.

Code:
import java.awt.*;
import java.awt.event.*;

public class Pract12 extends Frame implements ActionListener{


Frame frame;
Label name,course,rollno,college,sgpa;
Button buttonA,buttonB;

public Pract12() {
setLayout(null);
setSize(300,300);
setVisible(true);

buttonA = new Button("A");


buttonA.setBounds(30,60,30,30);

buttonB = new Button("B");


buttonB.setBounds(90,60,30,30);

name = new Label("Nitika Kumari");


name.setBounds(30,100,80,30);
name.setBackground(Color.gray);
name.setForeground(Color.white);

31
name.setVisible(false);

course = new Label("BSc.CS(hons)");


course.setBounds(30,140,120,30);
course.setBackground(Color.gray);
course.setForeground(Color.white);
course.setVisible(false);

rollno = new Label("CSC/20/7");


rollno.setBounds(30,180,60,30);
rollno.setBackground(Color.gray);
rollno.setForeground(Color.white);
rollno.setVisible(false);

college = new Label("Aryabhatta College");


college.setBounds(30,220,120,30);
college.setBackground(Color.gray);
college.setForeground(Color.white);
college.setVisible(false);

sgpa = new Label("9.82 :)");


sgpa.setBounds(30,100,40,30);
sgpa.setBackground(Color.darkGray);
sgpa.setForeground(Color.white);
sgpa.setVisible(false);

add(buttonA);
add(buttonB);
add(name);

32
add(course);
add(rollno);
add(college);
add(sgpa);

buttonA.addActionListener(this);
buttonB.addActionListener(this);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});

public void actionPerformed(ActionEvent ae) {


String str = ae.getActionCommand();
if (str.equals("A")) {
setBackground(Color.gray);
name.setVisible(true);
course.setVisible(true);
rollno.setVisible(true);
college.setVisible(true);
sgpa.setVisible(false);
}
else if (str.equals("B")) {
setBackground(Color.darkGray);
name.setVisible(false);

33
course.setVisible(false);
rollno.setVisible(false);
college.setVisible(false);
sgpa.setVisible(true);
}
}

public static void main(String[] args) {


Pract12 p=new Pract12();
p.setTitle("Practical-12");

Output:

This is default when button A is pressed when button B is pressed

Practical-13

34
Q12) Rewrite all the above GUI programs using Swing.

a) Write a program to create a frame using AWT. Implement mouseClicked(),


mouseEntered() and mouseExited() events such that:
i) Size of the frame should be tripled when mouse enters it.
ii) Frame should reduce to its original size when mouse is clicked in it.
iii) Close the frame when mouse exits it.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Pract13a {


JLabel jlabel;
JFrame jframe;

public Pract13a(){
jframe=new JFrame("Practical-13(a)");
jframe.setSize(200,200);
jframe.setLayout(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jlabel=new JLabel();
jlabel.setBackground(Color.black);
jlabel.setForeground(Color.white);
jlabel.setOpaque(true);
jlabel.setBounds(20,60,160,30);
jframe.add(jlabel);

jframe.addMouseListener(new MyMouseListener());
jframe.setVisible(true);
}

class MyMouseListener implements MouseListener {


public void mouseClicked(MouseEvent e) {
jlabel.setText("mouseClicked");
jframe.setSize(600,600);
}
public void mousePressed(MouseEvent e) {
jlabel.setText("mousePressed");
jframe.setSize(200,200);
}
public void mouseReleased(MouseEvent e) {
35
jlabel.setText("mouseReleased");
jframe.setSize(600,600);
}
public void mouseEntered(MouseEvent e) {
jlabel.setText("mouseEntered");
jframe.setSize(600,600);
}
public void mouseExited(MouseEvent e) {
jlabel.setText("mouseExited");
System.exit(0);
}
}

public static void main(String[] args) {


new Pract13a();
}
}

Output:

Before mouse is entered

36
After mouse is entered

b) Using AWT, write a program to display a string in frame window with pink color as
background.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Pract13b {


JLabel jlabel;
JFrame jframe;

public Pract13b(){
jframe=new JFrame("Practical-13(b)");
jframe.setSize(400,400);
jframe.setLayout(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setBackground(Color.pink);
jframe.getContentPane().setBackground(Color.pink);

jlabel=new JLabel("Don't Settle");

37
jlabel.setBounds(30,60,100,30);
jframe.add(jlabel);

jframe.setVisible(true);
}

public static void main(String[] args) {


new Pract13b();
}
}

Output:

c) Using AWT, write a program to create two buttons named “Red” and “Blue”. When a
button is pressed the background color should be set to the color named by the button’s
label.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

38
public class Pract13c {
JFrame jframe;

public Pract13c() {
jframe = new JFrame("Practical-13(c)");
jframe.setSize(400, 400);
jframe.setLayout(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JButton button1=new JButton("Red");


button1.setBounds(30,60,70,30);
jframe.add(button1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jframe.getContentPane().setBackground(Color.red);
}
});

JButton button2=new JButton("Blue");


button2.setBounds(150,60,60,30);
jframe.add(button2);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jframe.getContentPane().setBackground(Color.blue);
}
});

jframe.setVisible(true);
}
public static void main(String[] args) {
new Pract13c();
}

Output:

39
This is default when red button is pressed when blue button is pressed

d) Using AWT, write a program using appropriate adapter class to display the message
(“Typed character is: ”) in the frame window when user types any key.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Pract13d {


JFrame jframe;
JLabel jlabel1,jlabel2;
public Pract13d() {
jframe = new JFrame("Practical-13(d)");
jframe.setSize(300, 300);
jframe.setLayout(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jlabel1=new JLabel("Type Character is: ");


jlabel1.setBounds(20,70,120,30);
jframe.add(jlabel1);

jlabel2=new JLabel();
jlabel2.setBackground(Color.black);
jlabel2.setForeground(Color.white);
jlabel2.setOpaque(true);
jlabel2.setBounds(140,70,20,30);
jframe.add(jlabel2);

jframe.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {

40
char c=ke.getKeyChar();
jlabel2.setText(Character.toString(c));
}
});
jframe.setVisible(true);
}
public static void main(String[] args) {
new Pract13d();
}
}

Output:

When key ‘n’ is pressed

e) Using AWT, write a program to create two buttons labelled ‘A’ and ‘B’. When button ‘A’ is
pressed, it displays your personal information (Name, Course, Roll No, College) and when
button ‘B’ is pressed, it displays your CGPA in previous semester.

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

41
public class Pract13e {
JFrame jframe;
JLabel name,course,rollno,college,sgpa;
JButton buttonA,buttonB;

public Pract13e() {
jframe = new JFrame("Practical-13(e)");
jframe.setSize(300, 300);
jframe.setLayout(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

name = new JLabel("Nitika Kumari");


name.setBounds(30,70,80,30);
name.setBackground(Color.gray);
name.setForeground(Color.white);
name.setOpaque(true);
name.setVisible(false);
jframe.add(name);

course = new JLabel("BSc.CS(hons)");


course.setBounds(30,110,120,30);
course.setBackground(Color.gray);
course.setForeground(Color.white);
course.setOpaque(true);
course.setVisible(false);
jframe.add(course);

rollno = new JLabel("CSC/20/7");


rollno.setBounds(30,150,60,30);
rollno.setBackground(Color.gray);
rollno.setForeground(Color.white);
rollno.setOpaque(true);
rollno.setVisible(false);
jframe.add(rollno);

college = new JLabel("Aryabhatta College");


college.setBounds(30,190,120,30);
college.setBackground(Color.gray);
college.setForeground(Color.white);
college.setOpaque(true);
college.setVisible(false);
jframe.add(college);

sgpa = new JLabel("9.82 :)");


sgpa.setBounds(30,70,40,30);
sgpa.setBackground(Color.darkGray);
sgpa.setForeground(Color.white);

42
sgpa.setOpaque(true);
sgpa.setVisible(false);
jframe.add(sgpa);

JButton buttonA=new JButton("A");


buttonA.setBounds(30,30,50,30);
jframe.add(buttonA);

buttonA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jframe.getContentPane().setBackground(Color.gray);
name.setVisible(true);
course.setVisible(true);
rollno.setVisible(true);
college.setVisible(true);
sgpa.setVisible(false);
}
});

JButton buttonB=new JButton("B");


buttonB.setBounds(150,30,50,30);
jframe.add(buttonB);
buttonB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jframe.getContentPane().setBackground(Color.darkGray);
name.setVisible(false);
course.setVisible(false);
rollno.setVisible(false);
college.setVisible(false);
sgpa.setVisible(true);
}
});

jframe.setVisible(true);
}
public static void main(String[] args) {
new Pract13e();
}

Output:

43
This is default when button A is pressed when button B is pressed

44

You might also like