网站首页 > 文章中心 > 其它

java中改变颜色的代码

作者:小编 更新时间:2023-09-20 13:03:35 浏览量:409人看过

写一个java程序,使其画一个正方形并改变它的颜色(每秒钟改变一次).求完整代码谢谢

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

java中改变颜色的代码-图1

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("变色正方形");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

java中改变颜色的代码-图2

setVisible(true);

}

public?void?paint(Graphics?g){

g.setColor(colors[i]);

public?void?run()?{

while(true){

try?{

Thread.sleep(1000);

}?catch?(InterruptedException?e)?{

e.printStackTrace();

i++;

else

i=0;

repaint();

public?static?void?main(String[]?args)?{

F?f=new?F();

Thread?t=new?Thread(f);

t.start();

运行结果

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)

{

g.setColor(Color.black);//这里设置背景颜色

g.fillRect(0,?0,?this.getWidth(),?this.getHeight());//这里填充背景颜色

double?x=100;

double?y=100;

ellipse.setFrame(rect);

double?centerx=rect.getCenterX();

double?centery=rect.getCenterY();

测试结果图

java改变按钮颜色

JButton btnYellow = null;

JButton btnBlue = null;

JButton btnRed = null;

btnYellow.setActionCommand("yellow");

btnBlue.setActionCommand("blue");

btnRed.setActionCommand("red");

public void actionPerformed(ActionEvent e) {

String command = event.getActionCommand();

if( "yellow".equals(command) ){

setBackground(Color.yellow);

btnYellow.setBackground(Color.yellow);

}else if( "blue".equals(command) ){

setBackground(Color.blue);

btnBlue.setBackground(Color.blue);

}else if( "red".equals(command) ){

setBackground(Color.red);

btnRed.setBackground(Color.red);

写出了部分代码

java 编程 背景颜色的改变

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

新建一个类ChangeColor.java,代码如下:

import?java.awt.event.MouseEvent;

import?java.awt.event.MouseMotionListener;

/**

*/

public?class?ChangeColor?extends?JFrame?implements?MouseMotionListener?{

public?ChangeColor()?{

this.setTitle("Change?Color");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

this.getContentPane().setBackground(Color.GREEN);

this.addMouseMotionListener(this);

public?void?mouseMoved(MouseEvent?e)?{

this.getContentPane().setBackground(Color.RED);

}?else?{

this.getContentPane().setBackground(Color.BLUE);

public?void?mouseDragged(MouseEvent?e)?{

new?ChangeColor();

运行结果如下:

java通过按钮改变文本框内字体颜色,在原代码上面改,尽量简单一点?

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Frame;

import java.awt.Insets;

import java.awt.TextArea;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextPane;

import javax.swing.border.EmptyBorder;

import javax.swing.text.AttributeSet;

import javax.swing.text.BadLocationException;

import javax.swing.text.SimpleAttributeSet;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyleContext;

import javax.swing.text.StyledDocument;

public class main {

static String tf_str=null;

public static void main(String[] args) {

Frame a = new Frame("打印");

a.setLayout(new FlowLayout());

java中改变颜色的代码-图3

Button c = new Button("确定");

Button e = new Button("红色");

Button f = new Button("蓝色");

JTextPane d=new JTextPane();

d.setMargin(new Insets(100,100, 100, 100));

a.add(b);

a.add(c);

a.add(d);

a.add(e);

a.add(f);

a.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

});

c.addActionListener(new ActionListener() {

d.setText("");

tf_str = b.getText().trim();

b.setText("");

appendToPane(d, tf_str, Color.black);

b.requestFocus();

e.addActionListener(new ActionListener() {

appendToPane(d, tf_str, Color.RED);

f.addActionListener(new ActionListener() {

appendToPane(d, tf_str, Color.BLUE);

a.setVisible(true);

private static void appendToPane(JTextPane tp, String msg, Color c) {

StyleContext sc = StyleContext.getDefaultStyleContext();

AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);

aset = sc.addAttribute(aset, StyleConstants.FontFamily, "宋体");

aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

int len = tp.getDocument().getLength();

tp.setCaretPosition(len);

tp.setCharacterAttributes(aset, false);

tp.replaceSelection(msg);

分析题目:

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

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

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

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

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

五 具体参考代码

import?java.awt.*;

import?java.awt.event.*;

import?javax.swing.*;

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

public?class?MyFrame?extends?JFrame?implements?ActionListener{

public?MyFrame()?{

//组件的初始化

JButton?jbRed?=?new?JButton("red");

JButton?jbGreen?=?new?JButton("green");

jbGreen.addActionListener(this);

JButton?jbBlue?=?new?JButton("blue");

jbBlue.addActionListener(this);

//添加组件到窗口

add(jbRed);

add(jbGreen);

add(jbBlue);

//窗口的设置

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

setTitle("窗口标题");

addWindowListener(new?WindowAdapter()?{

@Override

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

System.exit(0);//退出

public?void?actionPerformed(ActionEvent?e)?{

String?cmd=e.getActionCommand();

r+=10;

}else?if("green".equals(cmd))?{

g+=10;

}else?if("blue".equals(cmd)){

b+=10;

this.getContentPane().setBackground(new?Color(r,g,b));

//System.out.println(this.getContentPane().getBackground());

EventQueue.invokeLater(new?Runnable()?{

new?MyFrame().setVisible(true);//启动窗口并设置可见

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章