Search code examples
javaswingjpanelactionlistenerjtextfield

Color is not applying in JPanel?


I am developing Java Swing app. In my application I used JPanels and put white color to the background but it is not working properly. Cant guess why? And Same issue on JPanel border. I tried several times but failed. Evan I am developing in netbeans its not working. Can you help me?

public class RegistrationForm extends JFrame {


    public RegistrationForm() {

         initComponents();
         pack();
         setLocationRelativeTo(null);
         this.setVisible(true);

    }   

    private void initComponents() {

        setUndecorated(true);
        jCheckBoxMenuItem1 = new JCheckBoxMenuItem();
        jPanel1 = new JPanel();
        headingLabel = new javax.swing.JLabel();
        conditionTextBox = new javax.swing.JCheckBox();
        joinButton = new javax.swing.JButton();
        firstNameField = new PlaceholderTextField();
        emailField = new PlaceholderTextField();
        userNameField = new PlaceholderTextField();
        passWordField = new PlaceholderPasswordField();
        confirmPassWordField = new PlaceholderPasswordField();
        jButton1 = new javax.swing.JButton();





        jCheckBoxMenuItem1.setSelected(true);
        jCheckBoxMenuItem1.setText("jCheckBoxMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setBackground(Color.WHITE);
        jPanel1.setLayout(new java.awt.GridBagLayout());

        headingLabel.setFont(new java.awt.Font("Tahoma", 1, 24));
        headingLabel.setText("Join Us Now");

        conditionTextBox.setFont(new java.awt.Font("Segoe UI", 0, 11)); 
        conditionTextBox.setText("I agree to the UAC system terms of service and regulations ");


        joinButton.setFont(new java.awt.Font("Segoe UI", 0, 14)); 
        joinButton.setText("Join");
        joinButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                joinButtonActionPerformed(evt);
            }
        });

        firstNameField.setFont(new java.awt.Font("Segoe UI", 0, 18)); 


        emailField.setFont(new java.awt.Font("Segoe UI", 0, 18)); 


        userNameField.setFont(new java.awt.Font("Segoe UI", 0, 18)); 
        userNameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userNameFieldActionPerformed(evt);
            }
        });

        passWordField.setFont(new java.awt.Font("Segoe UI", 0, 18)); 

        confirmPassWordField.setFont(new java.awt.Font("Segoe UI", 0, 18)); 


        jButton1.setActionCommand("closeRegistretion");

       //then adding components                   


    // Variables declaration - do not modify                     
    private javax.swing.JCheckBox conditionTextBox;
    private PlaceholderPasswordField confirmPassWordField;
    private PlaceholderTextField emailField;
    private PlaceholderTextField firstNameField;
    private javax.swing.JLabel headingLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton joinButton;
    private PlaceholderPasswordField passWordField;
    private PlaceholderTextField userNameField;
    // End of variables declaration                   
}

Solution

  • Use this code,In this code we make a panel with background color:-

    import java.awt.Color;
    import javax.swing.JPanel;
    
    public class NewClass1 extends javax.swing.JFrame 
    {
        public NewClass1() {            
             setLayout(null);
             JPanel panel=new JPanel();
             panel.setOpaque(true);
             add(panel);
             panel.setBackground(Color.red);
             panel.setBounds(0,0,400,300);
        }    
    
        public static void main(String args[]) {
    
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NewClass1().setVisible(true);
                }
            });
        }
    }