设置背景(新颜色());在java中不理解给定的RGB值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9698359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:19:30  来源:igfitidea点击:

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

javaswinghexrgbsetbackground

提问by JW_

After searching google for a half hour I gave up! :)

在谷歌搜索半小时后我放弃了!:)

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

我有一个带有一些 gui 的程序,在我设置的 JFrame 上,

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

[The problem] 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)

[问题] 它给出了一种灰色,但不是正确的!如果我在 Photo Shop 中检查 gui 的颜色,它会给出 RGB 值(126、125、123)

I'am really frustrated.. Someone have the same problem?

我真的很沮丧.. 有人有同样的问题吗?

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

附言。我尝试过使用 HEX 值,结果相同。

Best regards, Juri

最好的问候,尤里

采纳答案by mKorbel

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's color in Photo Shop, it gives me the RGB 
values (126, 125, 123)

you can not set setBackgroundfor JFrame, this is only possible for ContentPane, for example

您不能设置setBackgroundJFrame,这仅适用于ContentPane,例如

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();
            }
        });
    }
}

回答by Abhishek Choudhary

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.

检查亚当的评论,即使没有工作然后没有任何工作代码我只是猜测这个场景由于零排序或说JFrame的布局而被提升。实际上在 java swing 中,设置背景颜色需要多加注意,请查看 Swing Java Docs。

回答by Hachi

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)?

我已经尝试过你解释的;在 awt 中没问题;在 Swing 中,似乎背景设置不正确
,您是否检查过背景是否发生变化,例如使用 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);
}}

回答by Blue Okiris

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

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!

当你打开它时,它看起来像灰色在页面的顶部。:) 还要确保您的 JFrame 是不透明的,否则您将看不到您的颜色!

setOpaque(true);

回答by BUKHyman

This worked for me. Hope it helpsThe, 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.

这对我有用。希望对当前的JFrame有帮助,code,添加一个JPanel,你可以在这个面板上进一步构建guis。您可以在 JPanel 上自定义 RGB 颜色,而不是在 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);
    }
}

回答by sheikh sarfaraz

First step - make an object of jFrame:

第一步 - 创建一个对象jFrame

JFrame frame = new JFrame();

Second step:

第二步:

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

回答by David Ibarra

 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));
    }

}

}