Java Program to Display a Message in a New Frame

This is a Java Program to Display a Message in a New Frame

Problem Description

We have to write a program in Java such that it creates a frame with a button. When the button is clicked, a message is displayed in a new frame.

Expected Input and Output

For displaying a message in a new frame, we can have the following different sets of input and output.

1. To View the Original Frame :

On every execution of the program, 
it is expected that a frame with a button is created.

2. To View the Message in a New Frame :

On click of the button, 
it is expected that "!!! Hello !!!" message is displayed in a new frame.
Problem Solution

1. Create the original frame and add a button to it.
2. Add ActionListener to the button.
3. Display the original frame.
4. When the button is clicked, create a new frame and add a message to the frame.
5. Display the new frame.

Program/Source Code

Here is source code of the Java Program to display a message in a new frame. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1. /* Java Program to Display a Message in a New Frame */
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.awt.*;
  5. class Message implements ActionListener
  6. {
  7.     //Function to create the original frame
  8.     public static void main(String args[])
  9.     {
  10. 	//Create a frame
  11. 	JFrame frame = new JFrame("Original Frame");
  12. 	frame.setSize(300,300);
  13. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. 	//Create an object
  16. 	Message obj = new Message();
  18. 	//Create a button to view message
  19. 	JButton button = new JButton("View Message");
  20. 	frame.add(button);
  21. 	button.addActionListener(obj);
  23. 	//View the frame
  24. 	frame.setVisible(true);
  25.     }
  26.     //Function to create a new frame with message
  27.     public void actionPerformed(ActionEvent e)
  28.     {
  29. 	//Create a new frame
  30. 	JFrame sub_frame = new JFrame("Sub Frame");
  31. 	sub_frame.setSize(200,200);
  33. 	//Display the message
  34. 	JLabel label = new JLabel("!!! Hello !!!");
  35. 	sub_frame.add(label);
  37. 	//View the new frame
  38. 	sub_frame.setVisible(true);
  39.     }
  40. }
Program Explanation

1. To create a frame, use the JFrame class.
2. To create a button, use the JButton class.
3. Add ActionListener to the button.

Runtime Test Cases

Here’s the run time test cases to display message a in new frame for different input cases.


Test case 1 – To View the Original Frame.

Test case 2 – To View the Message in New Frame.

Sanfoundry Global Education & Learning Series – Java Programs.


If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.