Hand Coded JFrame, Java Frame Example

Hand Coded JFrame, Java Frame Example
Create Java frame application manually, without using drag drop

1. Import Libraries

javax swing for frames and interface:

 

java AWT color to use colors in frames:

 

java AWT event for handling button events:

 

2. extend class from JFrame:

 

3. constructor of class:

 

4. initialize components and make form visible:

 

/* a private helper method to set up the components on the JFrame */

5. Set the size of JFrame, Java frame:

 

6. set background of JFrame Java Frame:

 

7. Set background color of button (myButton) to white and foreground color to black:

 

8. Set background color of button (colorButton) to white and foreground color to black:

 

9. Set size of button (myButton) and add to content pane:

 

10. Set size of button (colorButton) and add to content pane:

 

11. Add Listeners to buttons:

 

12. Create a private class MyListener implementing Action Listener:

 

13. Implement button handler event method and passing instance of ActionEvent in parameters of both buttons:

 

14. Implement main method:

 

15. Initialize buttons:

Complete Code:

Screen Shots for custom JFrame application:

Custom JFrame:

JFrame, Java Frame Application
JFrame, Java Frame Application

Change Background color of frame using Button:

change background color JFrame
change background color JFrame

Show Dialog Box using Button:

Show dialog box using button JFrame
Show dialog box using button JFrame

Download java file here.

Alternate Link

 

Hope you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!

Don’t forget to give your valuable feedback through comments!

Leave a Reply

Your email address will not be published. Required fields are marked *