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();
}
}