如何在java中定义可自定义的RGB背景颜色?(How do you define a customisable RGB background colour in java?)

编程入门 行业动态 更新时间:2024-10-25 12:19:41
如何在java中定义可自定义的RGB背景颜色?(How do you define a customisable RGB background colour in java?)

我是java的新手,我已经在谷歌搜索了一段时间的答案,但我找不到获得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(); }

I am new to java and I have searched for an answer on Google for a while now, but I cannot find a way to get an RGB background colour, I can only find how to set a background colour from the main colours already provided by java(for example, "Color.BLACK"). I am using a JFrame. Please help. Thanks. In this is my background code.

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

最满意答案

缺少上下文,如果你的意思是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();

The context is missing, if you mean background color in Swing component (e.g JFrame, JPanel) they have methods getBackground() or setBackground(Color).

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

Color in java have lot of methods for return Color type 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();

更多推荐

本文发布于:2023-07-23 18:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   定义   颜色   背景   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!