有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

颜色如何在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();
    }

共 (2) 个答案

  1. # 1 楼答案

    你可以改变

        this.setBackground(Color.PINK);
    

        this.setBackground(new Color());//Put RGB number in the empty parenthesis
    
  2. # 2 楼答案

    缺少上下文,如果您指的是Swing组件中的背景色(例如JFrame、JPanel),则它们具有getBackground()或setBackground(color)方法

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

    java中的Color有很多返回颜色类型eg的方法

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