📄 phonebook.java
字号:
add(labeldelete2);add(TextFileddelete1);
add(labeldelete3);add(TextFileddelete2);
add(buttondelete3);add(buttondelete1);add(buttondelete2);
buttondelete1.addActionListener(this);
buttondelete2.addActionListener(this);
buttondelete3.addActionListener(this);
TextFileddelete1.setEditable(false);
TextFileddelete2.setEditable(false);
TextFileddelete3.setEditable(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==buttondelete1)
{ int b;
for(b=Integer.parseInt(TextFileddelete3.getText())-1;b<n-1;b++)
{
data[b].name = data[b+1].name;
data[b].phonenumber = data[b+1].phonenumber;
}
n=n-1;
data[n] = null;
now.setText(String.valueOf(n));
Delete.show(false);
TextFileddelete1.setEditable(false);
TextFileddelete2.setEditable(false);
TextFileddelete3.setEditable(true);
TextFileddelete1.setText("");
TextFileddelete2.setText("");
TextFileddelete3.setText("");
name.setText(data[Integer.parseInt(number.getText())-1].name);
phonenumber.setText(data[Integer.parseInt(number.getText())-1].phonenumber);
}
if(e.getSource()==buttondelete2)
{
Delete.show(false);
TextFileddelete1.setEditable(false);
TextFileddelete2.setEditable(false);
TextFileddelete3.setEditable(true);
TextFileddelete1.setText("");
TextFileddelete2.setText("");
TextFileddelete3.setText("");}
if(e.getSource()==buttondelete3)
{
TextFileddelete1.setText(data[Integer.parseInt(TextFileddelete3.getText())-1].name);
TextFileddelete2.setText(data[Integer.parseInt(TextFileddelete3.getText())-1].phonenumber);
number.setText(TextFileddelete3.getText());
name.setText(data[Integer.parseInt(TextFileddelete3.getText())-1].name);
phonenumber.setText(data[Integer.parseInt(TextFileddelete3.getText())-1].phonenumber);}
}
}
/****************数据类***************/
class Data implements Serializable
{
public int number;
public String name;
public String phonenumber;
public Data(int number,String name,String phonenumber)
{
this.number = number;
this.name = name;
this.phonenumber = phonenumber;
}
}
/****************PhoneBookPanel类***************/
public JPanel panel1,panel2;
public Label label1 = new Label("序号");
public Label label2 = new Label("已有记录");
public Label label3 = new Label("姓名");
public Label label4 = new Label("电话号码");
public Label label5 = new Label("简易java电话本",1);
public Label label6 = new Label("Design by invincia",1);
public Label label8 = new Label("版本号:alphi1",1);
public JTextField number = new JTextField("",3);
public JTextField now = new JTextField("",3);
public JTextField name = new JTextField("",13);
public JTextField phonenumber = new JTextField("",11);
public JButton buttonnew = new JButton("新建");
public JButton buttonfind = new JButton("查找");
public JButton buttonrework = new JButton("修改");
public JButton buttoninsert = new JButton("插入");
public JButton buttontaxis = new JButton("排序");
public JButton buttondelete = new JButton("删除");
public JButton buttonsave = new JButton("保存");
public JButton buttonload = new JButton("读取");
public JFileChooser fileChooser = new JFileChooser("C:\\");
public JFrame f = new JFrame();
public NewFrame New = new NewFrame();
public FindFrame Find = new FindFrame();
public ReworkFrame Rework = new ReworkFrame();
public InsertFrame Insert = new InsertFrame();
public TaxisFrame Taxis = new TaxisFrame();
public DeleteFrame Delete = new DeleteFrame();
public Data[] data = new Data[999];
public int n = 0;
public PhoneBookPanel() //电话本界面类
{
panel1 = new JPanel();
panel1.setLayout(new GridLayout(2,3,6,6));
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1,2,20,6));
panel1.add(buttonnew);
panel1.add(buttonfind);
panel1.add(buttonrework);
panel1.add(buttoninsert);
panel1.add(buttontaxis);
panel1.add(buttondelete);
panel2.add(buttonsave);
panel2.add(buttonload);
add(label5);
add(panel1);
add(label1);add(number);
add(label2);add(now);
add(label3);add(name);
add(label4);add(phonenumber);
add(panel2);
add(label6);
add(label8);
setVisible(true);
buttonnew.addActionListener(this);
buttonfind.addActionListener(this);
buttonrework.addActionListener(this);
buttoninsert.addActionListener(this);
buttontaxis.addActionListener(this);
buttondelete.addActionListener(this);
buttonsave.addActionListener(this);
buttonload.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
File file = null;
int result;
/****************PhoneBookPanel中新建按钮****************/
if(e.getSource()==buttonnew)
{New.show(true);
Find.show(false);
Rework.show(false);
Insert.show(false);
Taxis.show(false);
Delete.show(false);
}
/****************PhoneBookPanel中查找按钮****************/
if(e.getSource()==buttonfind)
{New.show(false);
Find.show(true);
Rework.show(false);
Insert.show(false);
Taxis.show(false);
Delete.show(false);
}
/****************PhoneBookPanel中修改按钮****************/
if(e.getSource()==buttonrework)
{New.show(false);
Find.show(false);
Rework.show(true);
Insert.show(false);
Taxis.show(false);
Delete.show(false);
}
/****************PhoneBookPanel中插入按钮****************/
if(e.getSource()==buttoninsert)
{New.show(false);
Find.show(false);
Rework.show(false);
Insert.show(true);
Taxis.show(false);
Delete.show(false);
}
/****************PhoneBookPanel中排序按钮****************/
if(e.getSource()==buttontaxis)
{New.show(false);
Find.show(false);
Rework.show(false);
Insert.show(false);
Taxis.show(true);
Delete.show(false);
}
/****************PhoneBookPanel中删除按钮****************/
if(e.getSource()==buttondelete)
{New.show(false);
Find.show(false);
Rework.show(false);
Insert.show(false);
Taxis.show(false);
Delete.show(true);
}
/****************PhoneBookPanel中保存按钮****************/
if(e.getSource()==buttonsave)
{
result = fileChooser.showSaveDialog(f);
file = null;
String fileName;
int b;
if (result == JFileChooser.APPROVE_OPTION)
{file = fileChooser.getSelectedFile();}
try
{
ObjectOutputStream out = new
ObjectOutputStream(new FileOutputStream(file));
out.writeObject(data);
out.close();
}catch(Exception t)
{number.setText("");
now.setText("");
name.setText("保存文件错误");
phonenumber.setText("重新输入");
}
/*FileOutputStream fileOutStream = null;
if(file != null)
{
try{
fileOutStream = new FileOutputStream(file);
}catch(FileNotFoundException fe){
name.setText("文件没有找到!");
return;
}
String content = phonenumber.getText();
try{
fileOutStream.write(content.getBytes());
}catch(IOException ioe){
name.setText("写入文件错误");
}
finally{//回收FileOutputStream对象,避免资源的浪费.
try{
if(fileOutStream != null)
{fileOutStream.close();}
}catch(IOException ioe2){}
}
}*/
}
/****************PhoneBookPanel中读取按钮****************/
if(e.getSource()==buttonload)
{
int readbyte;
String i;
result = fileChooser.showOpenDialog(f);
if (result == JFileChooser.APPROVE_OPTION)
{file = fileChooser.getSelectedFile();}
try
{ ObjectInputStream in = new
ObjectInputStream(new FileInputStream(file));
data = (Data[])in.readObject();
name.setText(data[0].name) ;
}catch (Exception w)
{ number.setText("");
now.setText("");
name.setText("读取文件错误");
phonenumber.setText("重新输入");
}
/*FileInputStream fileInStream = null;
if(file != null)
{
try{
fileInStream = new FileInputStream(file);
}catch(FileNotFoundException fe){
name.setText("文件没有找到!");
return;
}
try{//以read()方法读取FileInputStream对象内容,当返回值为-1时代表读完此数据流.
while((readbyte = fileInStream.read())!= -1)
{
i = String.valueOf((char)readbyte);
phonenumber.setText(phonenumber.getText()+i);
}
}catch(IOException ioe){
name.setText("读取文件错误");
}
finally{//回收FileInputStream对象,避免资源的浪费.
try{
if(fileInStream != null)
{fileInStream.close();}
}catch(IOException ioe2){}
}
}*/
}
}
}
public class PhoneBook
{
public static void main(String[] args)
{
PhoneBookFrame frame = new PhoneBookFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -