SetBackground (new color ()); in java does not understand the given rgb value

After searching on Google for half an hour, I gave up! :)

I have a program with some gui, on a JFrame that I installed,

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

[Problem] It gives a grayish color, but not the right one! If I check the gui color in Photo Shop, it gives me RGB values ​​(126, 125, 123)

I am really upset. Does anyone have the same problem?

Ps. I tried to use the HEX value, the same result.

Regards, Juri

+6
source share
7 answers
 I have a program with some gui, on the JFrame I set, setBackground( new Color(107, 106, 104) ); [The problem] It gives a greyish color, but not the right one! If I check the gui color in Photo Shop, it gives me the RGB values (126, 125, 123) 

you cannot set setBackground for JFrame , it 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(); } }); } } 
+6
source

check with Adam's comment, and even if it didn’t work, then without any working code, I just guess that this script comes up due to a zero order or JFrame layout. In fact in java swing, setting the background color requires a bit more attention, check out Swing Java Docs.

+1
source

I tried what you explained; in awt, this is not a problem; in the swing it seems that the background is not set correctly
you checked if your background is changing, for example. 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); }} 
0
source

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

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

 setOpaque(true); 
0
source

It worked for me. Hope this helps. The code adds JPanel to the current JFrame, you can continue to create guis in this panel. You can customize RGB colors on JPanel and 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); } } 
0
source

The first step is to make the object from jFrame :

 JFrame frame = new JFrame(); 

Second step:

 frame.getContentPane().setBackground(new Color(16,144,144)); 
0
source
  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)); } 

}

0
source

Source: https://habr.com/ru/post/910683/


All Articles