Topic: 请教高手,背景颜色的不间断随机变化 |
Print this page |
1.请教高手,背景颜色的不间断随机变化 | Copy to clipboard |
Posted by: flywithmoon Posted on: 2004-08-24 22:40 以下代码无法实现面板背景颜色的不间断随机变化,请教高手如何修改: 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); } } } |
2.Re:请教高手,背景颜色的不间断随机变化 [Re: flywithmoon] | Copy to clipboard |
Posted by: flywithmoon Posted on: 2004-08-26 09:25 斑竹哪里去了,怎么没人管了。 |
3.Re:请教高手,背景颜色的不间断随机变化 [Re: flywithmoon] | Copy to clipboard |
Posted by: littledeer1974 Posted on: 2004-08-27 16:24 [code] import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; class question01 extends JFrame { question01() { this.getContentPane().add(new MyPanel()); } public static void main(String args[]) { question01 f = new question01(); f.setSize(300, 300); f.show(); } } class MyPanel extends JPanel implements Runnable { JButton jb1, jb2, jb3, jb4; JPanel panel1, panel2, panel3, panel4; static int index = 0; Container cont; MyPanel() { // panel1 = new JPanel(); panel2 = new JPanel(); panel3 = new JPanel(); panel4 = new JPanel(); // jb1 = new JButton("1"); jb2 = new JButton("2"); jb3 = new JButton("3"); jb4 = new JButton("4"); // panel1.add(jb1); panel2.add(jb2); panel3.add(jb3); panel4.add(jb4); setLayout(new GridLayout(2, 2)); this.setNewColor(); this.add(panel1); this.add(panel2); this.add(panel3); this.add(panel4); Thread thread = new Thread(this); thread.start(); } public void setNewColor() { // panel1.setBackground(new Color((int) (Math.random() * 255), (int) (Math .random() * 255), (int) (Math.random() * 255))); panel2.setBackground(new Color((int) (Math.random() * 255), (int) (Math .random() * 255), (int) (Math.random() * 255))); panel3.setBackground(new Color((int) (Math.random() * 255), (int) (Math .random() * 255), (int) (Math.random() * 255))); panel4.setBackground(new Color((int) (Math.random() * 255), (int) (Math .random() * 255), (int) (Math.random() * 255))); } public void run() { while (true) { try { Thread.sleep(150); this.setNewColor(); System.out.print("background color is changed!\n"); } catch (Exception e) { e.printStackTrace(); } } } }[/code] 不知道这个是不是你想要的效果 |
4.Re:请教高手,背景颜色的不间断随机变化 [Re: flywithmoon] | Copy to clipboard |
Posted by: flywithmoon Posted on: 2004-08-27 16:55 请教一下,进程为什么必须放到主面板的构造里面,不能够放到主函数main里面,书上面就是放在main()里面的呀。 |
5.Re:请教高手,背景颜色的不间断随机变化 [Re: flywithmoon] | Copy to clipboard |
Posted by: littledeer1974 Posted on: 2004-08-27 23:20 进程放到哪里都可以呀,看你怎么设计了,上边我觉得拿出来, 整个程序结构会清晰一点,并不是什么规定,我想 |
6.Re:请教高手,背景颜色的不间断随机变化 [Re: flywithmoon] | Copy to clipboard |
Posted by: littledeer1974 Posted on: 2004-08-27 23:25 补充以下,要改变的是Panel的属性,所以应该放到MyPanel Class里比较好,放在主程序里,不太合适,因为主程序里还有生成实例的任务,那样有可能和Swing的线程冲突。 |
7.Re:请教高手,背景颜色的不间断随机变化 [Re: flywithmoon] | Copy to clipboard |
Posted by: flywithmoon Posted on: 2004-08-30 10:39 谢谢了,是不是说在test类的Thread t=new Thread(this);中的this要换成new myp() |
8.Re:请教高手,背景颜色的不间断随机变化 [Re: flywithmoon] | Copy to clipboard |
Posted by: littledeer1974 Posted on: 2004-08-30 15:43 不是,你在参考一下上边给你的程序,县程的生成有两种方式,这是一种implemets Runnable interface 初学时理解起来有点怪,你还可以多参照一些参考书,加油哦 |
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 |