Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JLabel.setBackground(Color.red) not working

807569Aug 24 2006 — edited Aug 28 2006
Hi, I am using the (JLabel) lblPrmf.setBackground(Color.RED), but the label is not colored. Here is my program:

import java.util.*;
import javax.swing.*; //JFrame and JPanel etc...
import java.awt.*; //BorderLayout and Container etc....
import java.awt.Color;
import java.awt.event.*; //ActionListener og WindowAdapter
public class AllVac extends JFrame
{

//Define JLabel
private JLabel lblDays;
private JLabel lblAbs;
private JLabel lblPrmf;

//Define JButton
private JButton month;
private JButton back;
private JButton forward;

//Define JPanel
private JPanel south;
private JPanel north;

public AllVac()
{
// JFrame jf = new JFrame("Vacation for the Stavanger Team ");
Container c = getContentPane();
// c.setTitle("Vacation for the Stavanger Team ");
c.setLayout(new BorderLayout());
c.add(new ShowNorth(), BorderLayout.NORTH);
c.add(new ShowSouth(), BorderLayout.SOUTH);

pack();
setVisible(true);
addWindowListener(new Close());

}

public static void main(String args[])
{
AllVac a = new AllVac();
}

public class ShowNorth extends JPanel
{

public ShowNorth()
{
north = new JPanel(new GridLayout(1, 3));
month = new JButton("August 2006");
back = new JButton("<<");
forward = new JButton(">>");
north.add(back);
north.add(month);
north.add(forward);
add(north);
}
}

public class ShowSouth extends JPanel
{
public ShowSouth()
{
int days = 31;
south = new JPanel(new GridLayout(16, days+1));

//Arrange the days
for ( int i = 1; i<(days+1); i++)
{
lblDays = new JLabel(" " + Integer.toString(i));
lblDays.setBorder(BorderFactory.createLineBorder(Color.red));
south.add(lblDays);
}
add(south, BorderLayout.LINE_START);

//Fill in the names and vacation days
for ( int i = 1; i<(days+1); i++)
{
lblAbs = new JLabel(" ");
south.add(lblAbs);
}
add(south, BorderLayout.LINE_START);


for (int j = 0; j<14; j++)
{
for ( int i = 1; i<days+1; i++)
{
String v = "v";

lblPrmf = new JLabel(" " + v + " ");
lblPrmf.setBackground(Color.RED);
lblPrmf.setBorder(BorderFactory.createLineBorder(Color.blue));
lblPrmf.setBackground(Color.RED);
lblPrmf.repaint();
south.add(lblPrmf);
}
add(south);
}
}
}

public class Close extends WindowAdapter
{
public void windowClosing(WindowEvent c)
{
System.exit(0);
}
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 25 2006
Added on Aug 24 2006
10 comments
1,013 views