Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教高手,背景颜色的不间断随机变化
flywithmoon





发贴: 44
积分: 0
于 2004-08-24 22:40 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
以下代码无法实现面板背景颜色的不间断随机变化,请教高手如何修改:
import java.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class test extends JFrame implements Runnable
{
  test(){
      Thread t=new Thread(this);
      this.getContentPane().add(new myp());
      t.start();
      }
  public static void main(String args []){
      test f=new test();
      f.setSize(300,300);
      f.show();
    }
  public void run()
  {
    while(true)
    {
      try{
      Thread.sleep(1500);repaint();
      System.out.print("hhh");
          }catch(Exception e){e.printStackTrace();}
      }
  }
}
class myp extends JPanel implements ActionListener
{
  JButton jb1,jb2,jb3,jb4;
  myp()
  {
    setLayout(new GridLayout(2,2));
    JPanel  p1=new JPanel();
    p1.setBackground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
    add(p1);jb1=new JButton("1");
    p1.add(jb1);
    JPanel  p2=new JPanel();
    p2.setBackground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
    add(p2);jb2=new JButton("2");
    p2.add(jb2);
    JPanel  p3=new JPanel();
    p3.setBackground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
    add(p3);jb3=new JButton("3");
    p3.add(jb3);
    JPanel  p4=new JPanel();
    p4.setBackground(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
    add(p4);jb4=new JButton("4");
    p4.add(jb4);
    jb1.addActionListener(this);
    jb2.addActionListener(this);
    jb3.addActionListener(this);
    jb4.addActionListener(this);repaint();
  }

  public void actionPerformed(ActionEvent e) {
    Graphics g=getGraphics();
    if(e.getActionCommand().equals("1"))
      {
        g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
        g.fillRect(50,50,60,80);}
    if(e.getActionCommand().equals("2"))
    {
      g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
      g.fillArc(190,50,100,100,30,120);
      }
    if(e.getActionCommand().equals("3"))
    {
      g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
      g.fillOval(50,180,50,50);
      }
    if(e.getActionCommand().equals("4"))
    {
      g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
      g.drawString("Hello",200,200);
      }
    }
}




话题树型展开
人气 标题 作者 字数 发贴时间
7287 请教高手,背景颜色的不间断随机变化 flywithmoon 2471 2004-08-24 22:40
6168 Re:请教高手,背景颜色的不间断随机变化 flywithmoon 14 2004-08-26 09:25
7290 Re:请教高手,背景颜色的不间断随机变化 littledeer1974 2314 2004-08-27 16:24
6025 Re:请教高手,背景颜色的不间断随机变化 flywithmoon 56 2004-08-27 16:55
6990 Re:请教高手,背景颜色的不间断随机变化 littledeer1974 52 2004-08-27 23:20
7101 Re:请教高手,背景颜色的不间断随机变化 littledeer1974 88 2004-08-27 23:25
5972 Re:请教高手,背景颜色的不间断随机变化 flywithmoon 59 2004-08-30 10:39
7065 Re:请教高手,背景颜色的不间断随机变化 littledeer1974 88 2004-08-30 15:43

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923