Thursday, 21 August 2014

How to Change the Background Colour in Java

I decided I did not like the background colour of my frame so I added the line shown in bold italics, compiled it and ran it:

andrew@UBUNTU:~/Java$ cat AndrewsFrame.java
import java.awt.*;
public class AndrewsFrame extends Frame
{
public AndrewsFrame()
  {
  super("Andrew's Frame");
  setSize(500,300);
  setBackground(Color.red);
  setVisible(true);
  }
public static void main (String args[])
  {
  AndrewsFrame af = new AndrewsFrame();
  }
}
andrew@UBUNTU:~/Java$ javac AndrewsFrame.java
andrew@UBUNTU:~/Java$ java AndrewsFrame


This time the background was red (as usual, click on the image to enlarge it and bring it into focus):