java的设计思想


(设计思想。是须要不断领悟的。。

一、封装

学java的人都知道这是向对象的编程语言,从字面上理解,就是针对对象的一些操作,将具有某一特性的实体封装成一个类或者是将具有一定功能的方法,抽取出来封装成一个供外面调用的方法,然后通过new这个类或方法生成对象,最后通

过对对象进行操作或者实现对应的功能。

如将一个人封装成一个人的类。人有属性:性别,年龄,出生日期。家庭住址等,外界对人进行操作时。仅仅能改变其属性值。可是不能改变属性的种类,这要更安全。并且当对人进行操作时。也是连带属性

一起进行操作,这就是面向对象的编程。

所以java的设计思想一般离不开封装。如做一个项目,首先对项目的每一个子系统进行封装。然后对子系统中的每一个模块进行封装,最后对每一个模块中的每一个组件(组件:能完毕某一特定功能的方法)进行封装,这里的子系统、模块、组件都是

一个封装体。这样做不仅能使项目做起来更加有条理。更加安全,并且让后面的维护和更改更加方便、简单、易操作。

(后面有项目的实例)


二、设计的基本原则

★ 面向接口编程

a、这是Java编程里面大家公认的第一原则。

b、接口非常重要的一个功能:封装隔离

c、接口是功能块或封装块的外观

接口及相关机制最主要的作用:通过接口能够实现不相关类的同样行为,而不须要考虑这些类之间的层次关系。

依据接口能够了解对象的交互界面,而不须要了解对象所属的类。

面向对象程序设计讲究“提高内聚,松散耦合”,那么不同的程序模块怎么相互訪问呢,就是通过接口。也就是接口是各部分对外的统一外观。接口在Java程序设计中体现的思想就是封装隔离,由于接口仅仅是描写叙述一个统一的行为,所以开发者在面

向接口编程时并不关心详细的实现。

由以上讲到的接口的作用和基本思想能够看到。接口在面向对象的Java程序设计中占有举足轻重的地位。其实在设计阶段最重要的任务之中的一个就是设计出各部分的接口。然后通过接口的组合。形成程序的基本框架结构。


注:对于行为模型,优先选用接口,尽量少用抽象类

既为约束子类的功能,又要提供一些公共的功能。就选用抽象类

★ 优先使用对象组合而非类继承

★ 分层

最典型的:三层结构

java之 ------ 设计思想_封装

① 表现层功能:展示数据、人机交互、收集參数调用逻辑层。


② 逻辑层功能:进行数据的逻辑校验、进行逻辑推断、实现业务功能、处理相关功能、处理兴许流程、组织数据返回给表现层。


③ 数据层功能:实现数据持久化、实现对象和持久化数据的双向映射。

★ 层间交互的基本原则

① 表现层调用逻辑层,逻辑层调用数据层。不能够反过来。

② 层间交互也应该通过接口进行调用,以确保各层的实现独立变化。

★ 开闭原则

简单点说就是对新增开放。对改动关闭。并且应尽量做到不用改动模块的源码,就能更改模块的行为。


★ 依赖性倒置原则

这个原则就是依赖抽象而不要依赖详细实现。


★ 接口隔离原则

这个原则就是不要使用通用的接口。而是为不同的用户使用不同的接口。


★ 替换原则

这个原则就是子类应当可以替换父类并出如今父类可以出现的不论什么地方。



三、大神们的基本经验

★ 类要单一

所设计的类功能要单一 。一个类应该是描写叙述一类极其相关的属性和方法。 

★ 加强内聚。松散耦合

指一个类。可以在自己内部实现的功能就自己内部实现,不要去调其他类,尽量降低与其他类的依赖关系。


★ 好的封装性 

不必对外开放的部分,一律不正确外开放。假设在不确定属性权限的情况下,先把类的属性设成private,用的时候再开。

★ 类的粒度要合理

Java规范中建议。一个类的大小不要超过1000行,一个方法的大小不要超过100行。

★ 实现类不能依赖它的使用类

一个类依赖(使用或调用)了还有一个类,那么在还有一个类中就不能反过来再依赖当前类。

★ 应考虑灵活性,也就是可配置、可维护 

用户的需求是会不断改变的,后期的改动与维护是不可避免的。因此软件开发时就要考虑灵活性。

★ 要考虑性能,考虑可伸缩性

要权衡速度与资源耗费之间的关系。要考虑软件可以在一定范围内可伸缩。

★ 要考虑今后可能的变化。也就是可扩展性 

可扩展性指该应用程序可以非常方便地加入新的功能。

★ 要考虑合理的复用 

要尽量考虑复用,但也不能光考虑复用,不能把其他的设计原则都丢掉了。

★ 要合理的考虑接口和抽象类的使用 

优先使用接口,假设既想约束子类的行为,又要为子类提供公共的功能时,採用抽象类。

★ 尽量降低类与协作类的交互次数和交互信息的量

类之间不要重复地调用。不要每次传非常多的数据。

★ 父类不应知道子类的信息。子类必须知道父类的信息 

★ 訪问对象必须通过接口,不能绕过接口直接去訪问 


四、实例

功能:图形界面的个人信息管理系统,实现增、删、改、查等功能,并且将相关信息存在文件里。

设计思想:每一个功能一个模块。也就是一个封装体。

表现层:

addressApp.java(主界面)

设置界面的大体框架。如菜单,和面板。标题等

package cn.hncu.addressManage;

import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Event;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.KeyStroke;
import java.awt.Point;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JDialog;

import cn.hncu.addressManage.UI.ListPanel;


public class addressApp {

private JFrame jFrame = null;
private JPanel jContentPane = null;
private JMenuBar jJMenuBar = null;
private JMenu fileMenu = null;
private JMenu editMenu = null;
private JMenu helpMenu = null;
private JMenuItem exitMenuItem = null;
private JMenuItem aboutMenuItem = null;
private JMenuItem cutMenuItem = null;
private JMenuItem copyMenuItem = null;
private JMenuItem pasteMenuItem = null;
private JMenuItem saveMenuItem = null;
private JDialog aboutDialog = null;
private JPanel aboutContentPane = null;
private JLabel aboutVersionLabel = null;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
addressApp application = new addressApp();
application.getJFrame().setVisible(true);
}
});
}

/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setJMenuBar(getJJMenuBar());
jFrame.setSize(800, 650);
jFrame.setContentPane(getJContentPane());
jFrame.setTitle("Application");
}
return jFrame;
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
//把我们的ListPanel加到jContentPane中
jContentPane.add(new ListPanel(jFrame,null,false));
//jContentPane.remove(listPanel)
}
return jContentPane;
}

/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.add(getFileMenu());
jJMenuBar.add(getEditMenu());
jJMenuBar.add(getHelpMenu());
}
return jJMenuBar;
}

/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getFileMenu() {
if (fileMenu == null) {
fileMenu = new JMenu();
fileMenu.setText("File");
fileMenu.add(getSaveMenuItem());
fileMenu.add(getExitMenuItem());
}
return fileMenu;
}

/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getEditMenu() {
if (editMenu == null) {
editMenu = new JMenu();
editMenu.setText("Edit");
editMenu.add(getCutMenuItem());
editMenu.add(getCopyMenuItem());
editMenu.add(getPasteMenuItem());
}
return editMenu;
}

/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getHelpMenu() {
if (helpMenu == null) {
helpMenu = new JMenu();
helpMenu.setText("Help");
helpMenu.add(getAboutMenuItem());
}
return helpMenu;
}

/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getExitMenuItem() {
if (exitMenuItem == null) {
exitMenuItem = new JMenuItem();
exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
return exitMenuItem;
}

/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getAboutMenuItem() {
if (aboutMenuItem == null) {
aboutMenuItem = new JMenuItem();
aboutMenuItem.setText("About");
aboutMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog aboutDialog = getAboutDialog();
aboutDialog.pack();
Point loc = getJFrame().getLocation();
loc.translate(20, 20);
aboutDialog.setLocation(loc);
aboutDialog.setVisible(true);
}
});
}
return aboutMenuItem;
}

/**
* This method initializes aboutDialog
*
* @return javax.swing.JDialog
*/
private JDialog getAboutDialog() {
if (aboutDialog == null) {
aboutDialog = new JDialog(getJFrame(), true);
aboutDialog.setTitle("About");
aboutDialog.setContentPane(getAboutContentPane());
}
return aboutDialog;
}

/**
* This method initializes aboutContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getAboutContentPane() {
if (aboutContentPane == null) {
aboutContentPane = new JPanel();
aboutContentPane.setLayout(new BorderLayout());
aboutContentPane.add(getAboutVersionLabel(), BorderLayout.CENTER);
}
return aboutContentPane;
}

/**
* This method initializes aboutVersionLabel
*
* @return javax.swing.JLabel
*/
private JLabel getAboutVersionLabel() {
if (aboutVersionLabel == null) {
aboutVersionLabel = new JLabel();
aboutVersionLabel.setText("Version 1.0");
aboutVersionLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
return aboutVersionLabel;
}

/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getCutMenuItem() {
if (cutMenuItem == null) {
cutMenuItem = new JMenuItem();
cutMenuItem.setText("Cut");
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
Event.CTRL_MASK, true));
}
return cutMenuItem;
}

/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getCopyMenuItem() {
if (copyMenuItem == null) {
copyMenuItem = new JMenuItem();
copyMenuItem.setText("Copy");
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
Event.CTRL_MASK, true));
}
return copyMenuItem;
}

/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getPasteMenuItem() {
if (pasteMenuItem == null) {
pasteMenuItem = new JMenuItem();
pasteMenuItem.setText("Paste");
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
Event.CTRL_MASK, true));
}
return pasteMenuItem;
}

/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getSaveMenuItem() {
if (saveMenuItem == null) {
saveMenuItem = new JMenuItem();
saveMenuItem.setText("Save");
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
Event.CTRL_MASK, true));
}
return saveMenuItem;
}

}

java之 ------ 设计思想_表现层_02

ListPanel.java

清单面板,包含数据的显示,和数据的相关操作button

package cn.hncu.addressManage.UI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import cn.hncu.addressManage.business.MySet;
import cn.hncu.utils.Panels;



public class ListPanel extends JPanel {

private static final long serialVersionUID = 1L;
private JLabel jLabel = null;
private JButton btnToAdd = null;
private JFrame jf=null;
private JList jListAddress = null;
private JButton btnToUpdate = null;
private JButton btnToDelete = null;
private JButton btnToQuery = null;

private Object objs[]; // @jve:decl-index=0:
private boolean isQuery;


/**
* This is the default constructor
*/
public ListPanel(JFrame jf,Object objs[],boolean isQuery) {
super();
this.jf = jf;

this.objs = objs;
this.isQuery = isQuery;

initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(294, 39, 202, 39));
jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
jLabel.setForeground(Color.red);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setText("我的地址本信息");
this.setSize(800, 600);
this.setLayout(null);
this.add(jLabel, null);
this.add(getBtnToAdd(), null);
this.add(getJListAddress(), null);
this.add(getBtnToUpdate(), null);
this.add(getBtnToDelete(), null);
this.add(getBtnToQuery(), null);
}

/**
* This method initializes btnToAdd
*
* @return javax.swing.JButton
*/
private JButton getBtnToAdd() {
if (btnToAdd == null) {
btnToAdd = new JButton();
btnToAdd.setFont(new Font("Dialog", Font.BOLD, 18));
btnToAdd.setSize(new Dimension(120, 41));
btnToAdd.setLocation(new Point(83, 448));
btnToAdd.setMnemonic(KeyEvent.VK_UNDEFINED);
btnToAdd.setText("转到新增");
btnToAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
//界面切换
jf.getContentPane().removeAll();
jf.getContentPane().add(new AddPanel( jf ));
jf.getContentPane().validate();
}
});
}
return btnToAdd;
}

/**
* This method initializes jListAddress
*
* @return javax.swing.JList
*/
private JList getJListAddress() {
if (jListAddress == null) {
jListAddress = new JList();
jListAddress.setBounds(new Rectangle(181, 116, 369, 216));

//为列表加入内容
if(!this.isQuery){
MySet mySet = new MySet();
objs= mySet.getAll();
}
jListAddress.setListData(objs);

}
return jListAddress;
}

/**
* This method initializes btnToUpdate
*
* @return javax.swing.JButton
*/
private JButton getBtnToUpdate() {
if (btnToUpdate == null) {
btnToUpdate = new JButton();
btnToUpdate.setBounds(new Rectangle(259, 446, 126, 46));
btnToUpdate.setFont(new Font("Dialog", Font.BOLD, 18));
btnToUpdate.setText("转到改动");
btnToUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String updateStr = (String)jListAddress.getSelectedValue();
if(updateStr==null){
Panels.changePanel(jf, new ErrorPanel(jf,ListPanel.this));
}else{
//界面切换
jf.getContentPane().removeAll();
jf.getContentPane().add(new UpdatePanel( jf,updateStr ));
jf.getContentPane().validate();
}
}
});
}
return btnToUpdate;
}

/**
* This method initializes btnToDelete
*
* @return javax.swing.JButton
*/
private JButton getBtnToDelete() {
if (btnToDelete == null) {
btnToDelete = new JButton();
btnToDelete.setBounds(new Rectangle(429, 446, 124, 46));
btnToDelete.setFont(new Font("Dialog", Font.BOLD, 18));
btnToDelete.setText("转到删除");
btnToDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
// jf.getContentPane().removeAll();
// jf.getContentPane().add(new DeletePanel());
// jf.getContentPane().validate();

String deleteStr = (String)(jListAddress.getSelectedValue());
if(deleteStr==null){
Panels.changePanel(jf, new ErrorPanel(jf,ListPanel.this));
}else{
//System.out.println(deleteStr);
Panels.changePanel(jf, new DeletePanel(jf,deleteStr));
}
}
});
}
return btnToDelete;
}

/**
* This method initializes btnToQuery
*
* @return javax.swing.JButton
*/
private JButton getBtnToQuery() {
if (btnToQuery == null) {
btnToQuery = new JButton();
btnToQuery.setBounds(new Rectangle(617, 445, 121, 46));
btnToQuery.setFont(new Font("Dialog", Font.BOLD, 18));
btnToQuery.setText("转到查询");
btnToQuery.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Panels.changePanel(jf, new QueryPanel(jf));
}
});
}
return btnToQuery;
}

}

AddPanel.java

当点击加入button时,就会跳到这个面板中

package cn.hncu.addressManage.UI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.addressManage.business.MySet;


public class AddPanel extends JPanel {

private static final long serialVersionUID = 1L;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JTextField tfdName = null;
private JLabel jLabel2 = null;
private JTextField tfdAge = null;
private JLabel jLabel3 = null;
private JTextField tfdAddress = null;
private JButton btnAdd = null;
private JButton btnBack = null;

private JFrame mainFrame = null;

/**
* This is the default constructor
*/
public AddPanel(JFrame mainFrame) {
super();
this.mainFrame = mainFrame;
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(111, 292, 57, 35));
jLabel3.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel3.setText("地址:");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(109, 222, 54, 44));
jLabel2.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel2.setText("年龄:");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(112, 147, 51, 45));
jLabel1.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel1.setText("姓名:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(339, 41, 160, 49));
jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
jLabel.setForeground(Color.red);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setForeground(Color.blue);
jLabel.setText("地址本加入");
this.setSize(800, 600);
this.setLayout(null);
this.add(jLabel, null);
this.add(jLabel1, null);
this.add(getTfdName(), null);
this.add(jLabel2, null);
this.add(getTfdAge(), null);
this.add(jLabel3, null);
this.add(getTfdAddress(), null);
this.add(getBtnAdd(), null);
this.add(getBtnBack(), null);
}

/**
* This method initializes tfdName
*
* @return javax.swing.JTextField
*/
private JTextField getTfdName() {
if (tfdName == null) {
tfdName = new JTextField();
tfdName.setBounds(new Rectangle(191, 148, 145, 47));
}
return tfdName;
}

/**
* This method initializes tfdAge
*
* @return javax.swing.JTextField
*/
private JTextField getTfdAge() {
if (tfdAge == null) {
tfdAge = new JTextField();
tfdAge.setBounds(new Rectangle(187, 223, 153, 45));
}
return tfdAge;
}

/**
* This method initializes tfdAddress
*
* @return javax.swing.JTextField
*/
private JTextField getTfdAddress() {
if (tfdAddress == null) {
tfdAddress = new JTextField();
tfdAddress.setBounds(new Rectangle(184, 290, 215, 41));
}
return tfdAddress;
}

/**
* This method initializes btnAdd
*
* @return javax.swing.JButton
*/
private JButton getBtnAdd() {
if (btnAdd == null) {
btnAdd = new JButton();
btnAdd.setSize(new Dimension(134, 49));
btnAdd.setText("新增");
btnAdd.setFont(new Font("Dialog", Font.BOLD, 18));
btnAdd.setLocation(new Point(112, 388));
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
add(); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return btnAdd;
}
private JButton getBtnBack() {
if (btnBack == null) {
btnBack = new JButton();
btnBack.setSize(new Dimension(134, 49));
btnBack.setText("返回");
btnBack.setFont(new Font("Dialog", Font.BOLD, 18));
btnBack.setLocation(new Point(445, 387));
btnBack.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
back();
}
});
}
return btnBack;
}

//把全部的处理方法写在这里
//地址信息加入
private void add(){
//1收集參数
String name = tfdName.getText();
String age = tfdAge.getText();
int iAge = 0;
try {
iAge = Integer.parseInt(age);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "年龄数据格式错误!!");
return;
}
String address = tfdAddress.getText();

//2组织參数
String str = name +","+iAge+","+address;

//3调用逻辑层
MySet mySet = new MySet();
boolean flag = mySet.add(str);

//4依据逻辑层的返回结果,导向不同的页面
if(flag){//加入成功,返回到ListPanel界面进行显示
back();
}else{
this.mainFrame.getContentPane().removeAll();
this.mainFrame.getContentPane().add(new ErrorPanel(mainFrame,this));
this.mainFrame.getContentPane().validate();
}

}

//返回到主界面
private void back() {
this.mainFrame.getContentPane().removeAll();
this.mainFrame.getContentPane().add(new ListPanel(this.mainFrame,null,false));
this.mainFrame.getContentPane().validate();
}

}


java之 ------ 设计思想_子类_03

UpdatePanel.java

改动面板

package cn.hncu.addressManage.UI;

import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.hncu.addressManage.business.MySet;


public class UpdatePanel extends JPanel {

private static final long serialVersionUID = 1L;
private JLabel jLabel = null;
private JTextField tfdName = null;
private JLabel jLabel1 = null;
private JTextField tfdAge = null;
private JLabel jLabel2 = null;
private JTextField tfdAddress = null;
private JButton btnUpdate = null;
private JButton btnBack = null;

private JFrame mainFrame=null;
private String updateStr=""; // @jve:decl-index=0:
private String strs[];

public UpdatePanel(JFrame mainFrame, String updateStr) {
super();
this.mainFrame = mainFrame;
this.updateStr = updateStr;
strs = updateStr.split(",");
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(121, 281, 56, 45));
jLabel2.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel2.setText("地址:");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(123, 183, 56, 45));
jLabel1.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel1.setText("年龄:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(129, 96, 49, 41));
jLabel.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel.setText("姓名:");
this.setSize(800, 600);
this.setLayout(null);
this.add(jLabel, null);
this.add(getTfdName(), null);
this.add(jLabel1, null);
this.add(getTfdAge(), null);
this.add(jLabel2, null);
this.add(getTfdAddress(), null);
this.add(getBtnUpdate(), null);
this.add(getBtnBack(), null);
}

/**
* This method initializes tfdName
*
* @return javax.swing.JTextField
*/
private JTextField getTfdName() {
if (tfdName == null) {
tfdName = new JTextField();
tfdName.setBounds(new Rectangle(191, 96, 139, 48));

tfdName.setText(strs[0]);
}
return tfdName;
}

/**
* This method initializes tfdAge
*
* @return javax.swing.JTextField
*/
private JTextField getTfdAge() {
if (tfdAge == null) {
tfdAge = new JTextField();
tfdAge.setBounds(new Rectangle(197, 180, 140, 53));

//应该解析,异常处理
tfdAge.setText(strs[1]);
}
return tfdAge;
}

/**
* This method initializes tfdAddress
*
* @return javax.swing.JTextField
*/
private JTextField getTfdAddress() {
if (tfdAddress == null) {
tfdAddress = new JTextField();
tfdAddress.setBounds(new Rectangle(196, 280, 160, 51));

tfdAddress.setText(strs[2]);
}
return tfdAddress;
}

/**
* This method initializes btnUpdate
*
* @return javax.swing.JButton
*/
private JButton getBtnUpdate() {
if (btnUpdate == null) {
btnUpdate = new JButton();
btnUpdate.setBounds(new Rectangle(119, 403, 122, 48));
btnUpdate.setFont(new Font("Dialog", Font.BOLD, 18));
btnUpdate.setText("改动");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
update();
}

});
}
return btnUpdate;
}

/**
* This method initializes btnBack
*
* @return javax.swing.JButton
*/
private JButton getBtnBack() {
if (btnBack == null) {
btnBack = new JButton();
btnBack.setBounds(new Rectangle(354, 402, 127, 51));
btnBack.setFont(new Font("Dialog", Font.BOLD, 18));
btnBack.setText("返回");
btnBack.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
back();
}
});
}
return btnBack;
}

//返回到主界面
private void back() {
this.mainFrame.getContentPane().removeAll();
this.mainFrame.getContentPane().add(new ListPanel(this.mainFrame,null,false));
this.mainFrame.getContentPane().validate();
}

//////////////程序的逻辑处理方法///////////////
/**
* 记录改动的事件处理方法
*/
private void update() {
//1收集參数
String name = tfdName.getText();
String age = tfdAge.getText();
int iAge = 0;
try {
iAge = Integer.parseInt(age);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "年龄数据格式错误!!");
return;
}
String address = tfdAddress.getText();

//2组织參数
String str = name +","+iAge+","+address;

//3调用逻辑层
MySet mySet = new MySet();
boolean flag = mySet.update(str,updateStr);

//4依据逻辑层的返回结果,导向不同的页面
if(flag){//改动成功,返回到ListPanel界面进行显示
back();
}else{
//假设改动后的记录在mySet中已存在,则返回false,导向出错页面.
//假设被改动的记录不存在,也是返回false。导向出错页面
this.mainFrame.getContentPane().removeAll();
this.mainFrame.getContentPane().add(new ErrorPanel(mainFrame,this));
this.mainFrame.getContentPane().validate();
}
}
}


java之 ------ 设计思想_子类_04

DeletePanel.java

删除面板

package cn.hncu.addressManage.UI;

import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.addressManage.business.MySet;
import cn.hncu.utils.Panels;



public class DeletePanel extends JPanel {

private static final long serialVersionUID = 1L;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JTextField tfdName = null;
private JLabel jLabel2 = null;
private JTextField tfdAge = null;
private JLabel jLabel3 = null;
private JTextField tfdAddress = null;
private JButton btnDelete = null;
private JButton btnBack = null;

private JFrame mainFrame = null;
private String strs[] = null;
private String deleteStr=""; // @jve:decl-index=0:
/**
* This is the default constructor
*/
public DeletePanel(JFrame mainFrame, String deleteStr) {
super();
this.mainFrame = mainFrame;
this.deleteStr = deleteStr;
strs = deleteStr.split(",");
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(167, 323, 58, 32));
jLabel3.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel3.setText("地址:");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(164, 242, 53, 43));
jLabel2.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel2.setText("年龄:");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(166, 161, 58, 45));
jLabel1.setFont(new Font("Dialog", Font.BOLD, 18));
jLabel1.setText("姓名:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(377, 67, 182, 53));
jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setText("删除地址");

this.setSize(800, 600);
this.setLayout(null);
this.add(jLabel, null);
this.add(jLabel1, null);
this.add(getTfdName(), null);
this.add(jLabel2, null);
this.add(getTfdAg