📄 myframe.java
字号:
package MyFrame1;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Rectangle;
import java.awt.Dimension;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.io.*;
import java.util.*;
public class MyFrame extends JFrame
{
JPanel MyPanel=new JPanel();
JPanel mypanel1=new JPanel();
JPanel mypanel2=new JPanel();
JPanel mypanel3=new JPanel();
JPanel Inputpanel=new JPanel();
ArrayList MyList=new ArrayList();//加入保存数据的数组
JButton add_data=new JButton("加入新记录");
JButton alter_data=new JButton("更改新记录");
JButton del_data=new JButton("删除记录");
JButton load_file=new JButton("加载记录");
JButton save_file=new JButton("保存记录");
final JLabel nameLabel=new JLabel("姓名");
final JLabel codeLabel=new JLabel("邮编");
final JLabel addressLabel=new JLabel("地址");
final JLabel homenumLabel=new JLabel("电话");
final JLabel PhoneLabel=new JLabel("手机");
final JLabel emailLabel=new JLabel("email");
JButton searchData=new JButton("用姓名查找");
final JTextField searchText=new JTextField(10);
final JTextField nameText=new JTextField(10);
final JTextField codeText=new JTextField(10);
final JTextField addressText=new JTextField(10);
final JTextField homenumText=new JTextField(10);
final JTextField PhoneText=new JTextField(10);
final JTextField emailText=new JTextField(10);
Object PhoneData[][] ={{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},
{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},
{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},
{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},
{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},
{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},
{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "},{" "," "," "," "," "," "}};
/* Object[][] PhoneData ={{"丁忻浩","353200","福建省","7758520","7758520","xinxun1@gmail"},
{"丁忻浩","353200","福建省","7758520","7758520","xinxun1@gmail"},
{"丁忻浩1111","353200","福建省","7758520","7758520","xinxun1@gmail"}};*/
String[] PhoneColumn={"姓名","邮编","地址","电话","手机","email"};
MyTable mytable=new MyTable(PhoneData);//使用表格结构
JTable table=new JTable(mytable);//把表格结构加载入jTable
public MyFrame()
{
try{
setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭窗口
jbInit();
DataInit();
pack();
show();
setVisible(true);
}
catch(Exception exception){
exception.printStackTrace();
}
}
private void jbInit()
{
setTitle("MyPhoneBook");//MyPhoneBookFrame的标题
setSize(new Dimension(680,400));//大小
// MyPanel=(JPanel)getContentPane();//得到窗体大小
// MyPanel.setLayout(flowLayout1);//设计布局
table.setPreferredScrollableViewportSize(new Dimension(640, 200));//初始化table的大小
JScrollPane MyScrollPane =new JScrollPane(table);//建立jscrollPane容器把表格加载
Inputpanel.setLayout(new GridLayout(2,6));
Inputpanel.add(nameLabel);
Inputpanel.add(nameText);
Inputpanel.add(codeLabel);
Inputpanel.add(codeText);
Inputpanel.add(addressLabel);
Inputpanel.add(addressText);
Inputpanel.add(homenumLabel);
Inputpanel.add(homenumText);
Inputpanel.add(PhoneLabel);
Inputpanel.add(PhoneText);
Inputpanel.add(emailLabel);
Inputpanel.add(emailText);
mypanel2.add(load_file);
mypanel2.add(save_file);
mypanel2.add(add_data);
mypanel2.add(alter_data);
mypanel2.add(del_data);
mypanel2.add(searchText);
mypanel2.add(searchData);
getContentPane().setLayout(new BorderLayout(10,20));
getContentPane().add(MyScrollPane,BorderLayout.CENTER);
getContentPane().add(Inputpanel,BorderLayout.NORTH);
getContentPane().add(mypanel2,BorderLayout.SOUTH);
//对按钮进行部局
}
private void DataInit()
{
try {
ObjectOutputStream fout = new ObjectOutputStream(
new FileOutputStream("PhoneData.dat",true));//打开文件
PhoneStruct firstNote = new PhoneStruct(" ");//首结点为空
MyList.add(firstNote);//加入到Arrialy数组的对象中
fout.writeObject(MyList);//写入文件
fout.close();//关闭文件
}catch(IOException e) {}
add_data.addActionListener(new ActionListener()//加入新记录
{
public void actionPerformed(ActionEvent e)
{
if(nameText.getText().length()==0)
{
JOptionPane.showMessageDialog(null, "添加姓名不可以为空?", "警告!", JOptionPane.ERROR_MESSAGE);
} //==>名字不可以为空
else
{
PhoneStruct datanote=new PhoneStruct();
datanote.name=nameText.getText();
datanote.zip=codeText.getText();
datanote.address=addressText.getText();
datanote.telephone=homenumText.getText();
datanote.mobile=PhoneText.getText();
datanote.email=emailText.getText();
//===>给对象赋值增加新增信息
MyList.add(datanote);//把数据加入到数组中
int numcols=PhoneColumn.length; //得到属性的长度
PhoneStruct temp=new PhoneStruct();//建立缓冲区保存数据
for (int i=1;i<MyList.size();i++)
{
temp = (PhoneStruct)MyList.get(i);
PhoneData[i-1][0]=temp.name;
PhoneData[i-1][1]=temp.zip;
PhoneData[i-1][2]=temp.address;
PhoneData[i-1][3]=temp.telephone;
PhoneData[i-1][4]=temp.mobile;
PhoneData[i-1][5]=temp.email;
}//刷新PhoneData的记录
table.updateUI();//数据更新
JOptionPane.showMessageDialog(null, "更新成功", "警告!", JOptionPane.ERROR_MESSAGE);
}
}
});//对加入元素按钮的功能的实现
del_data.addActionListener(new ActionListener()//删除记录
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "你要删除你的记录么?", "警告!", JOptionPane.YES_NO_OPTION );
int row=table.getSelectedRow();//得到要删除的行
if(MyList.size()<row)
{
JOptionPane.showMessageDialog(null, "删除不成功!!", "警告!", JOptionPane.ERROR_MESSAGE);
}
else
{
for (int i=1;i<MyList.size();i++)
{
PhoneData[i-1][0]=null;
PhoneData[i-1][1]=null;
PhoneData[i-1][2]=null;
PhoneData[i-1][3]=null;
PhoneData[i-1][4]=null;
PhoneData[i-1][5]=null;
}//清空PhoneData的记录
MyList.remove(row+1);//在数组中删除行
PhoneStruct temp=new PhoneStruct();//建立缓冲区保存数据
for (int i=1;i<MyList.size();i++)
{
temp = (PhoneStruct)MyList.get(i);
PhoneData[i-1][0]=temp.name;
PhoneData[i-1][1]=temp.zip;
PhoneData[i-1][2]=temp.address;
PhoneData[i-1][3]=temp.telephone;
PhoneData[i-1][4]=temp.mobile;
PhoneData[i-1][5]=temp.email;
}//刷新PhoneData的记录
table.updateUI();//数据更新
JOptionPane.showMessageDialog(null, "删除成功", "成功!", JOptionPane.ERROR_MESSAGE);
}
}
});
alter_data.addActionListener(new ActionListener()//更改记录
{
public void actionPerformed(ActionEvent e)
{
int row=table.getSelectedRow();//得到要更改的行
if(MyList.size()<row)
{
JOptionPane.showMessageDialog(null, "更改不成功!!", "警告!", JOptionPane.ERROR_MESSAGE);
}
else
{
PhoneStruct alternote=new PhoneStruct();
alternote.name=nameText.getText();
alternote.zip=codeText.getText();
alternote.address=addressText.getText();
alternote.telephone=homenumText.getText();
alternote.mobile=PhoneText.getText();
alternote.email=emailText.getText();
MyList.remove(row+1);
MyList.add(row+1,alternote);
PhoneStruct temp=new PhoneStruct();//建立缓冲区保存数据
for (int i=1;i<MyList.size();i++)
{
temp = (PhoneStruct)MyList.get(i);
PhoneData[i-1][0]=temp.name;
PhoneData[i-1][1]=temp.zip;
PhoneData[i-1][2]=temp.address;
PhoneData[i-1][3]=temp.telephone;
PhoneData[i-1][4]=temp.mobile;
PhoneData[i-1][5]=temp.email;
}//刷新PhoneData的记录
table.updateUI();//数据更新
JOptionPane.showMessageDialog(null, "更改成功", "成功!", JOptionPane.ERROR_MESSAGE);
}
}
});
load_file.addActionListener(new ActionListener()//加载文件
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "您确定要删除吗?", "警告!", JOptionPane.ERROR_MESSAGE);
try {
ObjectInputStream fin = new ObjectInputStream(
new FileInputStream("PhoneData.dat"));
MyList = (ArrayList)fin.readObject();
fin.close();
} catch(Exception ex) {}
PhoneStruct temp=new PhoneStruct();//建立缓冲区保存数据
for (int i=1;i<MyList.size();i++)
{
temp = (PhoneStruct)MyList.get(i);
PhoneData[i-1][0]=temp.name;
PhoneData[i-1][1]=temp.zip;
PhoneData[i-1][2]=temp.address;
PhoneData[i-1][3]=temp.telephone;
PhoneData[i-1][4]=temp.mobile;
PhoneData[i-1][5]=temp.email;
}//刷新PhoneData的记录
table.updateUI();//数据更新
JOptionPane.showMessageDialog(null, "提取成功", "成功!", JOptionPane.ERROR_MESSAGE);
}
});
save_file.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try {
ObjectOutputStream fout = new ObjectOutputStream(
new FileOutputStream("PhoneData.dat"));
fout.writeObject(MyList);
}catch(Exception ex){}
JOptionPane.showMessageDialog(null, "保存成功?", "成功!", JOptionPane.ERROR_MESSAGE);
}
});
searchData.addActionListener(new ActionListener()
{
PhoneStruct temp=new PhoneStruct();
public void actionPerformed(ActionEvent e)
{
for(int l=1;l<MyList.size();l++)
{
temp=(PhoneStruct)MyList.get(l);
if (temp.name.equalsIgnoreCase(searchText.getText()))
{
JOptionPane.showMessageDialog(null, "所选定行为"+l, "成功查找!!", JOptionPane.INFORMATION_MESSAGE);
break;
}
}
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -