「java自定义颜色」java中颜色怎么定义

博主:adminadmin 2022-11-21 23:13:11 72

今天给各位分享java自定义颜色的知识,其中也会对java中颜色怎么定义进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

请问java中怎样设置字体的颜色?

下拉框和textfield,textarea这些控件都有setforeground方法,这个函数是设置前景色的,设置为红色就行了.

例如:textfield

txt=new

textfield("请输入姓名");

txt.setforeground(color.red);

jxl怎样使用自定义颜色设置单元格颜色DevNote

xl在Java开源世界中比较有影响力的操作Excel的API工具,使用也很广泛。但是jxl组件中没有提供直接自定义RGB颜色的方法。我们可以通过重置jxl中默认的颜色常量,实现自定义颜色的功能。

代码如下:

import java.awt.Color;

import java.io.File;

import java.io.IOException;

import jxl.Workbook;

import jxl.format.Colour;

import jxl.write.Label;

import jxl.write.WritableCellFormat;

import jxl.write.WritableFont;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

public class TestJXLColor {

public static void main(String[] args) {

try {

WritableWorkbook workbook = Workbook.createWorkbook(new File(

"d:\\test-color.xls"));

WritableSheet sheet = workbook.createSheet("测试文字", 0);

// 方法一: 使用jxl默认颜色

WritableFont font = new WritableFont(WritableFont.createFont("宋体"),

10, WritableFont.NO_BOLD);// 字体样式

WritableCellFormat wcf = new WritableCellFormat(font);

wcf.setBackground(Colour.BLUE_GREY);

sheet.addCell(new Label(1, 1, "测试颜色---BLUE_GREY", wcf));

// 方法二:设置自定义颜色,通过java.awt.Color中decode方法提取16进制颜色值

Color color = Color.decode("#EEA9B8"); // 自定义的颜色

workbook.setColourRGB(Colour.ORANGE, color.getRed(),

color.getGreen(), color.getBlue());

WritableCellFormat wcf1 = new WritableCellFormat(font);// 单元格样式

wcf1.setBackground(Colour.ORANGE);

sheet.addCell(new Label(1, 2, "测试颜色---自定义#EEA9B8", wcf1));

// 方法三:设置自定义颜色,按红、绿、蓝的16进制值直接定义颜色值。

workbook.setColourRGB(Colour.LIGHT_BLUE, 0x76, 0xEE, 0x00);

WritableCellFormat wcf2 = new WritableCellFormat(font);// 单元格样式

JAVA中用什么来设置颜色

java里面的颜色类是 Color类,可以直接new这个类。

new Color(int i) : 这个是传该类静态颜色常量,直接生成具体颜色。

new Color(int a, int b, int c) : 这个是根据RGB三原色new一个对应的颜色对象出来。

还有好多方法,你可以自己查查API。

java里如何声明一个颜色类?

声明是指对象,定义才是指类比较好理解。

java里如何声明一个颜色类对象。

java里如何定义一个颜色类。

如果是第一种的话:   java.awt.Color color;

如果是 第二种就是自定义类,需要看具体需求来,或者可以参考java.awt.Color的源码。

用java声明一个颜色类Color

import java.awt.*;

import java.awt.event.*;

public class adjustcolor implements AdjustmentListener, WindowListener {

Frame f=new Frame("调整颜色");

Label l1=new Label("调整滚动条,会改变初始颜色",Label.CENTER);

Label l2=new Label("此处显示颜色值",Label.CENTER);

Label l3=new Label("红",Label.CENTER);

Label l4=new Label("绿",Label.CENTER);

Label l5=new Label("蓝",Label.CENTER);

Scrollbar scr1=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);

Scrollbar scr2=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);

Scrollbar scr3=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);

public adjustcolor(){

f.add(l1);

f.add(l2);

f.add(l3);

f.add(l4);

f.add(l5);

f.add(scr1);

f.add(scr2);

f.add(scr3);

f.setSize(400,350);

f.setVisible(true);

f.addWindowListener(this);

f.setResizable(false);

l1.setBackground(Color.GREEN);

scr1.setBounds(35,225,360,25);

scr2.setBounds(35,255,360,25);

scr3.setBounds(35,285,360,25);

l1.setBounds(0,0,400,200);

l2.setBounds(0,310,400,30);

l3.setBounds(0,225,30,30);

l4.setBounds(0,255,30,30);

l5.setBounds(0,285,30,30);

scr1.addAdjustmentListener(this);

scr2.addAdjustmentListener(this);

scr3.addAdjustmentListener(this);

l1.setBackground(Color.GREEN);

scr1.setBackground(Color.RED);

scr2.setBackground(Color.GREEN);

scr3.setBackground(Color.blue);

}

public void adjustmentValueChanged(AdjustmentEvent e){

int a=scr1.getValue();

int b=scr2.getValue();

int c=scr3.getValue();

l1.setBackground(new Color(a,b,c)) ;

l2.setText("红"+" "+"绿"+" "+"蓝"+" "+a+" "+b+" "+c);

l1.setText(null);

}

public static void main(String[] args){

new adjustcolor();

}

public void windowActivated(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowClosed(WindowEvent arg0) {

}

public void windowClosing(WindowEvent arg0) {

System.exit(0);

}

public void windowDeactivated(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowDeiconified(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowIconified(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowOpened(WindowEvent arg0) {

// TODO Auto-generated method stub

}

}

这是源代码 应该是你想要的

「java自定义颜色」java中颜色怎么定义

关于java自定义颜色和java中颜色怎么定义的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-21,除非注明,否则均为首码项目网原创文章,转载请注明出处。