Java语言程序设计基础
public class T17_4 extends Frame implements AdjustmentListener{ Scrollbar red,green,blue; TextArea textArea1=new TextArea(\测试区域\
Panel p1,p2,p3; Panel p;
public T17_4(){ super(\调整事件示例\
p=new Panel(); p1=new Panel(); p2=new Panel(); p3=new Panel();
p.setLayout(new GridLayout(3,1,5,5)); p1.setLayout(new GridLayout(1,1,20,10)); p2.setLayout(new GridLayout(1,1,20,10)); p3.setLayout(new GridLayout(1,1,20,10));
red=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,255); green=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,255); blue=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,255); p1.add(red); p2.add(green); p3.add(blue); p.add(p1); p.add(p2); p.add(p3);
textArea1.setBackground(new Color(0,0,0)); add(textArea1,BorderLayout.NORTH); add(p,BorderLayout.CENTER); red.addAdjustmentListener(this); green.addAdjustmentListener(this); blue.addAdjustmentListener(this);
addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0);
294
附录 实训参考答案
}
}
} });
setSize(400,300);
//implementation of AdjustmentListener
public void adjustmentValueChanged(AdjustmentEvent e){ int r=red.getValue(); int g=green.getValue(); int b=blue.getValue(); textArea1.setBackground(new Color(r,g,b)); }
public static void main(String[] args){ new T17_4().setVisible(true); }
295