分享
 
 
 

一种新的设计方式

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

afritxia.student@sina.com

以往,我们的Java应用程序(特别是主窗体程序部分)都是继承自javax.swing.JFrame类.然后在里面加入菜单,工具条,状态条及用户区,当然还要加入事件倾听和相应的处理函数...随着软件功能的增加,大量的程序段让程序变得不易分析,除错,维护和更新...就用我的记事本程序举例,如果开发新版本的软件,我们可以这样吗?

public class jNotepad2.0 extends jNotepad1.0{

...

}

理论上是可以的,但在现实中我从未见过有人这样做过(因为用这样的方法扩充功能是非常麻烦的).有的只是从旧版本的copy,再paste到新版中去,然后再进行添减修改...

需要更新的很多,最当先的是设计方式!我就用一个及简单的例子说明我的观点:

import java.awt.event.*;

import javax.swing.*;

public class MainFrame extends JFrame

{

public MainFrame(){

super("Main");

setSize(480, 320);

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); // 注意!

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

onClosing();

}

});

}

public void onClosing(){

// 关闭窗口前可能要有许多重要操作!固,以函数代之...

System.exit(0);

}

// 其它程序段...

public static void main(String[] args){

MainFrame App=new MainFrame();

App.show();

}

}

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE), addWindowListener()几乎是每一个完整的应用程序都应该具备的.那么我就通过某种途径把它们和窗体死捆在一起,只要我用到这个自定义的窗体,就不用重复上述操作!

package com.afr;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class AfrFrame extends JFrame

{

public AfrFrame(String title)

{

super(title);

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

onClosing();

}

});

}

// 倒是解决了重复问题,但有个问题:onClosing()函数怎么办?因为在用自定义窗体分配新的对象时,onClosing()

// 函数可能会有不同的操作...所以我只能这样

public void onClosing(){ // 空空的,什么也不做.我不可能预见未来的事!

}

}

我们可以再次继承AfrFrame类(这也是我的目的),然后改写onClosing()函数!

import java.awt.event.*;

import javax.swing.*;

import com.afr.*;

public class MainFrame2 extends AfrFrame

{

public MainFrame2(){

super("MainFrame2");

setSize(480, 320);

}

public void onClosing(){

System.exit(0);

}

public static void main(String[] args){

MainFrame2 App=new MainFrame2();

App.show();

}

}

你也看到了,我并没有在MainFrame2中添加任何事件倾听函数(因为父类已经具备了事件倾听的能力),只是简单的改写了onClosing()函数.当然,程序很简单,所以还看不出什么.但是程序加大,你就知道了...

package com.afr;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class AfrFrame extends JFrame

{

JMenuBar mainMenu=new JMenuBar();

JMenu file=new JMenu("File");

JMenuItem newf=new JMenuItem("New File");

JMenuItem open=new JMenuItem("Open ...");

JMenuItem exit=new JMenuItem("Exit");

public AfrFrame(String title)

{

super(title);

initMainMenu();

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

onClosing();

}

});

}

public void initMainMenu(){

newf.addActionListener(myAction);

file.add(newf);

open.addActionListener(myAction);

file.add(open);

file.addSeparator();

exit.addActionListener(myAction);

file.add(exit);

mainMenu.add(file);

setJMenuBar(mainMenu);

}

public void onNew(){

}

public void onOpen(){

}

public void onExit(){

onClosing();

}

public void onClosing(){

}

protected class MyAction extends AbstractAction

{

public void actionPerformed(ActionEvent e){

String cmd=e.getActionCommand();

if(cmd.equals("New File"))

onNew();

else if(cmd.equals("Open ..."))

onOpen();

else if(cmd.equals("Exit"))

onExit();

}

}

public final MyAction myAction=new MyAction();

}

/////////////////////////////////////////////////////////////////////////////////////////

import java.awt.event.*;

import javax.swing.*;

import com.afr.*;

public class MainFrame2 extends AfrFrame

{

public MainFrame2(){

super("MainFrame2");

setSize(480, 320);

}

public void onNew(){

System.out.println("Create New");

}

public void onClosing(){

System.exit(0);

}

public static void main(String[] args){

MainFrame2 App=new MainFrame2();

App.show();

}

}

我只改写了onNew()和onClosing()两个函数.这样MainFrame2看起来是否简单了许多?当然现在还存在一个问题:动态添加菜单项怎么办?我正在试图解决!(借助XML怎么样?)

To be continued ...

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有