Java Swing Jframe Design Using JPanels

How To Design a Form With Panels Using Java NetBeans

java form design with panels


In a Previous Java Design Tutorial We Created a Beautiful Java Login and Register Form .
And In this Java Tutorial we will see How To Create a Simple JFrame Design With JPanels and JLabels In Java NetBeans .



WATCH THE VIDEO TUTORIAL 

Project Source Code:

package javatutorials;

import java.awt.Color;

/**
 * @author 1BestCsharp.blogspot.com
 **/
public class Design_Form_With_JPanels extends javax.swing.JFrame {

    /**
     * Creates new form Design_Form_With_JPanels
     */
    
    // get jpanels background colors
    Color pTopColor;
    Color pCenterColor;
    Color pLeftColor;
    Color pRightColor;
    
    public Design_Form_With_JPanels() {
        initComponents();
        
        // set colors
        pTopColor = jPanel_top.getBackground();
        pCenterColor = jPanel_center.getBackground();
        pLeftColor = jPanel_left.getBackground();
        pRightColor = jPanel_right.getBackground();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel_top = new javax.swing.JPanel();
        jLabel_top = new javax.swing.JLabel();
        jPanel_center = new javax.swing.JPanel();
        jLabel_center = new javax.swing.JLabel();
        jPanel_left = new javax.swing.JPanel();
        jLabel_left = new javax.swing.JLabel();
        jPanel_right = new javax.swing.JPanel();
        jLabel_right = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));

        jPanel_top.setBackground(new java.awt.Color(46, 204, 113));
        jPanel_top.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jPanel_topMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jPanel_topMouseExited(evt);
            }
        });

        jLabel_top.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
        jLabel_top.setForeground(new java.awt.Color(255, 255, 255));
        jLabel_top.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_top.setText("TOP");

        javax.swing.GroupLayout jPanel_topLayout = new javax.swing.GroupLayout(jPanel_top);
        jPanel_top.setLayout(jPanel_topLayout);
        jPanel_topLayout.setHorizontalGroup(
            jPanel_topLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel_top, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel_topLayout.setVerticalGroup(
            jPanel_topLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_topLayout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(jLabel_top)
                .addContainerGap(46, Short.MAX_VALUE))
        );

        jPanel_center.setBackground(new java.awt.Color(234, 181, 67));
        jPanel_center.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jPanel_centerMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jPanel_centerMouseExited(evt);
            }
        });

        jLabel_center.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
        jLabel_center.setForeground(new java.awt.Color(255, 255, 255));
        jLabel_center.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_center.setText("CENTER");

        javax.swing.GroupLayout jPanel_centerLayout = new javax.swing.GroupLayout(jPanel_center);
        jPanel_center.setLayout(jPanel_centerLayout);
        jPanel_centerLayout.setHorizontalGroup(
            jPanel_centerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel_center, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel_centerLayout.setVerticalGroup(
            jPanel_centerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_centerLayout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(jLabel_center)
                .addContainerGap(46, Short.MAX_VALUE))
        );

        jPanel_left.setBackground(new java.awt.Color(130, 88, 159));
        jPanel_left.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jPanel_leftMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jPanel_leftMouseExited(evt);
            }
        });

        jLabel_left.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
        jLabel_left.setForeground(new java.awt.Color(255, 255, 255));
        jLabel_left.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_left.setText("LEFT");

        javax.swing.GroupLayout jPanel_leftLayout = new javax.swing.GroupLayout(jPanel_left);
        jPanel_left.setLayout(jPanel_leftLayout);
        jPanel_leftLayout.setHorizontalGroup(
            jPanel_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel_left, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
        );
        jPanel_leftLayout.setVerticalGroup(
            jPanel_leftLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_leftLayout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(jLabel_left)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel_right.setBackground(new java.awt.Color(194, 54, 22));
        jPanel_right.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jPanel_rightMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jPanel_rightMouseExited(evt);
            }
        });

        jLabel_right.setFont(new java.awt.Font("Arial", 0, 24)); // NOI18N
        jLabel_right.setForeground(new java.awt.Color(255, 255, 255));
        jLabel_right.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel_right.setText("RIGHT");

        javax.swing.GroupLayout jPanel_rightLayout = new javax.swing.GroupLayout(jPanel_right);
        jPanel_right.setLayout(jPanel_rightLayout);
        jPanel_rightLayout.setHorizontalGroup(
            jPanel_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel_right, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
        );
        jPanel_rightLayout.setVerticalGroup(
            jPanel_rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_rightLayout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(jLabel_right)
                .addContainerGap(44, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel_top, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel_center, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel_left, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel_right, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel_top, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel_center, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel_left, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel_right, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void jPanel_topMouseEntered(java.awt.event.MouseEvent evt) {                                        
        
        jPanel_top.setBackground(Color.WHITE);
        jLabel_top.setForeground(pTopColor);
        
    }                                       

    private void jPanel_topMouseExited(java.awt.event.MouseEvent evt) {                                       
        
        jPanel_top.setBackground(pTopColor);
        jLabel_top.setForeground(Color.WHITE);
        
    }                                      

    private void jPanel_centerMouseEntered(java.awt.event.MouseEvent evt) {                                           
        jPanel_center.setBackground(Color.WHITE);
        jLabel_center.setForeground(pCenterColor);
    }                                          

    private void jPanel_centerMouseExited(java.awt.event.MouseEvent evt) {                                          
        jPanel_center.setBackground(pCenterColor);
        jLabel_center.setForeground(Color.WHITE);
    }                                         

    private void jPanel_leftMouseEntered(java.awt.event.MouseEvent evt) {                                         
        jPanel_left.setBackground(Color.WHITE);
        jLabel_left.setForeground(pLeftColor);
    }                                        

    private void jPanel_leftMouseExited(java.awt.event.MouseEvent evt) {                                        
        jPanel_left.setBackground(pLeftColor);
        jLabel_left.setForeground(Color.WHITE);
    }                                       

    private void jPanel_rightMouseEntered(java.awt.event.MouseEvent evt) {                                          
        jPanel_right.setBackground(Color.WHITE);
        jLabel_right.setForeground(pRightColor);
    }                                         

    private void jPanel_rightMouseExited(java.awt.event.MouseEvent evt) {                                         
        jPanel_right.setBackground(pRightColor);
        jLabel_right.setForeground(Color.WHITE);
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Design_Form_With_JPanels.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Design_Form_With_JPanels.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Design_Form_With_JPanels.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Design_Form_With_JPanels.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Design_Form_With_JPanels().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel_center;
    private javax.swing.JLabel jLabel_left;
    private javax.swing.JLabel jLabel_right;
    private javax.swing.JLabel jLabel_top;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel_center;
    private javax.swing.JPanel jPanel_left;
    private javax.swing.JPanel jPanel_right;
    private javax.swing.JPanel jPanel_top;
    // End of variables declaration                   
}


OutPut:

Java Jframe Design Using JPanels



Share this

Related Posts

Previous
Next Post »