Yahoo Answers is shutting down on 4 May 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java Question - Background Color in a Window?

Alright, so I made this simple little window program:

import java.awt.Color;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Toolkit;

import java.awt.geom.Ellipse2D;

import javax.swing.JComponent;

import javax.swing.JFrame;

public class windowName {

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable(){

public void run(){

SimpleFrame frame = new SimpleFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

});

}

}

class SimpleFrame extends JFrame {

public SimpleFrame(){

//The following four lines create the screenWidth and screenHeight variables

Toolkit kit = Toolkit.getDefaultToolkit();

Dimension screenSize = kit.getScreenSize();

int screenWidth = screenSize.width;

int screenHeight = screenSize.height;

//The following lines are the properties of the frame itself

setSize(screenWidth/3, screenHeight/2);

setTitle("We're going to take your name!");

setLocation(450,200);

setBackground(Color.white);

//The following 2 lines create a new panel (which will be used to draw a circle)

DrawComponent component = new DrawComponent();

add(component);

}

}

class DrawComponent extends JComponent{

public void paintComponent(Graphics g){

Graphics2D g2 = (Graphics2D) g;

}

}

Now I'm having some problems. If you noticed, I tried to set the background color of the window to white. That's all I want: a window to pop up with a white background. However, this doesn't seem to work. I'm not sure why! (As a side note, the rest of the stuff on there is what I'll be adding on soon.)

Why won't it work and how can I make it work? Thank you so much for your time!

2 Answers

Relevance
  • ?
    Lv 5
    1 decade ago
    Favourite answer

    You need to call method setBackground(Color c) with method getContentPane().

    // getContentPane().setBackground(

    Color.white);

  • Anonymous
    5 years ago

    This depends completely on where your output goes to. I do not think, that it is possible in a Windows Console (cmd.exe). If you are on a Linux/Unix shell or a console with Ansi, you can Use ANSI-Color-Codes (see sources): public static void main(String[] args) {   System.out.println("\033[34mHi\033[0.....‡ }

Still have questions? Get answers by asking now.