`
phinecos
  • 浏览: 343536 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

《Java编程思想》读书笔记(7)

阅读更多
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Swing的确是MVC模式的一个优秀例子,它将接口(图形组件)和实现(当组件发生了某个事件之后,你要运行的代码)明明白白地分开来。Swing组件能通报在它身上可以发生什么事件,以及发生了什么事件。所以,如果你对某个事件不感兴趣,比如鼠标从按钮的上方经过,你完全可以不去理会这个事件。用这种方法能非常简洁优雅地解决事件驱动的问题,一旦你理解了其基本概念,你甚至可以去直接使用过去从未看到过的Swing组件。

为了对事件驱动编程进行学习,我们还是从一个简单的HelloWorld开始说起好了,正所谓“麻雀虽小,可五脏俱全”,越是细小的东西反而更能引人思考。不废话,先上代码(^o^

/**//////////////////////////////////////////////////////////////////////////////////////////////
////使用内部匿名类
/////////////////////////////////////////////////////////////////////////////////////////////

packagecom.vitamin.UI;

importjava.awt.BorderLayout;
importjava.awt.Container;
importjava.awt.Event;
importjava.awt.
event.ActionEvent;
importjava.awt.
event.ActionListener;

importjavax.swing.
*;

publicclassHelloFormextendsJFrame
{
privateJLabellbInfo=null;
privateJButtonbtnOK=null;

publicHelloForm()
{
super();
}

publicHelloForm(Stringtitle)
{
super(title);
this.initForm();
}


privatevoidinitForm()
{
this.lbInfo=newJLabel();
this.btnOK=newJButton("确定");
this.btnOK.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente)
{
lbInfo.setText(
"Hello,World");
}

}
);


Containercon
=this.getContentPane();
con.setLayout(
newBorderLayout());
con.add(
this.btnOK,BorderLayout.SOUTH);
con.add(
this.lbInfo,BorderLayout.NORTH);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
this.setVisible(true);
}

/**//**
*@paramargs
*/

publicstaticvoidmain(String[]args)
{
HelloFormhf
=newHelloForm("内部匿名类测试程序");

}


}

/**////////////////////////////////////////////////////
////////使用内部类
//////////////////////////////////////////////////

packagecom.vitamin.UI;
importjava.awt.BorderLayout;
importjava.awt.Container;
importjava.awt.
event.ActionEvent;
importjava.awt.
event.ActionListener;

importjavax.swing.
*;

publicclassHelloForm2extendsJFrame
{
privateJLabellbInfo=null;
privateJButtonbtnOK=null;

publicHelloForm2()
{
super();
//TODOAuto-generatedconstructorstub
}

publicHelloForm2(Stringtitle)
{
super(title);
this.initForm();
}


privatevoidinitForm()
{
this.lbInfo=newJLabel();
this.btnOK=newJButton("确定");
this.btnOK.addActionListener(newbuttonListener());


Containercon
=this.getContentPane();
con.setLayout(
newBorderLayout());
con.add(
this.btnOK,BorderLayout.SOUTH);
con.add(
this.lbInfo,BorderLayout.NORTH);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
this.setVisible(true);
}

classbuttonListenerimplementsActionListener
{
publicvoidactionPerformed(ActionEvente)
{
lbInfo.setText(
"Hello,World");
}

}

/**//**
*@paramargs
*/

publicstaticvoidmain(String[]args)
{
HelloForm2hf
=newHelloForm2("内部类测试");

}


}

这两种代码的写法都是可以的,但我觉得内部匿名类在这更合适些。不过话说回来,呵呵,AndewJames戏称为“函数指针先生”,但依我看javaC#的事件处理机制还是有些相似的,至于说谁更加优雅,这就很难说了。。
此外,Swing的功能非常强大,短短几行代码就能做很多事。但是,有些时候用手写代码创建GUI表单就不那么明智了;首先是太复杂,其次也不值得。Java和Swing的设计者们是想用语言和类库去支持GUI builder工具,然后让你用工具来简化编程。实际上只要知道布局是怎么一回事,以及事件该怎么处理就行了,懂不懂手写代码控制控件的摆放,其实并不重要。你完全可以选一个趁手的工具(毕竟Java的初衷就是想让你提高编程的效率)。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics