如何将界面设计和具体事件操作的代码分开? 根据需要吧,看看mvc的模式好了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 UI类可以采用单例模式,UI类里面可以包括:一个私有的构造函数,这样可以控制外界无法对UI类实例化一个私有的静态的UI对象,UI的对象可以自己实例化,这样便于管理一个静态的工厂方法(public static),将那个UI对象提供给外界使用。action类只要有一个UI类的接口成员就可以了这样你修改UI类就不会影响ACTION类而且别的类都可以使用UI类,实现了OPEN-CLOSE原则 不明白maddingxu(蓝牙剑客)的内容,可否有实际代码? /* * JmvcApp * Created on 2005-2-11 * Version 1.1 * Copyright (c) 2005, Jeffrey Xu */package com.jeffreyxu.frame;import java.awt.*;import javax.swing.*;/** * @author Jeffrey Xu * @version 1.1 * Jmvc 的主应用程序类 */public abstract class JmvcApp { private static JmvcApp theApp = null; // Singleton //模型 protected static JmvcModel theModel = null; //GUI界面部分 private static JFrame theFrame = null; // 顶层窗体 private static JMenuBar theMenuBar = null; private static JToolBar theToolBar = null; private static JPanel theContentPanel = null; public JmvcApp(String aName, boolean cMenu, boolean cTool) { if (theApp != null) return; theApp = this; initializeJmvc(aName, cMenu, cTool); } private void initializeJmvc(String aName, boolean cMenu, boolean cTool) { // 建立主JFrame theFrame = new JFrame(aName); theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 建立菜单栏 if (cMenu) { theMenuBar = new JMenuBar(); theFrame.setJMenuBar(theMenuBar); } // JPanel theContentPanel = new JPanel(); theContentPanel.setLayout(new BorderLayout()); theContentPanel.setPreferredSize(new Dimension(400, 300)); if (cTool) { theToolBar = new JToolBar(); theContentPanel.add(theToolBar, BorderLayout.NORTH); } theFrame.setContentPane(theContentPanel); } public static void addMainPane(JComponent pane) { } public static void addMenu(JMenu menu) { if (theMenuBar == null) return; theMenuBar.add(menu); } public static JFrame getFrame() { return theFrame; } public static JMenuBar getMenuBar() { return theMenuBar; } public static JToolBar getToolBar() { return theToolBar; } public static JPanel getContentPanel() { return theContentPanel; } public static JmvcModel getModel() { return theModel; } public static void setModel(JmvcModel m) { theModel = m; } public static JmvcApp getApp() { return theApp; } public static void showApp() { theFrame.pack(); theFrame.setVisible(true); } public boolean appClosing() { return true; } } /* * JmvcView.java * * Created on 2005-2-13 * Version 1.1 * Copyright (c) 2005, Jeffrey Xu */package com.jeffreyxu.frame;import java.util.*;/** * @author Jeffrey Xu * @version 1.1 */public class JmvcView implements Observer { public void update(Observable observed, Object value) { updateView(); } public void updateView() { }} /* * JmvcModel * Created on 2005-2-13 * Version 1.1 * Copyright (c) 2005, Jeffrey Xu */package com.jeffreyxu.frame;import java.util.*;/** * @author Jeffrey Xu * @version 1.1 * Jmvc Model 基类 */public class JmvcModel extends Observable { public JmvcModel() { super(); } public void addView(JmvcView view) { addObserver((Observer) view); } public void deleteView(JmvcView view) { deleteObserver((Observer) view); } public void notifyViews() { setChanged(); notifyObservers(); }} 麻烦帮忙加入异常处理操作。 Java Thread 如何获得网页中指定的文本的字号?(100分) 看书遇到的问题! 悬赏100分:穷举任意给定字符集中的字符所能组成的全部字符串问题 Desktop.open(new File("***")); [在线等]如何将MSSQL驱动导入Myeclips工程 JRadioButton的问题 通过WWW.HAOSHUA.COM 成功访问GOOGLE 纯新手求救 一个换码符“\”的问题 穿越代理服务器
UI类里面可以包括:
一个私有的构造函数,这样可以控制外界无法对UI类实例化
一个私有的静态的UI对象,UI的对象可以自己实例化,这样便于管理
一个静态的工厂方法(public static),将那个UI对象提供给外界使用。action类只要有一个UI类的接口成员就可以了这样你修改UI类就不会影响ACTION类
而且别的类都可以使用UI类,
实现了OPEN-CLOSE原则
* JmvcApp
* Created on 2005-2-11
* Version 1.1
* Copyright (c) 2005, Jeffrey Xu
*/
package com.jeffreyxu.frame;import java.awt.*;
import javax.swing.*;/**
* @author Jeffrey Xu
* @version 1.1
* Jmvc 的主应用程序类
*/
public abstract class JmvcApp {
private static JmvcApp theApp = null; // Singleton
//模型
protected static JmvcModel theModel = null;
//GUI界面部分
private static JFrame theFrame = null; // 顶层窗体
private static JMenuBar theMenuBar = null;
private static JToolBar theToolBar = null;
private static JPanel theContentPanel = null;
public JmvcApp(String aName, boolean cMenu, boolean cTool) {
if (theApp != null)
return;
theApp = this;
initializeJmvc(aName, cMenu, cTool);
}
private void initializeJmvc(String aName, boolean cMenu, boolean cTool) {
// 建立主JFrame
theFrame = new JFrame(aName);
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 建立菜单栏
if (cMenu) {
theMenuBar = new JMenuBar();
theFrame.setJMenuBar(theMenuBar);
}
// JPanel
theContentPanel = new JPanel();
theContentPanel.setLayout(new BorderLayout());
theContentPanel.setPreferredSize(new Dimension(400, 300));
if (cTool) {
theToolBar = new JToolBar();
theContentPanel.add(theToolBar, BorderLayout.NORTH);
}
theFrame.setContentPane(theContentPanel);
}
public static void addMainPane(JComponent pane) {
}
public static void addMenu(JMenu menu) {
if (theMenuBar == null)
return;
theMenuBar.add(menu);
}
public static JFrame getFrame() {
return theFrame;
}
public static JMenuBar getMenuBar() {
return theMenuBar;
}
public static JToolBar getToolBar() {
return theToolBar;
}
public static JPanel getContentPanel() {
return theContentPanel;
}
public static JmvcModel getModel() {
return theModel;
}
public static void setModel(JmvcModel m) {
theModel = m;
}
public static JmvcApp getApp() {
return theApp;
}
public static void showApp() {
theFrame.pack();
theFrame.setVisible(true);
}
public boolean appClosing() {
return true;
}
}
* JmvcView.java
*
* Created on 2005-2-13
* Version 1.1
* Copyright (c) 2005, Jeffrey Xu
*/
package com.jeffreyxu.frame;import java.util.*;/**
* @author Jeffrey Xu
* @version 1.1
*/public class JmvcView implements Observer {
public void update(Observable observed, Object value) {
updateView();
} public void updateView() { }
}
* JmvcModel
* Created on 2005-2-13
* Version 1.1
* Copyright (c) 2005, Jeffrey Xu
*/
package com.jeffreyxu.frame;import java.util.*;/**
* @author Jeffrey Xu
* @version 1.1
* Jmvc Model 基类
*/public class JmvcModel extends Observable {
public JmvcModel() {
super();
}
public void addView(JmvcView view) {
addObserver((Observer) view);
}
public void deleteView(JmvcView view) {
deleteObserver((Observer) view);
}
public void notifyViews() {
setChanged();
notifyObservers();
}
}