I am trying to figure out how to center the labels labeled blackSection, blueSection and so on. However i am unable to get the labels to center. I have tried blackSection.setVerticalAlignment(JLabel.CENTER) also have tried blackSection.setVerticalTextProperty(JLabel.CENTER). Any help would be appreciated, it is however a homework assignment so hints as to what to do would be preferred as opposed to just an answer.

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

public class Rainbows extends JFrame
{
   private JPanel blackPanel;
   private JPanel bluePanel;
   private JPanel cyanPanel;
   private JPanel greenPanel;
   private JPanel magentaPanel;
   private JPanel orangePanel;
   private JLabel blackSection;
   private JLabel blueSection;
   private JLabel cyanSection;
   private JLabel greenSection;
   private JLabel magentaSection;
   private JLabel orangeSection;
   private final int winWidth = 400;
   private final int winHeight = 400;

   public Rainbows()
   {
      // Sets the title for the window.
      setTitle("Rainbows and Butterflies");

      // Sets the size of the window.
      setSize(winWidth, winHeight);

      // Sets what happens when the close button is clicked.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Adds a GridLayout manager to the JFrame
      setLayout(new GridLayout(2, 3));

      // Calls 
      buildPanel();

      // Adds the panels to the JFrame.
      add(blackPanel);
      add(bluePanel);
      add(cyanPanel);
      add(greenPanel);
      add(magentaPanel);
      add(orangePanel);

      // Packs ths window so that all panels will be displayed.
      pack();

      // Displays the window to the user.
      setVisible(true);
   }

   private void buildPanel()
   {
      // Creates the six JPanel objects.
      blackPanel = new JPanel();
      bluePanel = new JPanel();
      cyanPanel = new JPanel();
      greenPanel = new JPanel();
      magentaPanel = new JPanel();
      orangePanel = new JPanel();

      // Creates the six JLabel objects.
      blackSection = new JLabel("Black");
      blueSection = new JLabel("Blue");
      cyanSection = new JLabel("Cyan");
      greenSection = new JLabel("Green");
      magentaSection = new JLabel("Magenta");
      orangeSection = new JLabel("Orange");

      // Creates a line border around each panel.
      blackPanel.setBorder(BorderFactory.createLineBorder(Color.YELLOW,2));
      bluePanel.setBorder(BorderFactory.createLineBorder(Color.YELLOW,2));
      cyanPanel.setBorder(BorderFactory.createLineBorder(Color.YELLOW,2));
      greenPanel.setBorder(BorderFactory.createLineBorder(Color.YELLOW,2));
      magentaPanel.setBorder(BorderFactory.createLineBorder(Color.YELLOW,2));
      orangePanel.setBorder(BorderFactory.createLineBorder(Color.YELLOW,2));

      // Sets the font of each label.
      blackSection.setFont(new Font("Times Roman", Font.BOLD, 30));
      blueSection.setFont(new Font("Times Roman", Font.BOLD, 30));
      cyanSection.setFont(new Font("Times Roman", Font.BOLD, 30));
      greenSection.setFont(new Font("Times Roman", Font.BOLD, 30));
      magentaSection.setFont(new Font("Times Roman", Font.BOLD, 30));
      orangeSection.setFont(new Font("Times Roman", Font.BOLD, 30));

      // Sets the tool tip for each label.
      blackSection.setToolTipText("Black");
      blueSection.setToolTipText("Blue");
      cyanSection.setToolTipText("Cyan");
      greenSection.setToolTipText("Green");
      magentaSection.setToolTipText("Magenta");
      orangeSection.setToolTipText("Orange");

      // Sets the forground color for each label.
      blackSection.setForeground(Color.BLACK);
      blueSection.setForeground(Color.BLUE);
      cyanSection.setForeground(Color.CYAN);
      greenSection.setForeground(Color.GREEN);
      magentaSection.setForeground(Color.MAGENTA);
      orangeSection.setForeground(Color.ORANGE);

      // Sets the background color for each label.
      blackPanel.setBackground(Color.WHITE);
      bluePanel.setBackground(Color.WHITE);
      cyanPanel.setBackground(Color.WHITE);
      greenPanel.setBackground(Color.WHITE);
      magentaPanel.setBackground(Color.WHITE);
      orangePanel.setBackground(Color.WHITE);

      // Adds the labels to there specific panel.
      blackPanel.add(blackSection);
      bluePanel.add(blueSection);
      cyanPanel.add(cyanSection);
      greenPanel.add(greenSection);
      magentaPanel.add(magentaSection);
      orangePanel.add(orangeSection);

   }

   public static void main(String[] args)
   {
      // Calls ThreeCards constructor.
      new Rainbows();
   }
}

Centering things is a responsiblility of the layout manager. Have a look at this and chose a layout manager that handles sizing & centering the way you want.
Note also the the frame has a layout manager that positions the panels, but the panels have their own layout managers (not necessarily the same) that layout the labels etc in the panels.

Thank you for clearing that up i was trying to figure out how to use the borderlayout to center it and i think i have figured it out. It seems i had to intiate a new instance of border layout when i created the pane SO it looks like this now:

// Creates the six JPanel objects.
blackPanel = new JPanel(new BorderLayout());
bluePanel = new JPanel(new BorderLayout());
cyanPanel = new JPanel(new BorderLayout());
greenPanel = new JPanel(new BorderLayout());
magentaPanel = new JPanel(new BorderLayout());
orangePanel = new JPanel(new BorderLayout());

// Centers the labels
blackSection.setHorizontalAlignment(JLabel.CENTER);
blueSection.setHorizontalAlignment(JLabel.CENTER);
cyanSection.setHorizontalAlignment(JLabel.CENTER);
greenSection.setHorizontalAlignment(JLabel.CENTER);
magentaSection.setHorizontalAlignment(JLabel.CENTER);
orangeSection.setHorizontalAlignment(JLabel.CENTER);

Thank you for your link again it helped to clear some things up.

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.