Topic: 怎么回事呀!快来帮忙呀!

  Print this page

1.怎么回事呀!快来帮忙呀! Copy to clipboard
Posted by: kudo
Posted on: 2004-10-16 20:34

这个问题困扰我好几天了~拜托帮帮忙~
import java.awt.*;
import java.awt.event.*;
public class Change implements AdjustmentListener
{  
  static Frame fra=new Frame("改变颜色");
  static Scrollbar scrRed=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,255);
  static Scrollbar scrGreen=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
  static Scrollbar scrBlue=new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);
  static Button btn=new Button();
  public static void main(String[] args)
  {  //设定各Scrollbar的初始植,取的是按钮btn的底色
    try{
    scrRed.setValue(btn.getBackground().getRed());
    scrGreen.setValue(btn.getBackground().getGreen());
    scrBlue.setValue(btn.getBackground().getBlue());}
    catch(Exception e)
    {};
    //注册事件监听器
    fra.addWindowListener(new MyWindowEvent());
    scrRed.addAdjustmentListener(new Change());
    scrGreen.addAdjustmentListener(new Change());
    scrBlue.addAdjustmentListener(new Change());
    fra.setLocation(100,100);
    //设定各控件的位置和大小
    btn.setBounds(120,120,150,150);
    scrRed.setBounds(120,50,150,30);
    scrGreen.setBounds(65,120,30,150);
    scrBlue.setBounds(300,120,30,150);
    //将各控件添加到fra上
    fra.add(scrRed);
    fra.add(scrGreen);
    fra.add(scrBlue);
    fra.add(btn);
    fra.setLayout(null);
    fra.setSize(400,350);
    fra.setVisible(true);
  }
  public void adjustmentValueChanged(AdjustmentEvent e)
  {  //使btn的底色随Scrollbar的变化而变化
    btn.setBackground(new Color(scrRed.getValue(),scrGreen.getValue(),scrBlue.getValue()));
  }
}  
class MyWindowEvent extends WindowAdapter
{
  public void windowClosing(WindowEvent e)
  {
    System.exit(0);
  }
}
/*
如果把try catch去掉,编译能通过,但运行出现以下错误:
java.lang.NullPointerException
  at Change.main(Change.java:12)
Exception in thread "main" Normal Termination
好像是null的异常~这个问题怎么解决呀?拜托了!!!!!
*/

2.Re:怎么回事呀!快来帮忙呀! [Re: kudo] Copy to clipboard
Posted by: kavinwang
Posted on: 2004-10-16 20:54

btn此时并没有设置背景,所以btn.getBackground()为null。
你可以先设置btn的背景,Color.White

3.Re:怎么回事呀!快来帮忙呀! [Re: kudo] Copy to clipboard
Posted by: kudo
Posted on: 2004-10-16 20:58

好人呀!!!!谢谢kavinwang,怎么给你加分~~!!!

4.Re:怎么回事呀!快来帮忙呀! [Re: kudo] Copy to clipboard
Posted by: kavinwang
Posted on: 2004-10-16 21:01

我也不知道怎样加分,加的分有什么用处?

5.Re:怎么回事呀!快来帮忙呀! [Re: kudo] Copy to clipboard
Posted by: nothing
Posted on: 2004-10-16 21:10

关于加分,可以参看站务版.

6.Re:怎么回事呀!快来帮忙呀! [Re: kudo] Copy to clipboard
Posted by: kavinwang
Posted on: 2004-10-17 15:12

无所谓啦,如果觉得可以加分,就加啦,反正我的目的不在此。


   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