java改变颜色代码 java中颜色

java 编程 背景颜色的改变

**************************************************************

成都创新互联是一家专注于网站建设、成都网站设计与策划设计,罗江网站建设哪家好?成都创新互联做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:罗江等地区。罗江做网站价格咨询:13518219792

新建一个类ChangeColor.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) {

new ChangeColor();

}

}

**************************************************************

运行结果如下:

**************************************************************

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 点击按纽改变背景颜色

分析题目:

一 分析布局: 题目明确的指出了按钮的位置和大小 ,那么说明需要使用的布局是空布局(绝对布局) , 而JFrame窗口的内容面板默认布局是边界布局(BorderLayout),所以需要设置一下

setLayout(null);//设置为绝对布局

二了解颜色. Color 可以通过红,绿,蓝 三原色, 不同的搭配, 形成不同的颜色.

每个原色的取值范围是0~255, 比如红色的rgb值就是r=255,g=0,b=0

胡萝卜色 r=237,g=145,b=33

三添加颜色 ,java给JFrame添加颜色,比较特殊. 必须添加到内容面板上,才能正常显示(因为JFrame分了好多层)

getContentPane().setBackground(new Color(r,g,b));//设置窗口的面板背景色

四 事件处理分析: 点击按钮,会触发ActionEvent 事件,这个事件会被ActionListener 接收器接收到, 只需要重写ActionListener 里的actionPerformed 方法, 即可实现点击按钮后,做某件事

五 具体参考代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

// 本类继承JFrame,实现了ActionListener接口

public class MyFrame extends JFrame implements ActionListener{

int r = 90;

int g = 15;

int b = 195;

public MyFrame() {

//组件的初始化

JButton jbRed = new JButton("red");

jbRed.setLocation(20, 80);//按钮位置

jbRed.setSize(80, 40);//按钮大小

jbRed.addActionListener(this);//添加点击按钮后的事件响应 ,因为本类实现了ActionListener接口,所以可以传入参数this

JButton jbGreen = new JButton("green");

jbGreen.setLocation(120, 80);

jbGreen.setSize(80, 40);

jbGreen.addActionListener(this);

JButton jbBlue = new JButton("blue");

jbBlue.setLocation(220, 80);

jbBlue.setSize(80, 40);

jbBlue.addActionListener(this);

//添加组件到窗口

add(jbRed);

add(jbGreen);

add(jbBlue);

//窗口的设置

setLayout(null);//因为每一个按钮都设置了位置和大小, 那么应该把窗口设置为空布局, 那么位置和大小才能有效

setTitle("窗口标题");

getContentPane().setBackground(new Color(r,g,b));//设置窗口的面板背景色

setLocation(220, 160);// 窗口位置

setSize(320, 240);// 窗口大小

//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭按钮时,结束程序

//下面也可以实现,点击关闭按钮时, 结束程序

addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {//点击关闭按钮会触发这个事件,调用这个方法

System.out.println("通过WindowListener实现关闭");

System.exit(0);//退出

}

});

}

public void actionPerformed(ActionEvent e) {

String cmd=e.getActionCommand();

//通过ActionCommand 来判断是哪一个按钮被点击了

if("red".equals(cmd)) {//如果是红色按钮被点击了,那么红色+10

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程序,使其画一个正方形并改变它的颜色(每秒钟改变一次)。求完整代码谢谢

按照你的要求,编写的Java程序如下

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JFrame;

public class F extends JFrame implements Runnable{

Color[] colors = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.black};  

int i=0;

F(){

setTitle("变色正方形");

setSize(400, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setVisible(true);

}

public void paint(Graphics g){

g.setColor(colors[i]);

g.drawRect(100,100,200,200);

}

@Override

public void run() {

while(true){

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

if(i7)

i++;

else

i=0;

repaint();

}

}

public static void main(String[] args) {

F f=new F();

Thread t=new Thread(f);

t.start();

}

}

运行结果

分享题目:java改变颜色代码 java中颜色
新闻来源:https://www.cdcxhl.com/article0/doddsio.html

成都网站建设公司_创新互联,为您提供网站设计公司微信小程序服务器托管网站内链做网站移动网站建设

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设