JW_
JW_

Reputation: 682

setBackground(new color()); in java does not understand the given RGB value

I have a program with some gui, on the JFrame I set,

 setBackground( new Color(107, 106, 104) );

The issue is that I get a greyish color, but not the right one. If I check it in PhotoShop, it gives me the RGB values (126, 125, 123)

Ps. I have tried with HEX value, the same result.

Upvotes: 6

Views: 102701

Answers (7)

mKorbel
mKorbel

Reputation: 109823

I have a program with some gui, on the JFrame I set,

setBackground( new Color(107, 106, 104) );

It gives a greyish color, but not the right one! If I check the gui's color in Photo Shop, it gives me the RGB values (126, 125, 123)

You can not set setBackground for JFrame, this is only possible for ContentPane, for example

JFrame#getContentPane.setBackground(new Color(107, 106, 104));

EDIT

enter image description here

from code

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Check extends JFrame {

    private static final long serialVersionUID = 1L;

    public void makeUI() {
        JFrame f = new JFrame();
        f.getContentPane().setBackground(new Color(107, 106, 104));
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f.setSize(new Dimension(300, 200));
        f.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Check().makeUI();
            }
        });
    }
}

Upvotes: 9

David Ibarra
David Ibarra

Reputation: 1

 if(evt.getSource() == jMenuItem11){
        getContentPane().setBackground(new Color(170, 8, 54));
    }
    if(evt.getSource() == jMenuItem12){
        getContentPane().setBackground(new Color(8, 54, 169));
    }
    if(evt.getSource() == jMenuItem13){
       getContentPane().setBackground(new Color(84, 8, 170));
    }

}

Upvotes: 0

sheikh sarfaraz
sheikh sarfaraz

Reputation: 1

First step - make an object of jFrame:

JFrame frame = new JFrame();

Second step:

frame.getContentPane().setBackground(new Color(16,144,144));

Upvotes: 0

BUKJACK
BUKJACK

Reputation: 1

This worked for me. Hope it helps The, code, adds a JPanel, to current JFrame, you can further build guis on this panel. You can customize the RGB colours, on JPanel, not on JFrame.

import javax.swing.*;
import java.awt.*;

public class Main{

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        //Class class = new Class();
        frame.setSize(1920,1080);
        //frame.setTitle("XYZ");
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        panel.setBackground(new Color(51,153,255));
        //panel.add(class);
    }
}

Upvotes: 0

Dylan Turner
Dylan Turner

Reputation: 318

http://www.tayloredmktg.com/rgb/

It looks like gray is at the top of the page riht when you open it. :) Also make sure your JFrame is opaque or you won't see your color!

setOpaque(true);

Upvotes: 0

Hachi
Hachi

Reputation: 3289

i've tried what you explained; in awt it's no problem; in swing it seems the background is not set properly
did you check, if your background changes, e.g. with setBackground(Color.red)?

example Code:

import java.awt.*;
import javax.swing.*;

public class Tmp extends Frame { public static void main(String[] args) {
    //Frame tmp = new Frame();
    Frame tmp = new JFrame();
    tmp.setBackground(new Color(107, 106, 104));
    tmp.setSize(40,40);
    tmp.setVisible(true);
}}

Upvotes: 0

Abhishek Choudhary
Abhishek Choudhary

Reputation: 8395

check with Adam's comment and even if not worked then without any working code I am just guessing that this scenario is getting raised due zero ordering or saying layout of the JFrame. Actually in java swing , setting the background color needs a little bit of more attention, check Swing Java Docs.

Upvotes: 1

Related Questions