要连接数据库的
测试通过的
有添加 删除 查找功能 简单点的
要连接数据库的
测试通过的
有添加 删除 查找功能 简单点的
附件:源程序清单
import java.awt.*;
import java.awt.event.*;
class Info
{
public static String name,address,mail,telephone;
Info(){}
}
public class addressBook extends Frame implements ActionListener
{
private MenuItem noteAdd=new MenuItem("Add");
private MenuItem noteDel=new MenuItem("Del");
private MenuItem noteEdit=new MenuItem("Modify");
private MenuItem noteExit=new MenuItem("Exit");
private List noteList=new List();
private String[][] message=new String [1000][4];
public Info notes=new Info();
public addressBook(){
super("通讯录 向阳");
Menu note=new Menu("Edit");
note.add(noteAdd);
note.add(noteDel);
note.add(noteEdit);
note.addSeparator();
note.add(noteExit);
MenuBar bar=new MenuBar();
bar.add(note);
setMenuBar(bar);
add(noteList);
noteAdd.addActionListener(this);
noteDel.addActionListener(this);
noteEdit.addActionListener(this);
noteExit.addActionListener(this);
noteList.addActionListener(this);
addWindowListener(new WindowCloser());
setSize(300,400);
show();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==noteAdd){
notes.mail=null;
notes.name=null;
notes.address=null;
notes.telephone=null;
int index=noteList.getItemCount();
EditDialog dlg=new EditDialog(this,"添加通讯录");
if(dlg.isOk){
message[index][0]=dlg.textName.getText();
message[index][1]=dlg.textAddress.getText();
message[index][2]=dlg.textEmail.getText();
message[index][3]=dlg.textTelephone.getText();
if (message[index][0]!=null){
String s="姓名:"+message[index][0]+";\t地址"+message[index][1]+";\tE-mail"+message[index][2]+";\t电话"+message[index][3];
noteList.add(s);
}
}
dlg.dispose();
}else if(e.getSource()==noteDel){
ConfirmDialog dlg=new ConfirmDialog(this,"确认删除","确定要删除这一条吗?");
if (dlg.close){
noteList.remove(noteList.getSelectedIndex());
}
dlg.dispose();
}else if(e.getSource()==noteEdit||e.getSource()==noteList){
int i=noteList.getSelectedIndex();
if(i!=-1){
notes.name=message[i][0];
notes.address=message[i][1];
notes.mail=message[i][2];
notes.telephone=message[i][3];
EditDialog dlg=new EditDialog(this,"修改通讯录");
if(dlg.isOk){
message[i][0]=dlg.textName.getText();
message[i][1]=dlg.textAddress.getText();
message[i][2]=dlg.textEmail.getText();
message[i][3]=dlg.textTelephone.getText();
String s="姓名:"+message[i][0]+";\t地址"+message[i][1]+";\tE-mail"+message[i][2]+";\t电话"+message[i][3];
noteList.replaceItem(s,i);
}
dlg.dispose();
}
}else if(e.getSource()==noteExit){
System.exit(0);
}
}
private class WindowCloser extends WindowAdapter{
public void WindowClosing(WindowEvent we){
System.exit(0);
}
}
public static void main(String[]args){
addressBook a=new addressBook();
}
}
class EditDialog extends Dialog implements ActionListener
{
private Button ok=new Button("确定");
private Button cancel=new Button("取消");
private Label labelName=new Label("姓名",Label.LEFT);
private Label labelAddress=new Label("地址",Label.LEFT);
private Label labelEmail=new Label("E-mail",Label.LEFT);
private Label labelTelephone=new Label("电话",Label.LEFT);
TextField textName=new TextField(30);
TextField textAddress=new TextField(30);
TextField textEmail=new TextField(30);
TextField textTelephone=new TextField(30);
public boolean isOk=false;
private Info notes=new Info();
public EditDialog(Frame parent,String title){
super(parent,title,true);
Panel pName=new Panel();
pName.setLayout(new FlowLayout());
pName.add(labelName);
pName.add(textName);
Panel pAddress=new Panel();
pName.setLayout(new FlowLayout());
pName.add(labelAddress);
pName.add(textAddress);
Panel pEmail=new Panel();
pName.add(labelEmail);
pName.add(textEmail);
Panel pTelephone=new Panel();
pName.add(labelTelephone);
pName.add(textTelephone);
Panel pInfo=new Panel();
pInfo.setLayout(new GridLayout(4,1));
pInfo.add(pName);
pInfo.add(pAddress);
pInfo.add(pEmail);
pInfo.add(pTelephone);
Panel pButton=new Panel();
pButton.setLayout(new FlowLayout());
pButton.add(ok);
pButton.add(cancel);
setLayout(new BorderLayout());
add("Center",pInfo);
add("South",pButton);
textName.setText(notes.name);
textAddress.setText(notes.address);
textEmail.setText(notes.mail);
textTelephone.setText(notes.telephone);
ok.addActionListener(this);
cancel.addActionListener(this);
addWindowListener(new WindowCloser());
setResizable(false);
pack();
show();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==ok){
isOk=true;
this.hide();
}
if(ae.getSource()==cancel){
isOk=false;
this.hide();
}
}
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
isOk=false;
EditDialog.this.hide();
}
}
}
class ConfirmDialog extends Dialog implements ActionListener
{
private Button okay=new Button("确定");
private Button cancel=new Button("取消");
private Label label=new Label();
public boolean close=false;
public ConfirmDialog (Frame parent,String title,String question)
{
super(parent,title,true);
label.setText(question);
Panel buttons=new Panel();
buttons.setLayout(new FlowLayout());
buttons.add(okay);
buttons.add(cancel);
setLayout(new BorderLayout());
add("Center",label);
add("South",buttons);
okay.addActionListener(this);
cancel.addActionListener(this);
addWindowListener(new WindowCloser());
setResizable(false);
pack();
show();
}
private class WindowCloser extends WindowAdapter{
public void WindowClosing(WindowEvent we){
ConfirmDialog.this.close=false;
ConfirmDialog.this.hide();
}
public void actionPerformed(ActionEvent ae){
close=(ae.getSource()==okay);
hide();
}
}}