📄 phonebook.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.AbstractButton;
import javax.swing.*;
import java.io.*;
import java.util.*;
class PhoneBookFrame extends JFrame //顶层容器类
{
public PhoneBookFrame()
{
setTitle("电话本");
setSize(220,270);
setResizable(false);
Container contentPane = getContentPane();
PhoneBookPanel panel = new PhoneBookPanel();
contentPane.add(panel);
}
}
class PhoneBookPanel extends JPanel implements ActionListener
{/****************类中类***************/
/****************NewFrame***************/
class NewFrame extends JFrame
{
public NewFrame()
{
setTitle("新建联系人");
setSize(220,150);
setResizable(false);
Container contentPaneNew = getContentPane();
NewPanel panelnew = new NewPanel();
contentPaneNew.add(panelnew);
}
}
class NewPanel extends JPanel implements ActionListener
{
public JButton buttonnew1 = new JButton("建立");
public JButton buttonnew2 = new JButton("返回");
public Label labelnew1 = new Label("当前序号");
public Label labelnew2 = new Label(String.valueOf(n+1));
public Label labelnew3 = new Label("姓名");
public Label labelnew4 = new Label("电话号码");
public Label labelnew5 = new Label(" ");
public JTextField TextFilednew1 = new JTextField("",13);
public JTextField TextFilednew2 = new JTextField("",11);
public NewPanel()
{
add(labelnew1);add(labelnew2);add(labelnew5);
add(labelnew3);add(TextFilednew1);
add(labelnew4);add(TextFilednew2);
add(buttonnew1);add(buttonnew2);
buttonnew1.addActionListener(this);
buttonnew2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==buttonnew1)
{
data[n] = new Data(n+1,TextFilednew1.getText(),TextFilednew2.getText());
n++;
labelnew2.setText(String.valueOf(n+1));
New.show(false);
number.setText(String.valueOf(n));
now.setText(String.valueOf(n));
name.setText(TextFilednew1.getText());
phonenumber.setText(TextFilednew2.getText());
TextFilednew1.setText("");
TextFilednew2.setText("");
}
if(e.getSource()==buttonnew2)
{New.show(false);}
}
}
/****************FindFrame***************/
class FindFrame extends JFrame
{
public FindFrame()
{
setTitle("查找联系人");
setSize(220,100);
setResizable(false);
Container contentPaneFind = getContentPane();
FindPanel panelfind = new FindPanel();
contentPaneFind.add(panelfind);
}
}
class FindPanel extends JPanel implements ActionListener
{ class FindFrame1 extends JFrame
{
public FindFrame1()
{
setTitle("姓名查找");
setSize(220,150);
setResizable(false);
Container contentPaneFind1 = getContentPane();
FindPanel1 panelfind1 = new FindPanel1();
contentPaneFind1.add(panelfind1);
}
}
class FindPanel1 extends JPanel implements ActionListener
{
public JButton buttonfind11 = new JButton("查找");
public JButton buttonfind12 = new JButton("返回");
public Label labelfind11 = new Label("要查找序号");
public Label labelfind12 = new Label("姓名");
public Label labelfind13 = new Label("电话号码");
public Label labelfind14 = new Label(" ");
public JTextField TextFiledfind11 = new JTextField("",13);
public JTextField TextFiledfind12 = new JTextField("",11);
public JTextField TextFiledfind13 = new JTextField("",3);
public FindPanel1()
{
add(labelfind11);add(TextFiledfind13);add(labelfind14);
add(labelfind12);add(TextFiledfind11);
add(labelfind13);add(TextFiledfind12);
add(buttonfind11);add(buttonfind12);
buttonfind11.addActionListener(this);
buttonfind12.addActionListener(this);
TextFiledfind12.setEditable(false);
TextFiledfind13.setEditable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==buttonfind11)
{if(TextFiledfind11.getText()!="")
{int a1;
for(a1=0;a1<n;a1++)
{if(TextFiledfind11.getText().equals(data[a1].name)==true)
{
TextFiledfind13.setText(String.valueOf(a1+1));
TextFiledfind11.setText(data[a1].name);
TextFiledfind12.setText(data[a1].phonenumber);
number.setText(String.valueOf(a1+1));
name.setText(data[a1].name);
phonenumber.setText(data[a1].phonenumber);
}
}
}
}
if(e.getSource()==buttonfind12)
{Find1.show(false);
TextFiledfind11.setText("");
TextFiledfind12.setText("");
TextFiledfind13.setText("");
}
}
}
class FindFrame2 extends JFrame
{
public FindFrame2()
{
setTitle("序号查找");
setSize(220,150);
setResizable(false);
Container contentPaneFind2 = getContentPane();
FindPanel2 panelfind2 = new FindPanel2();
contentPaneFind2.add(panelfind2);
}
}
class FindPanel2 extends JPanel implements ActionListener
{
public JButton buttonfind21 = new JButton("查找");
public JButton buttonfind22 = new JButton("返回");
public Label labelfind21 = new Label("要查找序号");
public Label labelfind22 = new Label("姓名");
public Label labelfind23 = new Label("电话号码");
public Label labelfind24 = new Label(" ");
public JTextField TextFiledfind21 = new JTextField("",13);
public JTextField TextFiledfind22 = new JTextField("",11);
public JTextField TextFiledfind23 = new JTextField("",3);
public FindPanel2()
{
add(labelfind21);add(TextFiledfind23);add(labelfind24);
add(labelfind22);add(TextFiledfind21);
add(labelfind23);add(TextFiledfind22);
add(buttonfind21);add(buttonfind22);
buttonfind21.addActionListener(this);
buttonfind22.addActionListener(this);
TextFiledfind21.setEditable(false);
TextFiledfind22.setEditable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==buttonfind21)
{if(TextFiledfind23.getText()!="")
{TextFiledfind21.setText(data[Integer.parseInt(TextFiledfind23.getText())-1].name);
TextFiledfind22.setText(data[Integer.parseInt(TextFiledfind23.getText())-1].phonenumber);
number.setText(TextFiledfind23.getText());
name.setText(data[Integer.parseInt(TextFiledfind23.getText())-1].name);
phonenumber.setText(data[Integer.parseInt(TextFiledfind23.getText())-1].phonenumber);
}
}
if(e.getSource()==buttonfind22)
{Find2.show(false);
TextFiledfind21.setText("");
TextFiledfind22.setText("");
TextFiledfind23.setText("");}
}
}
class FindFrame3 extends JFrame
{
public FindFrame3()
{
setTitle("电话号码查找");
setSize(220,150);
setResizable(false);
Container contentPaneFind3 = getContentPane();
FindPanel3 panelfind3 = new FindPanel3();
contentPaneFind3.add(panelfind3);
}
}
class FindPanel3 extends JPanel implements ActionListener
{
public JButton buttonfind31 = new JButton("查找");
public JButton buttonfind32 = new JButton("返回");
public Label labelfind31 = new Label("要查找序号");
public Label labelfind32 = new Label("姓名");
public Label labelfind33 = new Label("电话号码");
public Label labelfind34 = new Label(" ");
public JTextField TextFiledfind31 = new JTextField("",13);
public JTextField TextFiledfind32 = new JTextField("",11);
public JTextField TextFiledfind33 = new JTextField("",3);
public FindPanel3()
{
add(labelfind31);add(TextFiledfind33);add(labelfind34);
add(labelfind32);add(TextFiledfind31);
add(labelfind33);add(TextFiledfind32);
add(buttonfind31);add(buttonfind32);
buttonfind31.addActionListener(this);
buttonfind32.addActionListener(this);
TextFiledfind31.setEditable(false);
TextFiledfind33.setEditable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==buttonfind31)
{
if(TextFiledfind32.getText()!="")
{int a2;
for(a2=0;a2<n;a2++)
{if(TextFiledfind32.getText().equals(data[a2].phonenumber)==true)
{TextFiledfind33.setText(String.valueOf(a2+1));
TextFiledfind31.setText(data[a2].name);
TextFiledfind32.setText(data[a2].phonenumber);
number.setText(String.valueOf(a2+1));
name.setText(data[a2].name);
phonenumber.setText(data[a2].phonenumber);
}
}
}
}
if(e.getSource()==buttonfind32)
{Find3.show(false);
TextFiledfind31.setText("");
TextFiledfind32.setText("");
TextFiledfind33.setText("");}
}
}
public JButton buttonfind2 = new JButton("序号查找");
public JButton buttonfind1 = new JButton("姓名查找");
public JButton buttonfind3 = new JButton("号码查找");
public JButton buttonfind4 = new JButton("返回窗口");
public FindFrame1 Find1 = new FindFrame1();
public FindFrame2 Find2 = new FindFrame2();
public FindFrame3 Find3 = new FindFrame3();
public FindPanel()
{
add(buttonfind2);
add(buttonfind1);
add(buttonfind3);
add(buttonfind4);
buttonfind1.addActionListener(this);
buttonfind2.addActionListener(this);
buttonfind3.addActionListener(this);
buttonfind4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==buttonfind1){}
{Find1.show(true);
Find2.show(false);
Find3.show(false);}
if(e.getSource()==buttonfind2)
{Find1.show(false);
Find2.show(true);
Find3.show(false);}
if(e.getSource()==buttonfind3)
{Find1.show(false);
Find2.show(false);
Find3.show(true);}
if(e.getSource()==buttonfind4)
{Find.show(false);
Find1.show(false);
Find2.show(false);
Find3.show(false);}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -