I have problems with my GUI part...

I have put all components on the JFrame but I would like to add some backgrounds and decoration.

However, when I add background using JLabel. Everything loses its position and they are all ruined...

Is there anyway to resolve this?

Recommended Answers

All 6 Replies

Adding labels with different background colors to a JFrame is not a good idea (if that's what you did). If you did so, better use JLayeredPane component. You add in it all your components instead of adding them directly to the JFrame. Then you add your labels in a layer before.

How to move components to JLayeredPane? Here is my code inside class that extends JFrame.

    public MainJFrame() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            /*
             * for (javax.swing.UIManager.LookAndFeelInfo info :
             * javax.swing.UIManager.getInstalledLookAndFeels()) { if
             * ("Nimbus".equals(info.getName())) {
             * javax.swing.UIManager.setLookAndFeel(info.getClassName());
             * UIManager. break; } }
             */
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(
                    java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(
                    java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(
                    java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(
                    java.util.logging.Level.SEVERE, null, ex);
        }
        // </editor-fold>

        initComponents();
        setAction();
        this.setFocusable(true);
        this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                showFrame();
            }
        });

        addBindings();
    }

    /**
     * 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() {

        lp = this.getLayeredPane();
        createBG();

        jLabel1 = new javax.swing.JLabel("", Resource.bigLogo,
                SwingConstants.CENTER);
        jLabel2 = new javax.swing.JLabel("", Resource.exitImg,
                SwingConstants.CENTER);
        jLabel3 = new javax.swing.JLabel("", Resource.down,
                SwingConstants.CENTER);

        jPanel1 = new javax.swing.JPanel();
        jPanel1.setOpaque(false);
        jComboBox1 = new javax.swing.JComboBox();

        setPreferredSize(new java.awt.Dimension(378, 300));

        jComboBox1.setEditable(true);
        jComboBox1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
                jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout
                .setHorizontalGroup(jPanel1Layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                jPanel1Layout
                                        .createSequentialGroup()
                                        .addContainerGap()
                                        .addComponent(
                                                jLabel1,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                56,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(
                                                                jPanel1Layout
                                                                        .createSequentialGroup()
                                                                        .addGap(0,
                                                                                0,
                                                                                Short.MAX_VALUE)
                                                                        .addComponent(
                                                                                jLabel2))
                                                        .addGroup(
                                                                jPanel1Layout
                                                                        .createSequentialGroup()
                                                                        .addComponent(
                                                                                jComboBox1,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                291,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                                                7,
                                                                                Short.MAX_VALUE)
                                                                        .addComponent(
                                                                                jLabel3,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                22,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addContainerGap()));
        jPanel1Layout
                .setVerticalGroup(jPanel1Layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                jPanel1Layout
                                        .createSequentialGroup()
                                        .addGroup(
                                                jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(
                                                                jPanel1Layout
                                                                        .createSequentialGroup()
                                                                        .addComponent(
                                                                                jLabel2,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                25,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addGap(18,
                                                                                18,
                                                                                18)
                                                                        .addGroup(
                                                                                jPanel1Layout
                                                                                        .createParallelGroup(
                                                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                                        .addComponent(
                                                                                                jComboBox1,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                29,
                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                        .addComponent(
                                                                                                jLabel3,
                                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                                Short.MAX_VALUE))
                                                                        .addGap(0,
                                                                                11,
                                                                                Short.MAX_VALUE))
                                                        .addGroup(
                                                                jPanel1Layout
                                                                        .createSequentialGroup()
                                                                        .addContainerGap()
                                                                        .addComponent(
                                                                                jLabel1,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                Short.MAX_VALUE)))
                                        .addContainerGap()));

        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.PREFERRED_SIZE,
                javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE));

        setIconImage((Resource.bigLogo).getImage());
        setUndecorated(true);
        setSize(400, 100);
        // pack();

    }// </editor-fold>

    @Override
    public void paint(Graphics g){
        super.paint(g);

        //this.getContentPane().setBackground(new Color(233, 239, 246));
    }

    private void createBG() {
        logger.debug("--->enter createBG function");

        //this.getContentPane().setBackground(Color.yellow);

        bg = new JLabel();
        bg.setIcon(Resource.backgroundLogo);

        lp.setSize(400, 400);
        lp.add(bg, 1);

    }

Moreover, why doesn't the background show?
Thanks :)

I have tried new method by overriding paint function.
However, after overriding. All components become invisible but if they gain focus, they will appear again...

How to solve this problem?

    public void paint(Graphics g){
        logger.debug("start drawing");
        //super.paint(g);
        g.drawImage(Resource.backgroundImage, 0, 0, null);

        //this.getContentPane().setBackground(new Color(233, 239, 246));
    }

paint for a JFrame also paints the borders and the children of the frame. By overriding it and not calling super.paint you have supressed JFrame's painting those objects.
If you want to have custom painting the best option is a new subclass of JPanel and override its paintComponent method, thus avoiding your current problems.

But after adding JPanel which overrides paint,
my current JPanel which stores my components will disappear. ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.