Java-設計思想
一、封裝
學Java的人都知道這是向物件的程式設計語言,從字面上理解,就是針對物件的一些操作,將具有某一特性的實體封裝成一個類或者是將具有一定功能的方法,抽取出來封裝成一個供外面呼叫的方法,然後通過new這個類或方法生成物件,最後通過對物件進行操作或者實現相應的功能。如將一個人封裝成一個人的類,人有屬性:性別,年齡,出生日期,家庭住址等,外界對人進行操作時,只能改變其屬性值,但是不能改變屬性的種類,這要更安全,而且當對人進行操作時,也是連帶屬性一起進行操作,這就是面向物件的程式設計。
所以java的設計思想一般離不開封裝,如做一個專案,首先對專案的每個子系統進行封裝,然後對子系統中的每個模組進行封裝,最後對每個模組中的每個
二、Java程式設計的基本原則1
1、面向介面程式設計 a、這是Java程式設計裡面大家公認的第一原則b、介面很重要的一個功能:封裝隔離
c、介面是功能塊或封裝塊的外觀
介面及相關機制最基本的作用:通過介面可以實現不相關類的相同行為,而不需要考慮這些類之間的層次關係。根據介面可以瞭解物件的互動介面,而不需要了解物件所屬的類。
面向物件程式設計講究“提高內聚,鬆散耦合”,那麼不同的程式模組怎麼相互訪問呢,就是通過介面,也就是介面是各部分對外的統一外觀。介面在Java程式設計中體現的思想就是封裝隔離,因為介面只是描述一個統一的行為,所以開發人員在面向介面程式設計時並不關心具體的實現。
由以上講到的介面的作用和基本思想可以看到,介面在面向物件的Java程式設計中佔有舉足輕重的地位。事實上在設計階段最重要的任務之一就是設計出各部分的介面,然後通過介面的組合,形成程式的基本框架結構。
注:對於行為模型,優先選用介面,儘量少用抽象類
既為約束子類的功能,又要提供一些公共的功能,就選用抽象類
2、優先使用物件組合而非類繼承
在程式設計中,繼承應該當做一種資源來使用,而不應該隨意使用。同時在繼承的時候繼承鏈不能拖得太長,一旦有一個程式掛了,後面一路掛。
3、分層
最典型的三層結構: 表現層 --> 邏輯層 --> 資料層
① 表現層功能:展示資料、人機互動、收集引數呼叫邏輯層。
② 邏輯層功能:進行資料的邏輯校驗、進行邏輯判斷、實現業務功能、處理相關功能、處理後續流程、組織資料返回給表現層。
③ 資料層功能:實現資料持久化、實現物件和持久化資料的雙向對映。
4、層間互動的基本原則
① 表現層呼叫邏輯層,邏輯層呼叫資料層,不可以反過來。
② 層間互動也應該通過介面進行呼叫,以確保各層的實現獨立變化。
三、Java程式設計的基本原則2
★ 開閉原則 簡單點說就是對新增開放,對修改關閉。而且應儘量做到不用修改模組的原始碼,就能更改模組的行為。 ★ 依賴性倒置原則 這個原則就是依賴抽象而不要依賴具體實現。 ★ 介面隔離原則 這個原則就是不要使用通用的介面,而是為不同的使用者使用不同的介面。 ★ 替換原則 這個原則就是子類應當可以替換父類並出現在父類能夠出現的任何地方。四、類設計的基本經驗
★ 類要單一 所設計的類功能要單一 ,一個類應該是描述一類極其相關的屬性和方法。★ 加強內聚,鬆散耦合
指一個類,能夠在自己內部實現的功能就自己內部實現,不要去調其它類,儘量減少與其它類的依賴關係。
★ 好的封裝性
不必對外開放的部分,一律不對外開放。如果在不確定屬性許可權的情況下,先把類的屬性設成private,用的時候再開。
★ 類的粒度要合理
Java規範中建議,一個類的大小不要超過1000行,一個方法的大小不要超過100行。
★ 實現類不能依賴它的使用類
一個類依賴(使用或呼叫)了另一個類,那麼在另一個類中就不能反過來再依賴當前類。
★ 應考慮靈活性,也就是可配置、可維護
使用者的需求是會不斷改變的,後期的修改與維護是不可避免的,因此軟體開發時就要考慮靈活性。
★ 要考慮效能,考慮可伸縮性
要權衡速度與資源耗費之間的關係,要考慮軟體能夠在一定範圍內可伸縮。
★ 要考慮今後可能的變化,也就是可擴充套件性
可擴充套件性指該應用程式能夠很方便地新增新的功能。
★ 要考慮合理的複用
要儘量考慮複用,但也不能光考慮複用,不能把其它的設計原則都丟掉了。
★ 要合理的考慮介面和抽象類的使用
優先使用介面,如果既想約束實現類的行為,又要為實現類提供公共的功能時,採用抽象類。
★ 儘量減少類與協作類的互動次數和互動資訊的量
類之間不要反覆地呼叫,不要每次傳很多的資料。
★ 父類不應知道子類的資訊,子類必須知道父類的資訊
★ 訪問物件必須通過介面,不能繞過介面直接去訪問
五、例項
功能:圖形介面的個人資訊管理系統,實現增、刪、改、查等功能,而且將相關資訊存在檔案中。
設計思想:每個功能一個模組,也就是一個封裝體。
表現層:
AddressApp.java(主介面)
設定介面的大體框架,如選單,和麵板,標題等
ListPanel.java
/* * AddressApp.java * * Created on __DATE__, __TIME__ */ package cn.hncu; import javax.swing.JFrame; import cn.hncu.ui.ListPanel; /** * * @author __USER__ */ public class AddressApp extends JFrame { /** Creates new form AddressApp */ public AddressApp() { initComponents(); this.setContentPane(new ListPanel(this)); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ //GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { menuBar = new javax.swing.JMenuBar(); fileMenu = new javax.swing.JMenu(); openMenuItem = new javax.swing.JMenuItem(); saveMenuItem = new javax.swing.JMenuItem(); saveAsMenuItem = new javax.swing.JMenuItem(); exitMenuItem = new javax.swing.JMenuItem(); editMenu = new javax.swing.JMenu(); cutMenuItem = new javax.swing.JMenuItem(); copyMenuItem = new javax.swing.JMenuItem(); pasteMenuItem = new javax.swing.JMenuItem(); deleteMenuItem = new javax.swing.JMenuItem(); helpMenu = new javax.swing.JMenu(); contentsMenuItem = new javax.swing.JMenuItem(); aboutMenuItem = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setMinimumSize(new java.awt.Dimension(800, 600)); getContentPane().setLayout(null); fileMenu.setText("File"); openMenuItem.setText("Open"); fileMenu.add(openMenuItem); saveMenuItem.setText("Save"); fileMenu.add(saveMenuItem); saveAsMenuItem.setText("Save As ..."); fileMenu.add(saveAsMenuItem); exitMenuItem.setText("Exit"); exitMenuItem.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitMenuItemActionPerformed(evt); } }); fileMenu.add(exitMenuItem); menuBar.add(fileMenu); editMenu.setText("Edit"); cutMenuItem.setText("Cut"); editMenu.add(cutMenuItem); copyMenuItem.setText("Copy"); editMenu.add(copyMenuItem); pasteMenuItem.setText("Paste"); editMenu.add(pasteMenuItem); deleteMenuItem.setText("Delete"); editMenu.add(deleteMenuItem); menuBar.add(editMenu); helpMenu.setText("Help"); contentsMenuItem.setText("Contents"); helpMenu.add(contentsMenuItem); aboutMenuItem.setText("About"); helpMenu.add(aboutMenuItem); menuBar.add(helpMenu); setJMenuBar(menuBar); pack(); }// </editor-fold> //GEN-END:initComponents private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed System.exit(0); }//GEN-LAST:event_exitMenuItemActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new AddressApp().setVisible(true); } }); } //GEN-BEGIN:variables // Variables declaration - do not modify private javax.swing.JMenuItem aboutMenuItem; private javax.swing.JMenuItem contentsMenuItem; private javax.swing.JMenuItem copyMenuItem; private javax.swing.JMenuItem cutMenuItem; private javax.swing.JMenuItem deleteMenuItem; private javax.swing.JMenu editMenu; private javax.swing.JMenuItem exitMenuItem; private javax.swing.JMenu fileMenu; private javax.swing.JMenu helpMenu; private javax.swing.JMenuBar menuBar; private javax.swing.JMenuItem openMenuItem; private javax.swing.JMenuItem pasteMenuItem; private javax.swing.JMenuItem saveAsMenuItem; private javax.swing.JMenuItem saveMenuItem; // End of variables declaration//GEN-END:variables }
清單面板,包括資料的顯示,和資料的相關操作按鈕
AddPanel.java
/*
*
* Created on __DATE__, __TIME__
*/
package cn.hncu.ui;
import javax.swing.JFrame;
import cn.hncu.business.MySet;
/**
*
* @author __USER__
*/
public class ListPanel extends javax.swing.JPanel {
private JFrame mainFrame = null;
/** Creates new form ListPanel */
public ListPanel(JFrame mainFrame) {
this.mainFrame = mainFrame;
this.initComponents();
myInitData();
}
public ListPanel(JFrame mainFrame, Object objs[]) {
this.mainFrame = mainFrame;
this.initComponents();
jListAddress.setListData(objs);
}
private void myInitData() {
MySet set = new MySet();
Object objs[] = set.getAll();
jListAddress.setListData(objs);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
queryBtn = new javax.swing.JButton();
addBtn = new javax.swing.JButton();
updateBtn = new javax.swing.JButton();
deleteBtn = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jListAddress = new javax.swing.JList();
setPreferredSize(new java.awt.Dimension(800, 600));
setLayout(null);
queryBtn.setText("\u67e5\u8be2\u64cd\u4f5c");
queryBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
queryBtnActionPerformed(evt);
}
});
add(queryBtn);
queryBtn.setBounds(510, 330, 93, 29);
addBtn.setText("\u6dfb\u52a0\u64cd\u4f5c");
addBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addBtnActionPerformed(evt);
}
});
add(addBtn);
addBtn.setBounds(140, 330, 95, 29);
updateBtn.setText("\u4fee\u6539\u64cd\u4f5c");
updateBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
updateBtnActionPerformed(evt);
}
});
add(updateBtn);
updateBtn.setBounds(390, 330, 93, 29);
deleteBtn.setText("\u5220\u9664\u64cd\u4f5c");
deleteBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteBtnActionPerformed(evt);
}
});
add(deleteBtn);
deleteBtn.setBounds(260, 330, 93, 29);
jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24));
jLabel1.setText("\u6211\u7684\u5730\u5740\u672c");
add(jLabel1);
jLabel1.setBounds(310, 70, 150, 40);
jListAddress.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4",
"Item 5" };
public int getSize() {
return strings.length;
}
public Object getElementAt(int i) {
return strings[i];
}
});
jScrollPane1.setViewportView(jListAddress);
add(jScrollPane1);
jScrollPane1.setBounds(250, 130, 250, 178);
}// </editor-fold>
//GEN-END:initComponents
private void queryBtnActionPerformed(java.awt.event.ActionEvent evt) {
mainFrame.setContentPane(new QueryPanel(mainFrame));
mainFrame.validate();
}
private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {
String str = (String) (jListAddress.getSelectedValue());
mainFrame.setContentPane(new UpDatePanel(mainFrame, str));
mainFrame.validate();
}
private void deleteBtnActionPerformed(java.awt.event.ActionEvent evt) {
String str = (String) (jListAddress.getSelectedValue());
mainFrame.setContentPane(new DeletePanel(mainFrame, str));
mainFrame.validate();
}
private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {
mainFrame.setContentPane(new AddPanel(mainFrame));
mainFrame.validate();
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton addBtn;
private javax.swing.JButton deleteBtn;
private javax.swing.JLabel jLabel1;
private javax.swing.JList jListAddress;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton queryBtn;
private javax.swing.JButton updateBtn;
// End of variables declaration//GEN-END:variables
}
點選新增按鈕,就會跳到這個面板中進行新增操作
DeletePanel.java
/*
* AddPanel.java
*
* Created on __DATE__, __TIME__
*/
package cn.hncu.ui;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import cn.hncu.business.MySet;
/**
*
* @author __USER__
*/
public class AddPanel extends javax.swing.JPanel {
private JFrame mainFrame = null;
/** Creates new form AddPanel */
public AddPanel(JFrame mainFrame) {
this.mainFrame = mainFrame;
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabelAddr = new javax.swing.JLabel();
jLabelName1 = new javax.swing.JLabel();
jLabelAge = new javax.swing.JLabel();
tfdAddr = new javax.swing.JTextField();
tfdName = new javax.swing.JTextField();
tfdAge = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
btnBack = new javax.swing.JButton();
btnAdd = new javax.swing.JButton();
setLayout(null);
jLabelAddr.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelAddr.setText("\u5730\u5740");
add(jLabelAddr);
jLabelAddr.setBounds(220, 220, 70, 40);
jLabelName1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelName1.setText("\u59d3\u540d");
add(jLabelName1);
jLabelName1.setBounds(220, 140, 70, 40);
jLabelAge.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelAge.setText("\u5e74\u9f84");
add(jLabelAge);
jLabelAge.setBounds(220, 180, 70, 40);
add(tfdAddr);
tfdAddr.setBounds(290, 230, 220, 26);
add(tfdName);
tfdName.setBounds(290, 150, 90, 26);
add(tfdAge);
tfdAge.setBounds(290, 190, 90, 26);
jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24));
jLabel1.setText("\u6dfb\u52a0\u9762\u677f...");
add(jLabel1);
jLabel1.setBounds(320, 80, 120, 40);
btnBack.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
btnBack.setText("\u8fd4\u56de");
btnBack.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBackActionPerformed(evt);
}
});
add(btnBack);
btnBack.setBounds(450, 330, 95, 33);
btnAdd.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
btnAdd.setText("\u6dfb\u52a0");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});
add(btnAdd);
btnAdd.setBounds(280, 330, 90, 33);
}// </editor-fold>
//GEN-END:initComponents
private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
mainFrame.setContentPane(new ListPanel(mainFrame));
mainFrame.validate();
}
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
String name = tfdName.getText();
String addr = tfdAddr.getText();
String ageStr = tfdAge.getText();
try {
int ageInt = Integer.parseInt(ageStr);
} catch (NumberFormatException e) {
e.printStackTrace();
}
String str = name + " , " + ageStr + " , " + addr;
boolean flag = false;
MySet set = new MySet();
flag = set.add(str);
if (flag) {
mainFrame.setContentPane(new ListPanel(mainFrame));
mainFrame.validate();
} else {
JOptionPane.showMessageDialog(this, "新增失敗");
}
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnBack;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabelAddr;
private javax.swing.JLabel jLabelAge;
private javax.swing.JLabel jLabelName1;
private javax.swing.JTextField tfdAddr;
private javax.swing.JTextField tfdAge;
private javax.swing.JTextField tfdName;
// End of variables declaration//GEN-END:variables
}
刪除介面,選擇需要刪除的使用者點選刪除按鈕
QueryPanel.java
/*
* DeletePanel.java
*
* Created on __DATE__, __TIME__
*/
package cn.hncu.ui;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import cn.hncu.business.MySet;
/**
*
* @author __USER__
*/
public class DeletePanel extends javax.swing.JPanel {
private JFrame mainFrame = null;
String str = "";
/** Creates new form DeletePanel */
public DeletePanel(JFrame mainFrame, String str) {
this.str = str;
this.mainFrame = mainFrame;
initComponents();
String strs[] = str.split(",");
tfdName.setText(strs[0]);
tfdName.setEditable(false);
tfdAge.setText(strs[1]);
tfdAge.setEditable(false);
tfdAddr.setText(strs[2]);
tfdAddr.setEditable(false);
}
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabelAddr = new javax.swing.JLabel();
jLabelName = new javax.swing.JLabel();
jLabelAge = new javax.swing.JLabel();
tfdAddr = new javax.swing.JTextField();
tfdName = new javax.swing.JTextField();
tfdAge = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
btnBack = new javax.swing.JButton();
btnDelete = new javax.swing.JButton();
setLayout(null);
jLabelAddr.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelAddr.setText("\u5730\u5740");
add(jLabelAddr);
jLabelAddr.setBounds(220, 230, 70, 40);
jLabelName.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelName.setText("\u59d3\u540d");
add(jLabelName);
jLabelName.setBounds(220, 150, 70, 40);
jLabelAge.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelAge.setText("\u5e74\u9f84");
add(jLabelAge);
jLabelAge.setBounds(220, 190, 70, 40);
add(tfdAddr);
tfdAddr.setBounds(290, 240, 220, 26);
add(tfdName);
tfdName.setBounds(290, 160, 90, 26);
add(tfdAge);
tfdAge.setBounds(290, 200, 90, 26);
jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24));
jLabel1.setText("\u5220\u9664\u9762\u677f...");
add(jLabel1);
jLabel1.setBounds(320, 90, 120, 40);
btnBack.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
btnBack.setText("\u8fd4\u56de");
btnBack.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBackActionPerformed(evt);
}
});
add(btnBack);
btnBack.setBounds(450, 340, 95, 33);
btnDelete.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
btnDelete.setText("\u5220\u9664");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});
add(btnDelete);
btnDelete.setBounds(280, 340, 90, 33);
}// </editor-fold>
//GEN-END:initComponents
private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {
MySet set = new MySet();
boolean flag = set.delete(str);
if (flag) {
mainFrame.setContentPane(new ListPanel(mainFrame));
mainFrame.validate();
} else {
JOptionPane.showMessageDialog(this, "新增失敗");
}
}
private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
mainFrame.setContentPane(new ListPanel(mainFrame));
mainFrame.validate();
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton btnBack;
private javax.swing.JButton btnDelete;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabelAddr;
private javax.swing.JLabel jLabelAge;
private javax.swing.JLabel jLabelName;
private javax.swing.JTextField tfdAddr;
private javax.swing.JTextField tfdAge;
private javax.swing.JTextField tfdName;
// End of variables declaration//GEN-END:variables
}
查詢介面
UpDatePanel.java
/*
* QueryPanel.java
*
* Created on __DATE__, __TIME__
*/
package cn.hncu.ui;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import cn.hncu.business.MySet;
/**
*
* @author __USER__
*/
public class QueryPanel extends javax.swing.JPanel {
private JFrame mainFrame = null;
/** Creates new form QueryPanel */
public QueryPanel(JFrame mainFrame) {
this.mainFrame = mainFrame;
initComponents();
}
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabelAddr = new javax.swing.JLabel();
jLabelName1 = new javax.swing.JLabel();
jLabelAge = new javax.swing.JLabel();
tfdAddr = new javax.swing.JTextField();
tfdName = new javax.swing.JTextField();
tfdAge = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
btnBack = new javax.swing.JButton();
btnQuery = new javax.swing.JButton();
setLayout(null);
jLabelAddr.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelAddr.setText("\u5730\u5740");
add(jLabelAddr);
jLabelAddr.setBounds(230, 230, 70, 40);
jLabelName1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelName1.setText("\u59d3\u540d");
add(jLabelName1);
jLabelName1.setBounds(230, 150, 70, 40);
jLabelAge.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelAge.setText("\u5e74\u9f84");
add(jLabelAge);
jLabelAge.setBounds(230, 190, 70, 40);
add(tfdAddr);
tfdAddr.setBounds(300, 240, 220, 26);
add(tfdName);
tfdName.setBounds(300, 160, 90, 26);
add(tfdAge);
tfdAge.setBounds(300, 200, 90, 26);
jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24));
jLabel1.setText("\u67e5\u8be2\u9762\u677f...");
add(jLabel1);
jLabel1.setBounds(330, 90, 120, 40);
btnBack.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
btnBack.setText("\u8fd4\u56de");
btnBack.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBackActionPerformed(evt);
}
});
add(btnBack);
btnBack.setBounds(460, 340, 95, 33);
btnQuery.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
btnQuery.setText("\u67e5\u8be2");
btnQuery.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnQueryActionPerformed(evt);
}
});
add(btnQuery);
btnQuery.setBounds(290, 340, 90, 33);
}// </editor-fold>
//GEN-END:initComponents
private void btnQueryActionPerformed(java.awt.event.ActionEvent evt) {
String name = tfdName.getText();
String addr = tfdAddr.getText();
String ageStr = tfdAge.getText();
MySet set = new MySet();
try {
Object obj[] = set.Query(name, ageStr, addr);
mainFrame.setContentPane(new ListPanel(mainFrame, obj));
mainFrame.validate();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "無該聯絡人");
}
}
private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
mainFrame.setContentPane(new ListPanel(mainFrame));
mainFrame.validate();
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton btnBack;
private javax.swing.JButton btnQuery;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabelAddr;
private javax.swing.JLabel jLabelAge;
private javax.swing.JLabel jLabelName1;
private javax.swing.JTextField tfdAddr;
private javax.swing.JTextField tfdAge;
private javax.swing.JTextField tfdName;
// End of variables declaration//GEN-END:variables
}
修改介面
/*
* UpDate.java
*
* Created on __DATE__, __TIME__
*/
package cn.hncu.ui;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import cn.hncu.business.MySet;
/**
*
* @author __USER__
*/
public class UpDatePanel extends javax.swing.JPanel {
private JFrame mainFrame = null;
private String oldStr = "";
/** Creates new form UpDate */
public UpDatePanel(JFrame mainFrame, String oldStr) {
this.mainFrame = mainFrame;
this.oldStr = oldStr;
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabelAddr = new javax.swing.JLabel();
jLabelName1 = new javax.swing.JLabel();
jLabelAge = new javax.swing.JLabel();
tfdAddr = new javax.swing.JTextField();
tfdName = new javax.swing.JTextField();
tfdAge = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
btnBack = new javax.swing.JButton();
btnUpdate = new javax.swing.JButton();
setLayout(null);
jLabelAddr.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelAddr.setText("\u5730\u5740");
add(jLabelAddr);
jLabelAddr.setBounds(250, 230, 70, 40);
jLabelName1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelName1.setText("\u59d3\u540d");
add(jLabelName1);
jLabelName1.setBounds(250, 150, 70, 40);
jLabelAge.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 14));
jLabelAge.setText("\u5e74\u9f84");
add(jLabelAge);
jLabelAge.setBounds(250, 190, 70, 40);
add(tfdAddr);
tfdAddr.setBounds(320, 240, 220, 26);
add(tfdName);
tfdName.setBounds(320, 160, 90, 26);
tfdAge.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tfdAgeActionPerformed(evt);
}
});
add(tfdAge);
tfdAge.setBounds(320, 200, 90, 26);
jLabel1.setFont(new java.awt.Font("Microsoft YaHei UI", 1, 24));
jLabel1.setText("\u4fee\u6539\u9762\u677f...");
add(jLabel1);
jLabel1.setBounds(350, 90, 120, 40);
btnBack.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
btnBack.setText("\u8fd4\u56de");
btnBack.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBackActionPerformed(evt);
}
});
add(btnBack);
btnBack.setBounds(480, 340, 95, 33);
btnUpdate.setFont(new java.awt.Font("Microsoft YaHei UI", 0, 18));
btnUpdate.setText("\u4fee\u6539");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(evt);
}
});
add(btnUpdate);
btnUpdate.setBounds(310, 340, 90, 33);
}// </editor-fold>
//GEN-END:initComponents
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
String name = tfdName.getText();
String addr = tfdAddr.getText();
String ageStr = tfdAge.getText();
String newStr = name + " , " + ageStr + " , " + addr;
boolean flag = false;
MySet set = new MySet();
flag = set.upDate(oldStr, newStr);
if (flag) {
mainFrame.setContentPane(new ListPanel(mainFrame));
mainFrame.validate();
} else {
JOptionPane.showMessageDialog(this, "修改失敗");
}
}
private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {
mainFrame.setContentPane(new ListPanel(mainFrame));
mainFrame.validate();
}
private void tfdAgeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
//GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton btnBack;
private javax.swing.JButton btnUpdate;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabelAddr;
private javax.swing.JLabel jLabelAge;
private javax.swing.JLabel jLabelName1;
private javax.swing.JTextField tfdAddr;
private javax.swing.JTextField tfdAge;
private javax.swing.JTextField tfdName;
// End of variables declaration//GEN-END:variables
}
邏輯層(business層)
MySet.java
package cn.hncu.business;
import javax.swing.JOptionPane;
import cn.hncu.dao.DaoFile;
public class MySet {
final String FileName="dao.data";
public static Object objs[]=new Object[0];
public boolean contains(Object obj){
for (Object o:objs){
if (o.equals(obj)){
return true;
}
}
return false;
}
public boolean add(Object o){
if (contains(o)){
return false;
}
Object temp[]=new Object[objs.length+1];
System.arraycopy(objs, 0, temp, 0, objs.length);
temp[objs.length]=o;
objs=temp;
boolean result=save();
return result;
}
public boolean delete(Object obj){
if (!contains(obj)){
return false;
}
Object tempObj[]=new Object[objs.length-1];
int k=0;
for (int i=0;i<objs.length;i++){ boolean="" for="" i="0;i<objs.length;i++){" if="" int="" k="0;" object="" objs="tempObj;" public="" result="save();" return="" string="" strobj="(String)">0){
if (!strs[0].contains(nameStr)){
continue;
}
}
if (ageStr!=null&&ageStr.trim().length()>0){
if (!strs[1].equals(ageStr)){
continue;
}
}
if (addrStr!=null&&addrStr.trim().length()>0){
if (!strs[2].contains(addrStr)){
continue;
}
}
tempObj[k++]=objs[i];
}
Object tempObj2[]=new Object[k];
for (int i=0;i<k;i++){ boolean="" data="" if="" objs="DaoFile.read(FileName);" pre="" public="" result="=false){" return="" saving="">資料層<p> </p><p>
DaoFile.java</p><p> </p><pre class="brush:java;">package cn.hncu.dao;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class DaoFile {
public static Object[] read(String fileName){
ArrayList<object> list=new ArrayList<object>();
ObjectInputStream in=null;
try {
in=new ObjectInputStream(new FileInputStream(fileName));
Object obj=null;
//※※物件流的讀不能用available()來判斷,而應該用異常來確定是否讀到結束
while (true){
obj=in.readObject();
list.add(obj);
}
} catch (Exception e) {
//讀到檔案末尾,就是出異常,通過這來判斷是否讀到結束。
//因此,本程式中,這裡是正常的檔案讀取結束,不是我們之前認為的出異常--所以不輸出異常資訊
e.printStackTrace();
} finally {
if (in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Object objs[]=list.toArray();
if (objs==null){
objs=new Object[0];
}
return objs;
}
public static boolean write(String fileName,Object objs[]){
ObjectOutputStream out=null;
try {
out=new ObjectOutputStream(new FileOutputStream(fileName));
for (Object o:objs){
out.writeObject(o);
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
}</object></object></pre>
</k;i++){></objs.length;i++){><br>
原文連結:http://www.2cto.com/kf/201605/511889.html