java代码如何改底色 javagui设置背景颜色( 二 )


r+=10;
if(r255) {//如果red大于255 ,可以设置为0 ,也可以设置为255,一直锁定为255 也可设置为初始的90,这里题目这里没有要求
r=90;
}
}else if("green".equals(cmd)) {
g+=10;
if(g255) {
g=15;
}
}else if("blue".equals(cmd)){
b+=10;
if(b255) {
b=195;
}
}
this.getContentPane().setBackground(new Color(r,g,b));
//System.out.println(this.getContentPane().getBackground());
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame().setVisible(true);//启动窗口并设置可见
}
});
}
}
用Java设置Excel背景色?excel表格的样式都是通过创建cellstyle实现的,HSSFCellStyle cellStyle =workbook.createCellStyle();
设置背景色的代码cellStyle.setFillBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
再将想要设置背景色的单元格cell.setStyle(cellStyle);
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 编程 背景颜色的改变**************************************************************
新建一个类ChangeColor.javajava代码如何改底色,代码如下java代码如何改底色:
**************************************************************
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
/**
* @author Godwin
* @version 2010-05-16
*/
public class ChangeColor extends JFrame implements MouseMotionListener {
public ChangeColor() {
this.setTitle("Change Color");
this.setBounds(300, 200, 400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.getContentPane().setBackground(Color.GREEN);
this.addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e) {
if (e.getX()(this.getWidth() / 2)) {
this.getContentPane().setBackground(Color.RED);
} else {
this.getContentPane().setBackground(Color.BLUE);
}
}
public void mouseDragged(MouseEvent e) {
}
public static void main(String[] args) {

推荐阅读