新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
按照你的要求,编写的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();
}
}
运行结果
以下是代码,可以参考一下,希望对您有帮助。
============================================
public class PrintStars {
private static final int LENGTH=10;
private static final int WIDTH=20;
public static void main(String[] args) {
for (int i = 0; i LENGTH; i++) {
if(i==0||i==LENGTH-1){
for (int j = 0; j WIDTH; j++) {
System.out.print("* ");
}
}else {
for (int j = 0; j WIDTH; j++) {
if(j==0||j==WIDTH-1){
System.out.print("* ");
}else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
兄弟帮你写了一个:
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
public class Print {
public static void main(String[] args) {
new Te();
}
}
class Te extends Frame implements ActionListener {
Color cc = Color.red;
int x = -20, y = -50;
Random r = new Random();
public Te() {
this.setLayout(null);
Button b = new Button("画圆");
this.add(b);
b.setBounds(30,30,50,50);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter () {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setBounds(200,200,500,400);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
this.cc = Color.red;
this.x = r.nextInt(400);
do {
int x1 = r.nextInt(300);
this.y = x1;
} while (this.y 50);
this.repaint();
}
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(cc);
g.drawRect(x,y,50,50);
g.setColor(c);
}
}
你的代码有问题,你的类本身是frame,你也在类中绘制,但是你却没有显示,而是另外定义了一个frame来显示,你修改一下:
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
public class FrameTest extends Frame {
/**
* @param args
*/
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.fillRect(100, 100, 30, 30);
try {
Thread.sleep(500);
}
catch (Exception ex) {
ex.printStackTrace();
}
//repaint();
}
FrameTest()
{
super("title");
setLocation(100,100);
setSize(600,400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FrameTest ft=new FrameTest();
}
}
这样应该没问题了。