import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
public class TelephoneNum
{
public static void main(String[] args)
{
TelephoneNumFrame frame = new TelephoneNumFrame();//主界面
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置用户在此窗体上发起 "close" 时默认执行的操作
}
}
class TelephoneNumFrame extends JFrame implements TableModelListener, ActionListener
{
JMenuBar menubar;
JMenu setmenu, caozuo;
JMenuItem name, data, addrecord, delrecord, selrecord, updrecord;
JButton add;
JButton select;
JLabel namelabel;
JLabel danweilabel;
JLabel zhiwulabel;
JLabel phonelabel;
JTextField phonename;
JTextField phonenum;
JTextField phonedanwei;
JTextField phonezhiwu;
JPanel panel;
JScrollPane phonescrollpane; //滚动条
JTable phonetable;
DefaultTableModel defaultModel; //表模型
TableColumn column;
String head[] = {"序号", "姓名","号码","单位","职务"};
Object phoneInfo[][] = {};
TelephoneNumFrame()
{
setLayout(new BorderLayout());//设置布局
menubar = new JMenuBar();//菜单条
this.setJMenuBar(menubar);
setmenu = new JMenu("设置");//菜单
menubar.add(setmenu);
caozuo = new JMenu("操作");
menubar.add(caozuo);
name = new JMenuItem("按姓名排序");//菜单项
setmenu.add(name);
name.addActionListener(this);
data = new JMenuItem("按序号排序");
setmenu.add(data);
data.addActionListener(this);
addrecord = new JMenuItem("添加记录");
caozuo.add(addrecord);
addrecord.addActionListener(this);
delrecord = new JMenuItem("删除记录");
caozuo.add(delrecord);
delrecord.addActionListener(this);
selrecord = new JMenuItem("修改记录");
caozuo.add(selrecord);
selrecord.addActionListener(this);
updrecord = new JMenuItem("查找记录");
caozuo.add(updrecord);
updrecord.addActionListener(this);
namelabel = new JLabel("姓名");
phonename = new JTextField(16);
phonelabel = new JLabel("电话号码");
phonenum = new JTextField(16);
danweilabel = new JLabel("单位");
phonedanwei = new JTextField(16);
zhiwulabel = new JLabel("职务");
phonezhiwu = new JTextField(16);
add = new JButton("添加记录");
add.addActionListener(this);
select = new JButton("查找记录");
select.addActionListener(this);
panel = new JPanel();
panel.setLayout(new GridLayout(1,4)); // 行数2、列数3!!!!!!!!!!!
add(panel, BorderLayout.NORTH);//添加面板
panel.add(namelabel);
panel.add(phonename);
panel.add(phonelabel);
panel.add(phonenum);
panel.add(danweilabel);
panel.add(phonedanwei);
panel.add(zhiwulabel);
panel.add(phonezhiwu);
panel.add(add);
panel.add(select);
defaultModel = new DefaultTableModel(phoneInfo,head);//设置表的模型 DefaultTableModel 是指默认的表控制模型 它可以来控制 JTBALE //用JTABLE的GETTABLEMODEL来得到一个表控制模型来控制 JTBALE
defaultModel.addTableModelListener(this);
phonetable = new JTable(defaultModel);//以该模型为基础建表
phonetable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//将表设置成不自动调整列的宽度;使用滚动条。
phonescrollpane = new JScrollPane(phonetable);//加面板上
add(phonescrollpane, BorderLayout.CENTER);//添加面板
column=phonetable.getColumnModel().getColumn(0);
column.setPreferredWidth(100);
column=phonetable.getColumnModel().getColumn(1);
column.setPreferredWidth(100);
column=phonetable.getColumnModel().getColumn(2);
column.setPreferredWidth(150);
column=phonetable.getColumnModel().getColumn(3);
column.setPreferredWidth(150);
column=phonetable.getColumnModel().getColumn(4);
column.setPreferredWidth(150);
this.pack(); //调整此窗口的大小,以适合其子组件的首选大小和布局
this.setSize(661, 461); //设置此frame的状态
this.setVisible(true); //设置此frame显示
this.setResizable(false); //设置此frame显示
getAllRecord();
}
public void tableChanged(TableModelEvent e)//更改表格值事件
{
phonetable.repaint();
}
public void actionPerformed(ActionEvent ae)
{
if("按姓名排序" == ae.getActionCommand())
{
removeAllRecord();
getAllRecord();
int count = defaultModel.getRowCount();
for(int i = 0; i < count - 1; i++)
for(int j = i + 1; j < count; j++)
{
String min = null;
String max = null;
min = String.valueOf(defaultModel.getValueAt(i, 1));
max = String.valueOf(defaultModel.getValueAt(j, 1));
if(min.compareTo(max) > 0)
{
rowchange(i, j);
}
else
{
;
}
}
}
else if("按序号排序" == ae.getActionCommand())
{
removeAllRecord();
getAllRecord();
int count = defaultModel.getRowCount();
for(int i = 0; i < count - 1; i++)
for(int j = i + 1; j < count; j++)
{
int min = 0;
int max = 0;
min = Integer.valueOf(String.valueOf(defaultModel.getValueAt(i, 0)));
max = Integer.valueOf(String.valueOf(defaultModel.getValueAt(i, 0)));
if(min > max)
{
rowchange(i, j);
}
else
{
;
}
}
}
else if("添加记录" == ae.getActionCommand())
{
String getaddname = phonename.getText();
String getaddnum = phonenum.getText();
String getadddanwei = phonedanwei.getText();
String getaddzhiwu = phonezhiwu.getText();
if(getaddname.length() == 0 || getaddnum.length() == 0)
JOptionPane.showMessageDialog(this, "请输入姓名和电话号码", "Warning", JOptionPane.INFORMATION_MESSAGE);
else
{
ResultSet rs;
Connection con;
Statement sql;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
JOptionPane.showMessageDialog(this, "ClassNotFoundException", "Warning", JOptionPane.INFORMATION_MESSAGE);
}
try
{
con=DriverManager.getConnection("jdbc:odbc:phone", "", "");
sql=con.createStatement();
//sql.executeUpdate("INSERT INTO 号码记录(姓名, 号码) VALUES('" + getaddname + "','" + getaddnum + "')");
sql.executeUpdate("INSERT INTO 号码记录(姓名,号码,单位,职务) VALUES('" + getaddname + "','" + getaddnum + "','" + getadddanwei + "','" + getaddzhiwu + "')");
rs=sql.executeQuery("SELECT * FROM 号码记录 WHERE 姓名='" + getaddname + "' AND 号码='" + getaddnum + "'");
long getaddxuhao = 0;
while (rs.next())
{
getaddxuhao = rs.getLong("序号");
}
defaultModel.addRow(new Vector());//在表格中添加一行并填写值
defaultModel.setValueAt( getaddxuhao, defaultModel.getRowCount() - 1, 0);
defaultModel.setValueAt( getaddname, defaultModel.getRowCount() - 1, 1);
defaultModel.setValueAt( getaddnum, defaultModel.getRowCount() - 1, 2);
defaultModel.setValueAt( getadddanwei, defaultModel.getRowCount() - 1, 3);
defaultModel.setValueAt( getaddzhiwu, defaultModel.getRowCount() - 1, 4);
con.close();
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(this, "SQLException", "Warning", JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, "Exception", "Warning", JOptionPane.INFORMATION_MESSAGE);
}
}
phonename.setText("");
phonenum.setText("");
phonedanwei.setText("");
phonezhiwu.set