1、首先打開java編譯軟件,引入愛心代碼編程。
創(chuàng)新互聯(lián)建站主要從事網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)河源,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
2、其次打開圖面編譯,選擇編輯顏色。
3、最后在該代碼編程中輸入需要添加的顏色即可。
按照你的要求,編寫的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();
}
}
運(yùn)行結(jié)果
**************************************************************
新建一個(gè)類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();
}
}
**************************************************************
運(yùn)行結(jié)果如下:
**************************************************************