import java.sql.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
public class contect implements ActionListener
{
JFrame f=null ;
public contect()
{
f=new JFrame("通讯录"); //创建顶层容器
Container contentPane=f.getContentPane();//获得内容面板
JPanel buttonPanel = new JPanel();
JButton b1=new JButton("信息查询");
JButton b2=new JButton("信息管理");
b1.addActionListener(this);
buttonPanel.add(b1);
b2.addActionListener(this);
buttonPanel.add(b2);
buttonPanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.red,1),"请选择操作种类",
TitledBorder.CENTER,TitledBorder.TOP));
contentPane.add(buttonPanel,BorderLayout.CENTER);//将中间容器添加到内容面板
JMenuBar mBar=new JMenuBar();//创建菜单条
JMenu sys=new JMenu("系统");
JMenuItem exit=new JMenuItem("退出系统");
sys.add(exit);
mBar.add(sys);
f.setJMenuBar(mBar);//为窗体增加菜单
f.pack();
f.setVisible(true);
exit.addActionListener(this);//建立监听器
f.setBounds(100,150,250,130);
f.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("信息查询"))
{
new select(f);
}
else
if(cmd.equals("信息管理"))
{
new manager(f);
}
else
if (cmd.equals("退出系统"))
{
System.exit(0);
}
}
public static void main(String args[])throws Exception
{
new contect();
}
}
/////////////查询///////
class select implements ActionListener
{
JDialog Dialog1;
select(JFrame f)
{
Dialog1 = new JDialog(f,"请选择查询方式",true);
Container dialogPane=Dialog1.getContentPane();
dialogPane.setLayout(new GridLayout(2,0));
JButton b1=new JButton("通过姓名查询");
dialogPane.add(b1);
JButton b2=new JButton("通过地址查询");
dialogPane.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
Dialog1.setBounds(200,200,200,150);
Dialog1.show();
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("通过姓名查询"))
{
new name(Dialog1);
}
else
if(cmd.equals("通过地址查询"))
{
new address(Dialog1);
}
}
}
class name implements ActionListener
{
JDialog Dialog1;
JTextField F1= new JTextField();
JTable jtable = new JTable();
JButton b1=new JButton("查询");
JScrollPane jscrollpane = new JScrollPane(jtable);//将表格加个滚动条
Container dialogPane;
name(JDialog Dialog1)
{
Dialog1 = new JDialog(Dialog1,"按姓名查询",true);
dialogPane=Dialog1.getContentPane();
dialogPane.setLayout(new GridLayout(3,1));
JLabel L= new JLabel("请输入要查询人的姓名:",SwingConstants.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,2));
buttonPanel.add(L);
buttonPanel.add(F1);
JPanel buttonPane2 = new JPanel();
buttonPane2.add(b1);
dialogPane.add( buttonPanel,BorderLayout.NORTH);
dialogPane.add(jscrollpane,BorderLayout.CENTER);
dialogPane.add(buttonPane2,BorderLayout.SOUTH);
b1.addActionListener(this);
Dialog1.getRootPane().setDefaultButton(b1);
Dialog1.setBounds(400,150,400,300);
Dialog1.show();
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("查询"))
{
String[] col = { "姓名", "地址", "电话","邮箱" };
DefaultTableModel mm= new DefaultTableModel(col,0); //定义一个表的模板
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载access数据库驱
Connection conn = DriverManager.getConnection("jdbc:odbc:T","test","1234"); //建立连接这里student为数据库名
Statement stmt = conn.createStatement();
String ss=F1.getText();
ResultSet rs=stmt.executeQuery("select * from TXL where name='"+ss+"' ");//将查询得到的结果集给rs
while(rs.next())
{
String name = rs.getString(1);
String address = rs.getString(2);
String tel = rs.getString(3);
String em = rs.getString(4);
String[] row={name,address,tel,em}; //将一行的数据存在row 字符串数组里
mm.addRow(row);//添加在表模板中
}
jtable.setModel(mm);//将jtable这个表 设置为刚刚定义的模板
jtable.setPreferredScrollableViewportSize(new Dimension(400,50));
dialogPane.add(jscrollpane); //将加载了表的滚动条在中显示
}catch(Exception e1){}
}
else
if(cmd.equals("退出"))
{
Dialog1.dispose();
}
}
}
class address implements ActionListener
{
JDialog Dialog1;
JTextField F1= new JTextField();
JTable jtable = new JTable();
JButton b1=new JButton("查询");
JScrollPane jscrollpane = new JScrollPane(jtable);//将表格加个滚动条
Container dialogPane;
address(JDialog Dialog1)
{
Dialog1 = new JDialog(Dialog1,"通过地址查询",true);
dialogPane=Dialog1.getContentPane();
dialogPane.setLayout(new GridLayout(3,1));
JLabel L= new JLabel("输入地址:",SwingConstants.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1,2));
buttonPanel.add(L);
buttonPanel.add(F1);
JPanel buttonPane2 = new JPanel();
buttonPane2.add(b1);
dialogPane.add( buttonPanel,BorderLayout.NORTH);
dialogPane.add(jscrollpane,BorderLayout.CENTER);
dialogPane.add(buttonPane2,BorderLayout.SOUTH);
b1.addActionListener(this);
Dialog1.getRootPane().setDefaultButton(b1);
Dialog1.setBounds(400,150,400,300);
Dialog1.show();
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("查询"))
{
String[] col = { "姓名", "地址", "电话","邮箱" };
DefaultTableModel mm= new DefaultTableModel(col,0); //定义一个表的模板
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载access数据库驱
Connection conn = DriverManager.getConnection("jdbc:odbc:T","test","1234"); //建立连接这里student为数据库名
Statement stmt = conn.createStatement();
String ss=F1.getText();
ResultSet rs=stmt.executeQuery("select * from TXL where address='"+ss+"' ");//将查询得到的结果集给rs
while(rs.next())
{
String name = rs.getString(1);
String address = rs.getString(2);
String tel = rs.getString(3);
String em = rs.getString(4);
String[] row={name,address,tel,em}; //将一行的数据存在row 字符串数组里
mm.addRow(row);//添加在表模板中
}
jtable.setModel(mm);//将jtable这个表 设置为刚刚定义的模板
jtable.setPreferredScrollableViewportSize(new Dimension(400,50));
dialogPane.add(jscrollpane); //将加载了表的滚动条在中显示
}catch(Exception e1){}
}
else
if(cmd.equals("退出"))
{
Dialog1.dispose();
}
}
}
////////////////////////////维护////////////
class manager implements ActionListener
{
JDialog Dialog1;
manager(JFrame f)
{
Dialog1 = new JDialog(f,"请选择维护方式",true);
Container dialogPane=Dialog1.getContentPane();
dialogPane.setLayout(new GridLayout(3,0));
JButton b1=new JButton("添加记录");
dialogPane.add(b1);
JButton b2=new JButton("修改记录");
dialogPane.add(b2);
JButton b3=new JButton("删除记录");
dialogPane.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
Dialog1.setBounds(400,100,200,150);
Dialog1.s
评论7
最新资源