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.

advertisement

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.

advertisement
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.

  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);
  14.  
  15. 	//Create an object
  16. 	Message obj = new Message();
  17.  
  18. 	//Create a button to view message
  19. 	JButton button = new JButton("View Message");
  20. 	frame.add(button);
  21. 	button.addActionListener(obj);
  22.  
  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);
  32.  
  33. 	//Display the message
  34. 	JLabel label = new JLabel("!!! Hello !!!");
  35. 	sub_frame.add(label);
  36.  
  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.

advertisement
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.
java-program-message-new-frame-original

Test case 2 – To View the Message in New Frame.
java-program-message-new-frame

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn