标签:java设计思想
java的设计思想
(设计思想,是需要不断领悟的。。。)
学java的人都知道这是向对象的编程语言,从字面上理解,就是针对对象的一些操作,将具有某一特性的实体封装成一个类或者是将具有一定功能的方法,抽取出来封装成一个供外面调用的方法,然后通过new这个类或方法生成对象,最后通
过对对象进行操作或者实现相应的功能。如将一个人封装成一个人的类,人有属性:性别,年龄,出生日期,家庭住址等,外界对人进行操作时,只能改变其属性值,但是不能改变属性的种类,这要更安全,而且当对人进行操作时,也是连带属性
一起进行操作,这就是面向对象的编程。
所以java的设计思想一般离不开封装,如做一个项目,首先对项目的每个子系统进行封装,然后对子系统中的每个模块进行封装,最后对每个模块中的每个组件(组件:能完成某一特定功能的方法)进行封装,这里的子系统、模块、组件都是
一个封装体,这样做不仅能使项目做起来更加有条理,更加安全,而且让后面的维护和更改更加方便、简单、易操作。(后面有项目的实例)
a、这是Java编程里面大家公认的第一原则。
b、接口很重要的一个功能:封装隔离
c、接口是功能块或封装块的外观
接口及相关机制最基本的作用:通过接口可以实现不相关类的相同行为,而不需要考虑这些类之间的层次关系。根据接口可以了解对象的交互界面,而不需要了解对象所属的类。
面向对象程序设计讲究“提高内聚,松散耦合”,那么不同的程序模块怎么相互访问呢,就是通过接口,也就是接口是各部分对外的统一外观。接口在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; } }
ListPanel.java
清单面板,包括数据的显示,和数据的相关操作按钮
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
当点击添加按钮时,就会跳到这个面板中
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(); } }
修改面板
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(); } } }
删除面板
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(getTfdAge(), null); this.add(jLabel3, null); this.add(getTfdAddress(), null); this.add(getBtnDelete(), null); this.add(getBtnBack(), null); tfdName.setText(strs[0]); tfdAge.setText(strs[1]); tfdAddress.setText(strs[2]); } /** * This method initializes tfdName * * @return javax.swing.JTextField */ private JTextField getTfdName() { if (tfdName == null) { tfdName = new JTextField(); tfdName.setBounds(new Rectangle(239, 161, 177, 45)); } return tfdName; } /** * This method initializes tfdAge * * @return javax.swing.JTextField */ private JTextField getTfdAge() { if (tfdAge == null) { tfdAge = new JTextField(); tfdAge.setBounds(new Rectangle(241, 240, 179, 46)); } return tfdAge; } /** * This method initializes tfdAddress * * @return javax.swing.JTextField */ private JTextField getTfdAddress() { if (tfdAddress == null) { tfdAddress = new JTextField(); tfdAddress.setBounds(new Rectangle(246, 319, 231, 42)); } return tfdAddress; } /** * This method initializes btnDelete * * @return javax.swing.JButton */ private JButton getBtnDelete() { if (btnDelete == null) { btnDelete = new JButton(); btnDelete.setBounds(new Rectangle(138, 455, 131, 52)); btnDelete.setFont(new Font("Dialog", Font.BOLD, 18)); btnDelete.setText("删除"); btnDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { boolean flag = new MySet().delete(deleteStr); if(flag){ Panels.changePanel(mainFrame, new ListPanel(mainFrame,null,false)); }else{ Panels.changePanel(mainFrame, new ErrorPanel(mainFrame,DeletePanel.this)); } } }); } return btnDelete; } /** * This method initializes btnBack * * @return javax.swing.JButton */ private JButton getBtnBack() { if (btnBack == null) { btnBack = new JButton(); btnBack.setBounds(new Rectangle(399, 456, 146, 50)); 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) { Panels.changePanel(mainFrame, new ListPanel(mainFrame,null,false)); } }); } return btnBack; } }
查询面板
package cn.hncu.addressManage.UI; import java.awt.GridBagLayout; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.Rectangle; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.SwingConstants; import javax.swing.JTextField; import javax.swing.JButton; import cn.hncu.addressManage.business.MySet; import cn.hncu.utils.Panels; public class QueryPanel extends JPanel { private static final long serialVersionUID = 1L; private JLabel jLabel = null; private JLabel jLabel1 = null; private JLabel jLabel11 = null; private JLabel jLabel12 = null; private JTextField tfdName = null; private JTextField tfdAge = null; private JTextField tfdAddress = null; private JLabel jLabel2 = null; private JTextField tfdAge2 = null; private JButton btnQuery = null; private JButton btnBack = null; private JFrame mainFrame = null; /** * This is the default constructor */ public QueryPanel(JFrame mainFrame) { super(); this.mainFrame = mainFrame; initialize(); } /** * This method initializes this * * @return void */ private void initialize() { jLabel2 = new JLabel(); jLabel2.setBounds(new Rectangle(417, 264, 88, 43)); jLabel2.setFont(new Font("Dialog", Font.BOLD, 18)); jLabel2.setText("最大年龄:"); jLabel12 = new JLabel(); jLabel12.setBounds(new Rectangle(120, 347, 48, 47)); jLabel12.setText("地址:"); jLabel12.setFont(new Font("Dialog", Font.BOLD, 18)); jLabel11 = new JLabel(); jLabel11.setBounds(new Rectangle(118, 264, 94, 44)); jLabel11.setText("最小年龄:"); jLabel11.setFont(new Font("Dialog", Font.BOLD, 18)); jLabel1 = new JLabel(); jLabel1.setBounds(new Rectangle(123, 177, 50, 49)); jLabel1.setFont(new Font("Dialog", Font.BOLD, 18)); jLabel1.setText("姓名:"); jLabel = new JLabel(); jLabel.setBounds(new Rectangle(365, 68, 146, 54)); 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(jLabel11, null); this.add(jLabel12, null); this.add(getTfdName(), null); this.add(getTfdAge(), null); this.add(getTfdAddress(), null); this.add(jLabel2, null); this.add(getTfdAge2(), null); this.add(getBtnQuery(), 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(201, 176, 173, 48)); } return tfdName; } /** * This method initializes tfdAge * * @return javax.swing.JTextField */ private JTextField getTfdAge() { if (tfdAge == null) { tfdAge = new JTextField(); tfdAge.setBounds(new Rectangle(250, 262, 104, 49)); } return tfdAge; } /** * This method initializes tfdAddress * * @return javax.swing.JTextField */ private JTextField getTfdAddress() { if (tfdAddress == null) { tfdAddress = new JTextField(); tfdAddress.setBounds(new Rectangle(196, 354, 212, 43)); } return tfdAddress; } /** * This method initializes tfdAge2 * * @return javax.swing.JTextField */ private JTextField getTfdAge2() { if (tfdAge2 == null) { tfdAge2 = new JTextField(); tfdAge2.setBounds(new Rectangle(514, 265, 154, 45)); } return tfdAge2; } /** * This method initializes btnQuery * * @return javax.swing.JButton */ private JButton getBtnQuery() { if (btnQuery == null) { btnQuery = new JButton(); btnQuery.setBounds(new Rectangle(175, 445, 162, 59)); btnQuery.setFont(new Font("Dialog", Font.BOLD, 18)); btnQuery.setText("查询"); btnQuery.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { query(); } }); } return btnQuery; } protected void query() { //1收集参数 //姓名 String name = tfdName.getText().trim(); //年龄,因为可能范围查询,所以用两个参数。应该进行异常处理,此处省略 String strAge1 = tfdAge.getText().trim(); String strAge2 = tfdAge2.getText().trim(); int age1 =-1; int age2 =10000; if (strAge1!=null && strAge1.length()>0) { try { age1 = Integer.parseInt(strAge1); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "最小年龄格式出错"); return; } } if (strAge2!=null && strAge2.length()>0) { try { age2 = Integer.parseInt(strAge2); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "最大年龄格式出错"); return; } } //地址 String address = tfdAddress.getText().trim(); //2组织参数 String queryStr = name+","+age1+","+age2+","+address; //3调用逻辑层 MySet mySet = new MySet(); System.out.println("queryStr:"+queryStr); Object tmpObjs[] = mySet.query(queryStr); for(Object obj: tmpObjs){ System.out.print(obj+" "); } //4根据逻辑层的调用结果,导向不同的显示界面 if(tmpObjs!=null){ Panels.changePanel(mainFrame, new ListPanel(mainFrame,tmpObjs,true)); } } /** * This method initializes btnBack * * @return javax.swing.JButton */ private JButton getBtnBack() { if (btnBack == null) { btnBack = new JButton(); btnBack.setBounds(new Rectangle(440, 448, 151, 57)); 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) { Panels.changePanel(mainFrame, new ListPanel(mainFrame,null,false)); } }); } return btnBack; } }
当出错时弹出的面板。出错情况:当修改和删除时,没有选中要操作的项,就视为出错
package cn.hncu.addressManage.UI; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.Rectangle; import java.awt.Font; import java.awt.Color; import javax.swing.JButton; import cn.hncu.utils.Panels; public class ErrorPanel extends JPanel { private static final long serialVersionUID = 1L; private JLabel jLabel = null; private JButton btnBack = null; private JFrame jf=null; private JPanel jp=null; /** * This is the default constructor */ public ErrorPanel( JFrame jf, JPanel jp) { super(); this.jf = jf; this.jp = jp; initialize(); } /** * This method initializes this * * @return void */ private void initialize() { jLabel = new JLabel(); jLabel.setBounds(new Rectangle(283, 291, 302, 107)); jLabel.setFont(new Font("Dialog", Font.BOLD, 72)); jLabel.setBounds(new Rectangle(248, 219, 302, 107)); jLabel.setForeground(Color.magenta); jLabel.setText("杯具啦。。。"); this.setSize(800, 600); this.setLayout(null); this.add(jLabel, null); this.add(getBtnBack(), null); } /** * This method initializes btnBack * * @return javax.swing.JButton */ private JButton getBtnBack() { if (btnBack == null) { btnBack = new JButton(); btnBack.setBounds(new Rectangle(401, 465, 129, 58)); 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) { Panels.changePanel(jf,jp); } }); } return btnBack; } }
公共面板
package cn.hncu.utils; import javax.swing.JFrame; import javax.swing.JPanel; public class Panels { public static void changePanel(JFrame jf, JPanel panel){ jf.getContentPane().removeAll(); jf.getContentPane().add(panel); //jf.getContentPane().validate(); jf.getContentPane().repaint(); } }
MySet.java
package cn.hncu.addressManage.business; import cn.hncu.addressManage.dao.FileManage; public class MySet { private static final String FILE_NAME="a.txt";//※※ static Object[] objs=new Object[0]; public boolean add(Object obj){ if(contains(obj)){ return false; } Object tempObjs[] = new Object[objs.length+1]; System.arraycopy(objs, 0, tempObjs, 0, objs.length); tempObjs[objs.length] = obj; objs = tempObjs; boolean result = save();//※※ return result; } public boolean contains(Object obj){ for(Object o:objs){ if(o.equals(obj)){ return true; } } return false; } public Object[] getAll(){ objs = FileManage.readFromFile(FILE_NAME);//※※ return objs; } public int size(){ return objs.length; } public boolean update(Object obj, Object oldObj) { if(contains(obj)){ return false; } for(int i=0; i<objs.length;i++){ if(oldObj.equals(objs[i])){ objs[i]=obj; boolean result = save();//※※ return result; } } return false; } public boolean delete(Object obj) { //1判断是否存在 if(!contains(obj)){ return false; } //2创建一个新的数组,长度为原来的长度减1 Object tempObjs[] = new Object[objs.length-1]; //3把原来数组中“非删除对象”移到新的数组中 int k=0; for(int i=0; i<objs.length; i++){ if(!obj.equals(objs[i])){ tempObjs[k++] = objs[i]; } } //4把新的数组赋值给原来的数组对象 objs = tempObjs; boolean result = save();//※※ return result; } public Object[] query(String queryStr) { //解析查询参数 String strs[]= new String[4]; String ss[] = queryStr.split(","); int j=0; for(String s: ss){ strs[j++] = s; } Object tmpObjs[] = new Object[objs.length]; int age11 = Integer.parseInt(strs[1]); int age12 = Integer.parseInt(strs[2]); int k=0; for(int i=0;i<objs.length;i++){ //解析数据库中的数据 String strObj = (String)objs[i]; String strsObj[] = strObj.split(","); //卫条件1:姓名 if(strs[0]!=null && strs[0].length()>0 //先判断用户是否输了合法的查询项 && !strsObj[0].contains(strs[0])){ //再过滤不符合用户输入条件的 continue; } //卫条件2:最小年龄 int age = Integer.parseInt(strsObj[1]); if(age11>age){ continue; } //卫条件3:最大年龄 if(age12<age){ continue; } //卫条件4:地址 if(strs[3]!=null && strs[3].length()>0 //先判断用户是否输了合法的查询项 && !strsObj[2].contains(strs[3])){ //再过滤不符合用户输入条件的 continue; } tmpObjs[k++] = objs[i]; } Object oobjs[] = new Object[k]; for(int i=0;i<k;i++){ oobjs[i] = tmpObjs[i]; } return oobjs; } //※※ private boolean save() { if(!FileManage.writeToFile(FILE_NAME, objs)){ System.out.println("文件存储失败!"); return false; }else{ return true; } } }
package cn.hncu.addressManage.dao; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; /*数据层*/ public class FileManage { public static Object[] readFromFile(String fileName){ ArrayList<Object> arList = new ArrayList<Object>(); ObjectInputStream in = null; //把数据读到arList中 try{ in = new ObjectInputStream(new FileInputStream(fileName)); Object obj = null; while(true){ obj = in.readObject(); arList.add(obj); } }catch (Exception e) { System.out.println("数据已经从文件读取完毕..."); }finally{ try { if(in!=null){ in.close(); } } catch (IOException e) { e.printStackTrace(); } } //把arList中的数据转换到objs[]中返回 Object objs[] = arList.toArray(); if(objs==null){ objs = new Object[0]; } return objs; } public static boolean writeToFile(String fileName,Object objs[]){ ObjectOutputStream out = null; try { out = new ObjectOutputStream(new FileOutputStream(fileName)); for(int i=0;i<objs.length;i++){ out.writeObject(objs[i]); } System.out.println("数据已经存储到文件..."); }catch (IOException e) { e.printStackTrace(); }finally{ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } return true; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:java设计思想
原文地址:http://blog.csdn.net/u011479875/article/details/47786951