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

本文介绍了Swing作为MVC模式的优秀实例,探讨了如何通过事件驱动编程处理图形用户界面交互。通过两个示例代码展示了如何使用内部匿名类和内部类为Swing组件添加事件监听器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

ExpandedBlockStart.gifContractedBlock.gif/**//////////////////////////////////////////////////////////////////////////////////////////////
InBlock.gif
////使用内部匿名类
ExpandedBlockEnd.gif
/////////////////////////////////////////////////////////////////////////////////////////////

None.gifpackagecom.vitamin.UI;
None.gif
None.gifimportjava.awt.BorderLayout;
None.gifimportjava.awt.Container;
None.gifimportjava.awt.Event;
None.gifimportjava.awt.
event.ActionEvent;
None.gifimportjava.awt.
event.ActionListener;
None.gif
None.gifimportjavax.swing.
*;
None.gif
None.gif
publicclassHelloFormextendsJFrame
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
privateJLabellbInfo=null;
InBlock.gif
privateJButtonbtnOK=null;
InBlock.gif
InBlock.gif
publicHelloForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsuper();
ExpandedSubBlockEnd.gif}

InBlock.gif
publicHelloForm(Stringtitle)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsuper(title);
InBlock.gif
this.initForm();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
privatevoidinitForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.lbInfo=newJLabel();
InBlock.gif
this.btnOK=newJButton("确定");
ExpandedSubBlockStart.gifContractedSubBlock.gif
this.btnOK.addActionListener(newActionListener()dot.gif{
InBlock.gif
publicvoidactionPerformed(ActionEvente)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giflbInfo.setText(
"Hello,World");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}
);
InBlock.gif
InBlock.gif
InBlock.gifContainercon
=this.getContentPane();
InBlock.gifcon.setLayout(
newBorderLayout());
InBlock.gifcon.add(
this.btnOK,BorderLayout.SOUTH);
InBlock.gifcon.add(
this.lbInfo,BorderLayout.NORTH);
InBlock.gif
InBlock.gif
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
InBlock.gif
this.setSize(300,300);
InBlock.gif
this.setVisible(true);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**
InBlock.gif*@paramargs
ExpandedSubBlockEnd.gif
*/

InBlock.gif
publicstaticvoidmain(String[]args)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifHelloFormhf
=newHelloForm("内部匿名类测试程序");
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif/**////////////////////////////////////////////////////
InBlock.gif
////////使用内部类
ExpandedBlockEnd.gif
//////////////////////////////////////////////////

None.gifpackagecom.vitamin.UI;
None.gifimportjava.awt.BorderLayout;
None.gifimportjava.awt.Container;
None.gifimportjava.awt.
event.ActionEvent;
None.gifimportjava.awt.
event.ActionListener;
None.gif
None.gifimportjavax.swing.
*;
None.gif
None.gif
publicclassHelloForm2extendsJFrame
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
privateJLabellbInfo=null;
InBlock.gif
privateJButtonbtnOK=null;
InBlock.gif
InBlock.gif
publicHelloForm2()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsuper();
InBlock.gif
//TODOAuto-generatedconstructorstub
ExpandedSubBlockEnd.gif
}

InBlock.gif
publicHelloForm2(Stringtitle)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsuper(title);
InBlock.gif
this.initForm();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
privatevoidinitForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
this.lbInfo=newJLabel();
InBlock.gif
this.btnOK=newJButton("确定");
InBlock.gif
this.btnOK.addActionListener(newbuttonListener());
InBlock.gif
InBlock.gif
InBlock.gifContainercon
=this.getContentPane();
InBlock.gifcon.setLayout(
newBorderLayout());
InBlock.gifcon.add(
this.btnOK,BorderLayout.SOUTH);
InBlock.gifcon.add(
this.lbInfo,BorderLayout.NORTH);
InBlock.gif
InBlock.gif
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
InBlock.gif
this.setSize(300,300);
InBlock.gif
this.setVisible(true);
ExpandedSubBlockEnd.gif}

InBlock.gif
classbuttonListenerimplementsActionListener
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
publicvoidactionPerformed(ActionEvente)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giflbInfo.setText(
"Hello,World");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**
InBlock.gif*@paramargs
ExpandedSubBlockEnd.gif
*/

InBlock.gif
publicstaticvoidmain(String[]args)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifHelloForm2hf
=newHelloForm2("内部类测试");
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值