Hand Coded JFrame, Java Frame Example
Create Java frame application manually, without using drag drop
1. Import Libraries
javax swing for frames and interface:
1 |
import javax.swing.*; |
java AWT color to use colors in frames:
1 |
import java.awt.Color; |
java AWT event for handling button events:
1 |
import java.awt.event.*; |
2. extend class from JFrame:
1 |
public class JavaFrame extends JFrame{ |
3. constructor of class:
1 |
public JavaFrame() |
4. initialize components and make form visible:
1 2 3 4 5 |
{ initComponent(); setVisible(true); } private void initComponent(){ |
/* a private helper method to set up the components on the JFrame */
5. Set the size of JFrame, Java frame:
1 |
setSize(500,250); |
6. set background of JFrame Java Frame:
1 2 |
getContentPane().setBackground(Color.blue); getContentPane().setLayout(null); |
7. Set background color of button (myButton) to white and foreground color to black:
1 2 |
myButton.setBackground(Color.white); myButton.setForeground(Color.black); |
8. Set background color of button (colorButton) to white and foreground color to black:
1 2 |
colorButton.setBackground(Color.white); colorButton.setForeground(Color.black); |
9. Set size of button (myButton) and add to content pane:
1 2 |
getContentPane().add(myButton); myButton.setBounds(150,50, 200, 50); |
10. Set size of button (colorButton) and add to content pane:
1 2 |
getContentPane().add(colorButton); colorButton.setBounds(150, 105, 200, 50); |
11. Add Listeners to buttons:
1 2 3 4 |
MyListener l = new MyListener(); myButton.addActionListener(l);; colorButton.addActionListener(l); } |
12. Create a private class MyListener implementing Action Listener:
1 2 3 4 5 6 7 8 9 |
private class MyListener implements ActionListener{ public void actionPerformed(ActionEvent event){ Object o = event.getSource(); if(o==myButton){ myButtonHandler(event);//add handler event to myButton }else{ colorButtonHandler(event);//add handler event to color } } |
13. Implement button handler event method and passing instance of ActionEvent in parameters of both buttons:
1 2 3 4 5 6 7 8 9 10 11 |
private void myButtonHandler(ActionEvent e){ JOptionPane.showMessageDialog(null, "Messege Dialog"); } private void colorButtonHandler(ActionEvent e){ Color c = getContentPane().getBackground(); if(c==Color.blue) getContentPane().setBackground(Color.GREEN); else getContentPane().setBackground(Color.blue); } } |
14. Implement main method:
1 2 3 |
public static void main(String args[])throws Exception{ new JavaFrame(); } |
15. Initialize buttons:
1 2 3 4 |
private JButton myButton = new JButton("open Message Dialog"); private JButton colorButton = new JButton("Change Background"); } |
Complete Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import javax.swing.*; import java.awt.Color; import java.awt.event.*; public class JavaFrame extends JFrame{ public JavaFrame(){ initComponent(); setVisible(true); } private void initComponent(){ /* a private helper method to set up the components on the JFrame */ setSize(500,250); getContentPane().setBackground(Color.blue); getContentPane().setLayout(null); myButton.setBackground(Color.white); myButton.setForeground(Color.black); colorButton.setBackground(Color.white); colorButton.setForeground(Color.black); getContentPane().add(myButton); myButton.setBounds(150,50, 200, 50); getContentPane().add(colorButton); colorButton.setBounds(150, 105, 200, 50); MyListener l = new MyListener(); myButton.addActionListener(l);; colorButton.addActionListener(l); } private class MyListener implements ActionListener{ public void actionPerformed(ActionEvent event){ Object o = event.getSource(); if(o==myButton){ myButtonHandler(event); }else{ colorButtonHandler(event); } } private void myButtonHandler(ActionEvent e){ JOptionPane.showMessageDialog(null, "Messege Dialog"); } private void colorButtonHandler(ActionEvent e){ Color c = getContentPane().getBackground(); if(c==Color.blue) getContentPane().setBackground(Color.GREEN); else getContentPane().setBackground(Color.blue); } } public static void main(String args[])throws Exception{ new JavaFrame(); } private JButton myButton = new JButton("open Message Dialog"); private JButton colorButton = new JButton("Change Background"); } |
Screen Shots for custom JFrame application:
Custom JFrame:

Change Background color of frame using Button:

Show Dialog Box using Button:

Download java file here.
Hope you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!
Don’t forget to give your valuable feedback through comments!