Curt Sizemore
Curt Sizemore

Reputation: 89

Background Color Issues

Can someone please assist me on why my background color of the frame is not being set. Is it possible to set the background color within the Paint() or must it be done in the JColor constructor

I am supposed to do the following for the BG color-

Write A GUI application that displays a single JButton and any background color you choose.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @author Curtis
 */
public class JColor extends JFrame implements ActionListener
{
Font myFont = new Font("Playbill", Font.PLAIN, 28);
JButton myButton = new JButton("Click Me!");
Color bgColor = new Color(255, 97, 3);
Color txtColor = new Color(0, 0, 205);
String firstName = "Curtis";
String lastName = "Sizemore";

public JColor()
        {
            super("String Painting Fun");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout (new BorderLayout());
            add(myButton, BorderLayout.SOUTH);
            setDefaultLookAndFeelDecorated(true);
            setBackground(Color.BLUE);
        }

@Override
public void paint(Graphics e)
{
    super.paint(e);
}

public static void main(String[] args) 
{
    final int TALL = 200;
    final int WIDE = 250;
    JColor frame = new JColor();
    frame.setSize(WIDE, TALL);

    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) 
{
    throw new UnsupportedOperationException("Not supported yet.");
}
}

Upvotes: 2

Views: 1709

Answers (1)

Pshemo
Pshemo

Reputation: 124275

Try calling it on the ContentPane instance (more info here)

public JColor() {
    super("String Painting Fun");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    add(myButton, BorderLayout.SOUTH);
    setDefaultLookAndFeelDecorated(true);
    getContentPane().setBackground(Color.BLUE);//<- update
}

Upvotes: 4

Related Questions