如何在 Java 中定义可自定义的 RGB 背景颜色



我是java的新手,我已经在Google上搜索了一段时间的答案,但是我找不到获取RGB背景颜色的方法,我只能找到如何从java已经提供的主要颜色(例如,"Color.BLACK"(中设置背景颜色。我正在使用 JFrame。请帮忙。谢谢。这是我的背景代码。

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(Color.PINK); //My current background colour code but I am looking to use a customisable RGB one.
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 40, y);
        tm.start();
    }

你可以改变

    this.setBackground(Color.PINK);

    this.setBackground(new Color());//Put RGB number in the empty parenthesis
上下文

缺失,如果你的意思是Swing组件中的背景颜色(例如JFrame,JPanel(,他们有方法getBackground((或setBackground(Color(。

JPanel panel = new JPanel();
Color yourColor = panel.getBackground();

java中的颜色有很多返回颜色类型的方法,例如。

int rgbValue = yourColor.getRGB(); // Returns the RGB value representing the color in the default sRGB ColorModel.
// or specific red, green, blue color value
int red = yourColor.getRed();
int green = yourColor.getGreen();
int blue = yourColor.getBlue();

最新更新