AWT和Swing
由于本人已经有了很多界面开发经验,对于这块不是重点,只列举一些例子。
JSF和Web Start(jnlp+jar),将逐步替代Swing和Applet的地位。
图形界面开发,步骤一定要记住。
1.选择容器
2.设置布局管理器,Panel默认为Flow,Frame默认为Border
3.添加组件,可能是容器组件的多层嵌套
4.添加时间处理,add***Listener,并给出实现。
事件模型是重点.
import
java.util.
*
;
public class NumberSource {
NumberListener listener=null;
public void addNumberListener(NumberListener nl){
if (listener==null) listener=nl;
}
public void run(){
for(long i=1l;i<=200000l;i++){
if (i%10000==0) {
NumberEvent ne=new NumberEvent(this,(int)(i/10000));
listener. print(ne);
}
}
}
public static void main(String[] args){
NumberSource ns=new NumberSource();
MyNumberListener ls=new MyNumberListener();
ns.addNumberListener(ls);
ns.run();
}
}
class NumberEvent extends EventObject {
int time;
public NumberEvent(Object src,int time){
super(src);
this.time=time;
}
public int getTime(){
return time;
}
}
interface NumberListener extends EventListener {
void print(NumberEvent ne);
}
class MyNumberListener implements NumberListener {
public void print(NumberEvent ne){
System.out.println(ne.getTime()+" 10000");
}
}
public class NumberSource {
NumberListener listener=null;
public void addNumberListener(NumberListener nl){
if (listener==null) listener=nl;
}
public void run(){
for(long i=1l;i<=200000l;i++){
if (i%10000==0) {
NumberEvent ne=new NumberEvent(this,(int)(i/10000));
listener. print(ne);
}
}
}
public static void main(String[] args){
NumberSource ns=new NumberSource();
MyNumberListener ls=new MyNumberListener();
ns.addNumberListener(ls);
ns.run();
}
}
class NumberEvent extends EventObject {
int time;
public NumberEvent(Object src,int time){
super(src);
this.time=time;
}
public int getTime(){
return time;
}
}
interface NumberListener extends EventListener {
void print(NumberEvent ne);
}
class MyNumberListener implements NumberListener {
public void print(NumberEvent ne){
System.out.println(ne.getTime()+" 10000");
}
}
主要部分有事件源,事件对象,该事件的监听器接口,接口实现类。
事件源内部有一个事件监听器接口类型的对象,因为它只关心在相应的事件处理方法,不关心具体类怎么实现的,也不关心具体类的其他属性。
需要建立事件源与监听器之间的联系,通过add***Listener方法搭桥,然后在事件源fire的时候调用监听器方法,得到响应和处理。
下面为实际的基于观察者模式的事件驱动模型。Girl为事件源,如果有情绪波动,它会调用监听她的监听器方法,而由于Girl可以更换男友,那么监听器的实现也会随之变化。
import
java.util.
*
;
public class Girl {
// private ArrayList listeners=new ArrayList();
private EmotionListener listener=null;
private String name;
private int day;
public Girl (String lover){
this.name = lover;
}
public String getLover() { return name; }
public void setEmotionListener(EmotionListener l) throws HeartOccupiedException{
/**//*
if (listeners.size()<10){
listeners.add(l);
}
*/
if (listener==null){
listener=l;
System.out.println("I have a Boy Friend");
}
else {
System.out.println("I already have a bf");
throw new HeartOccupiedException("Go Away!");
}
}
public void removeEmotionListener(EmotionListener l){
/**//*
if(listeners.contains(l)){
System.out.println("I can't live without you");
listeners.remove(l);
}
*/
if (listener==l) {
listener=null;
System.out.println("I can't live without you");
}
}
public void run(){
for(day=1;day<=10;day++){
try{
Thread.sleep(1000);
fire();
}catch(Exception e){}
}
}
private void fire (){
EmotionEvent evt = new EmotionEvent(this);
if(listener == null){
System.out.println("Being a single is lone.");
return;
}
if(day % 2 == 0) {
/**//*
Iterator it=listeners.iterator();
while(it.hasNext()){
EmotionListener l=(EmotionListener)it.next();
l.happy(evt);
}
*/
listener.happy(evt);
}
else{
/**//*
Iterator it=listeners.iterator();
while(it.hasNext()){
EmotionListener l=(EmotionListener)it.next();
l.sad(evt);
}
*/
listener.sad(evt);
}
}
public static void main(String[] args){
Girl girl=new Girl("Anna");
// Girl girl2=new Girl("Marry");
Boy boy1=new Boy("HuXinzhe");
/**//*
Boy boy2=new Boy("LiuChunyang");
Boy boy3=new Boy("LiuXinfu");
Boy boy4=new Boy("ZhaoDekui");
Boy boy5=new Boy("ChenZongquan");
Boy boy6=new Boy("Jerry");
Boy boy7=new Boy("Larry");
Boy boy8=new Boy("George");
Boy boy9=new Boy("WangHaige");
Boy boy10=new Boy("CuiJixin");
*/
try{
girl.setEmotionListener(boy1);
/**//*
girl.setEmotionListener(boy2);
girl.setEmotionListener(boy3);
girl.setEmotionListener(boy4);
girl.setEmotionListener(boy5);
girl.setEmotionListener(boy6);
girl.setEmotionListener(boy7);
girl.setEmotionListener(boy8);
girl.setEmotionListener(boy9);
girl.setEmotionListener(boy10);
*/
// girl2.setEmotionListener(boy1);
// girl.setEmotionListener(boy2);
}
catch(HeartOccupiedException e){
System.out.println(e.getMessage());
}
girl.run();
girl.removeEmotionListener(boy1);
// girl2.run();
// girl2.removeEmotionListener(boy1);
}
}
class EmotionEvent extends java.util.EventObject {
public EmotionEvent(Object src){
super(src);
}
}
interface EmotionListener extends java.util.EventListener {
public void happy(EmotionEvent evt);
public void sad(EmotionEvent evt);
}
class Boy implements EmotionListener {
private String name;
public Boy(String name){ this.name = name;}
public void happy(EmotionEvent evt){
String lover = ((Girl)evt.getSource()).getLover();
System.out.println(name+" said: "+lover + ", I am also happy");
}
public void sad(EmotionEvent evt){
String lover = ((Girl)evt.getSource()).getLover();
System.out.println(name+" said: "+lover + ", take care, you make me sad too.");
}
}
class HeartOccupiedException extends Exception {
public HeartOccupiedException(){
this("No reason");
}
public HeartOccupiedException(String mesg){
super(mesg);
}
}
public class Girl {
// private ArrayList listeners=new ArrayList();
private EmotionListener listener=null;
private String name;
private int day;
public Girl (String lover){
this.name = lover;
}
public String getLover() { return name; }
public void setEmotionListener(EmotionListener l) throws HeartOccupiedException{
/**//*
if (listeners.size()<10){
listeners.add(l);
}
*/
if (listener==null){
listener=l;
System.out.println("I have a Boy Friend");
}
else {
System.out.println("I already have a bf");
throw new HeartOccupiedException("Go Away!");
}
}
public void removeEmotionListener(EmotionListener l){
/**//*
if(listeners.contains(l)){
System.out.println("I can't live without you");
listeners.remove(l);
}
*/
if (listener==l) {
listener=null;
System.out.println("I can't live without you");
}
}
public void run(){
for(day=1;day<=10;day++){
try{
Thread.sleep(1000);
fire();
}catch(Exception e){}
}
}
private void fire (){
EmotionEvent evt = new EmotionEvent(this);
if(listener == null){
System.out.println("Being a single is lone.");
return;
}
if(day % 2 == 0) {
/**//*
Iterator it=listeners.iterator();
while(it.hasNext()){
EmotionListener l=(EmotionListener)it.next();
l.happy(evt);
}
*/
listener.happy(evt);
}
else{
/**//*
Iterator it=listeners.iterator();
while(it.hasNext()){
EmotionListener l=(EmotionListener)it.next();
l.sad(evt);
}
*/
listener.sad(evt);
}
}
public static void main(String[] args){
Girl girl=new Girl("Anna");
// Girl girl2=new Girl("Marry");
Boy boy1=new Boy("HuXinzhe");
/**//*
Boy boy2=new Boy("LiuChunyang");
Boy boy3=new Boy("LiuXinfu");
Boy boy4=new Boy("ZhaoDekui");
Boy boy5=new Boy("ChenZongquan");
Boy boy6=new Boy("Jerry");
Boy boy7=new Boy("Larry");
Boy boy8=new Boy("George");
Boy boy9=new Boy("WangHaige");
Boy boy10=new Boy("CuiJixin");
*/
try{
girl.setEmotionListener(boy1);
/**//*
girl.setEmotionListener(boy2);
girl.setEmotionListener(boy3);
girl.setEmotionListener(boy4);
girl.setEmotionListener(boy5);
girl.setEmotionListener(boy6);
girl.setEmotionListener(boy7);
girl.setEmotionListener(boy8);
girl.setEmotionListener(boy9);
girl.setEmotionListener(boy10);
*/
// girl2.setEmotionListener(boy1);
// girl.setEmotionListener(boy2);
}
catch(HeartOccupiedException e){
System.out.println(e.getMessage());
}
girl.run();
girl.removeEmotionListener(boy1);
// girl2.run();
// girl2.removeEmotionListener(boy1);
}
}
class EmotionEvent extends java.util.EventObject {
public EmotionEvent(Object src){
super(src);
}
}
interface EmotionListener extends java.util.EventListener {
public void happy(EmotionEvent evt);
public void sad(EmotionEvent evt);
}
class Boy implements EmotionListener {
private String name;
public Boy(String name){ this.name = name;}
public void happy(EmotionEvent evt){
String lover = ((Girl)evt.getSource()).getLover();
System.out.println(name+" said: "+lover + ", I am also happy");
}
public void sad(EmotionEvent evt){
String lover = ((Girl)evt.getSource()).getLover();
System.out.println(name+" said: "+lover + ", take care, you make me sad too.");
}
}
class HeartOccupiedException extends Exception {
public HeartOccupiedException(){
this("No reason");
}
public HeartOccupiedException(String mesg){
super(mesg);
}
}
|
|