「java颜色指令」java中颜色怎么定义
今天给各位分享java颜色指令的知识,其中也会对java中颜色怎么定义进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java里颜色类默认的几种颜色对应的int值是多少?
- 2、爱心代码编程java怎样加颜色
- 3、java关键字用什么颜色
- 4、java设定背景颜色
- 5、java 中怎样设置窗口的颜色
- 6、JAVA中怎么改变字体颜色?
java里颜色类默认的几种颜色对应的int值是多少?
public final static Color white = new Color(255, 255, 255);
public final static Color lightGray = new Color(192, 192, 192);
public final static Color gray = new Color(128, 128, 128);
public final static Color darkGray = new Color(64, 64, 64);
public final static Color black = new Color(0, 0, 0);
public final static Color red = new Color(255, 0, 0);
public final static Color pink = new Color(255, 175, 175);
public final static Color orange = new Color(255, 200, 0);
public final static Color yellow = new Color(255, 255, 0);
public final static Color green = new Color(0, 255, 0);
public final static Color magenta = new Color(255, 0, 255);
public final static Color cyan = new Color(0, 255, 255);
public final static Color blue = new Color(0, 0, 255);
其值在Color类内部以int的形式存着,24-32位为alpha值,16-23为red,8-15为green,0-7则是blue。默认的alpha值为全1,也就是255,完全不透明。
比如说
public final static Color pink = new Color(255, 175, 175);
表示在其内部颜色的值为255*2^24+255*2^16+175*2^8+175=4294946735
爱心代码编程java怎样加颜色
1、首先打开java编译软件,引入爱心代码编程。
2、其次打开图面编译,选择编辑颜色。
3、最后在该代码编程中输入需要添加的颜色即可。
java关键字用什么颜色
红色。在eclipse中,关键字有特定的颜色区分,为红色。红色字体是java语法的关键字。 蓝色字体是代码里面定义的常量,或者字符串值。 黑色字体就是代码了。
java设定背景颜色
本来是在drawcomponent这个里边使用setBackground,你想啊drawcomponent是继承JComponent的所以它是一个容器,所以它同样有setBackground这个方法来设置它的背景颜色
但是因为你在设置它本身为一个画布,因为你用了paintComponent(Graphics g)
这个方法,所以setBackground这个方法即使你用了也看不到很大的效果。但是有一种取代的方法就是在paintComponent(Graphics g)方法中首先就用Graphics 所含有的方法g.setColor(Color.black);来设置背景颜色再用g.fillRect(0, 0, this.getWidth(), this.getHeight());来填满整个容器,这就达到了设置背景目的。然后你再g.setColor(其他颜色);来绘制其它图形.
具体代码:(在你以上的代码上修改了点)
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
g.setColor(Color.black);//这里设置背景颜色
g.fillRect(0, 0, this.getWidth(), this.getHeight());//这里填充背景颜色
double x=100;
double y=100;
double w=200;
double h=150;
Rectangle2D rect=new Rectangle2D.Double(x,y,w,h);
g2.setPaint(Color.white);//这里是你设置其他笔触颜色
g2.draw(rect);
Ellipse2D ellipse=new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse);
Point2D p1=new Point2D.Double(x-40,y-30);
Point2D p2=new Point2D.Double(x+w+40,y+h+30);
g2.draw(new Line2D.Double(p1,p2));
double centerx=rect.getCenterX();
double centery=rect.getCenterY();
double radius=150;
Ellipse2D circle=new Ellipse2D.Double();
circle.setFrameFromCenter(centerx,centery,centerx+125,centery+125);
g2.draw(circle);
}
测试结果图
java 中怎样设置窗口的颜色
调用需要设置颜色的控件的setBackgroud();方法就可以了。
但是设置JFrame和JLabel的背景色,一般就是下面的做法
JFrame frame = new JFrame();
frame.setBackground(Color.Red);
JLabel l = new JLabel();
l.setBackground(Color.Yellow);
frame.add(l);
结果根本就没有反应。这是由于Swing跟AWT有千丝万缕的联系,它既要支持AWT又要有自己新的体系,所以呢,这个如果对于AWT中的Frame是可以直接通过setBackground来设置背景色,但是对于JFrame则不可以,应该采用下面的方法:
JFrame frame = new JFrame();
frame.getContentPane().setBackground(Color.Red);
而对于JLabel来说则要设置JLabel为不透明的才行,即
JLabel comp = new JLabel(value);
comp.setBackground(color);
comp.setOpaque(true);
这句代码frame.setBackground(Color.Red);
改变的是框架的颜色,框架的上面还有窗格,所以你要改变窗格的颜色才可以侧低改变框架的颜色
在主函数里加Containerframe.getContentPane()意思是获得窗格
setBackground(Color.Red); 改变窗格颜色
另外再附一段背景颜色渐变的代码
运行示意图如下:
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
class ShadePanel extends JPanel {
private static final long serialVersionUID = -2644424271663261406L;
public ShadePanel() {
super();
setLayout(null);
}
@Override
protected void paintComponent(Graphics g1) {// 重写绘制组件外观
Graphics2D g = (Graphics2D) g1;
super.paintComponent(g);// 执行超类方法
int width = getWidth();// 获取组件大小
int height = getHeight();
// 创建填充模式对象
GradientPaint paint = new GradientPaint(0, 0, Color.CYAN, 0, height,
Color.MAGENTA);
g.setPaint(paint);// 设置绘图对象的填充模式
g.fillRect(0, 0, width, height);// 绘制矩形填充控件界面
}
}
public class ShadeBackgroundImage extends JFrame {
private static final long serialVersionUID = 4693799019369193520L;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ShadeBackgroundImage frame = new ShadeBackgroundImage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public ShadeBackgroundImage() {
setTitle("背景为渐变色的主界面");// 设置窗体标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();// 创建内容面板
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
ShadePanel shadePanel = new ShadePanel();// 创建渐变背景面板
contentPane.add(shadePanel, BorderLayout.CENTER);// 添加面板到窗体内容面板
}
}
JAVA中怎么改变字体颜色?
字体大小及颜色
a:Java代码区域的字体大小和颜色:
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Java修改 -- Java Edit Text Font
b:控制台
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Debug -- Console font
c:其他文件
window -- Preferences -- General -- Appearance -- Colors And Fonts -- Basic -- Text Font
java颜色指令的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java中颜色怎么定义、java颜色指令的信息别忘了在本站进行查找喔。
发布于:2022-12-10,除非注明,否则均为
原创文章,转载请注明出处。