user1413188
user1413188

Reputation: 101

Adding JPanels to the JApplet with a null layout

I want to add 4 JPanels to the JApplet I have, and I give to each one of them a different color. But none of the colors are shown - I mean I can’t see the output. No colors at all. The following code is in the init() method.

  this.setSize(1400, 780);
      this.setVisible(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setLocationRelativeTo(null);
  this.setLayout(null) ; 

      setLayout(null) ; 

  Panel1 = new JPanel() ;
  Panel2 = new JPanel () ; 
  Panel3 = new JPanel() ; 
  Panel4 = new JPanel() ; 

  Label1 = new JLabel ("Label1") ; 
  Label2 = new JLabel ("Label2") ; 
  Label3 = new JLabel ("Label3") ; 
  Label4 = new JLabel ("Label4") ; 

  Panel1.add(Label1) ; 
  Panel2.add(Label2) ; 
  Panel3.add(Label3) ; 
  Panel4.add(Label4) ; 

  // Panel 1 "About Me"
  Panel1.setSize(140,390) ; 
  Panel1.setLocation(0,0) ; 
  Panel1.setBackground(Color.red) ; 
  Panel1.setVisible(true) ; 
  this.add(Panel1) ; 

  // Panel 2 "MyHoppies" 
  Panel2.setSize(140,390) ; 
  Panel2.setLocation(0,700) ; 
  Panel2.setBackground(Color.yellow) ;
  this.add(Panel2) ; 

  // Panel 3 "Photo Gallery"
  Panel3.setSize(140,390) ; 
  Panel3.setLocation(390,0) ; 
  Panel3.setBackground(Color.black) ;
  this.add(Panel3) ;

  // Panel 4 "Happey face" 
  Panel4.setSize(140,390) ;
  Panel4.setLocation(390,700) ; 
  Panel4.setBackground(Color.pink) ; 
  this.add(Panel4) ; 

Upvotes: 4

Views: 1546

Answers (3)

mKorbel
mKorbel

Reputation: 109823

  • this.setVisible(true) must be last code line in the GUI constructor

  • use Using Java Naming Conventions correctly then Panel1 should be panel1 e.i.

  • don't extends JFrame or JApplet, create that as local variable with the same way as for Panel1

  • don't use NullLayout, use proper LayoutManager instead, in this case GridLayout probably, otherwise JFrames contents isn't resiziable with JFrame

enter image description here

code

import java.awt.*;
import javax.swing.*;

public class ColorongPanels {

    private JFrame frame = new JFrame("ColorongPanels");
    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JPanel panel3 = new JPanel();
    private JPanel panel4 = new JPanel();
    private JLabel label1 = new JLabel("Label1");
    private JLabel label2 = new JLabel("Label2");
    private JLabel label3 = new JLabel("Label3");
    private JLabel label4 = new JLabel("Label4");

    public ColorongPanels() {
        frame.setLayout(new GridLayout(2, 2, 5, 5));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel1.setBackground(Color.red);
        panel1.setLayout(new BorderLayout());
        panel1.add(label1, BorderLayout.CENTER);
        panel2.setBackground(Color.yellow);
        panel2.setLayout(new BorderLayout());
        panel2.add(label2, BorderLayout.CENTER);
        panel3.setBackground(Color.black);
        panel3.setLayout(new BorderLayout());
        panel3.add(label3, BorderLayout.CENTER);
        panel4.setBackground(Color.pink);
        panel4.setLayout(new BorderLayout());
        panel4.add(label4, BorderLayout.CENTER);
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);
        frame.add(panel4);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ColorongPanels();
            }
        });
    }
}

Upvotes: 6

brimborium
brimborium

Reputation: 9522

First: You need to JPanel.setOpaque(true) the panels in order to see the background colors.

Second: The background color property has different effects on different platforms. For instance: If you set the background of a JButton, you will see the button colored in Win7, but not in WinXP (not sure, how it is under other operating systems). That is at least my experience...

Upvotes: 2

Joseph Elcid
Joseph Elcid

Reputation: 877

You should do this:
panelx.setOpaque(true) and it should work

Upvotes: 3

Related Questions