package org.addressbook.ui;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Color;
import org.addressbook.sql.*;
public class MainFrame extends JFrame implements ActionListener, MouseListener{
DBManager db=new DBManager();
JTextField queryText=new JTextField("请在此输入要查询姓名");
JButton bt_query=new JButton("查询");
JButton bt_add= new JButton("添加");
JButton bt_return=new JButton("刷新");
JButton bt_delete=new JButton("删除");
JTable tableView = new JTable();
public MainFrame() {
super("通讯录");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设定关闭窗体时退出程序
////////////////以下的程序是设置JTable的内容/////////////////////
tableView.setModel(db);
tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
setTool();
JScrollPane scrollpane = new JScrollPane(tableView);
scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
scrollpane.setPreferredSize(new Dimension(605, 500));
////////////////////////////////////////////////
JPanel top=new JPanel();
top.add(queryText);
top.add(bt_query);
JPanel bottom=new JPanel();
bottom.add(bt_add);
bottom.add(bt_return);
bottom.add(bt_delete);
JPanel pane=new JPanel();
pane.setLayout(new BorderLayout());
pane.add(top,BorderLayout.NORTH);
pane.add(scrollpane,BorderLayout.CENTER);
pane.add(bottom,BorderLayout.SOUTH);
//////////////////////////////////////////////////
bt_delete.addActionListener(this);
bt_add.addActionListener(this);
bt_query.addActionListener(this);
bt_return.addActionListener(this);
queryText.addMouseListener(this);
getContentPane().add(pane);
pack();
setVisible(true); setResizable(false);
setLocationRelativeTo(null);//设置位置居屏幕中央
}
public void setTool(){
JComboBox comboBox = new JComboBox();
comboBox.addItem("男");
comboBox.addItem("女");
TableColumn colorColumn = tableView.getColumn("性别");
colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
DefaultTableCellRenderer colorColumnRenderer = new DefaultTableCellRenderer();
colorColumnRenderer.setBackground(Color.pink);
colorColumnRenderer.setToolTipText("Click for combo box");
colorColumn.setCellRenderer(colorColumnRenderer);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt_delete){
int i=tableView.getSelectedRow();
if(i>=0){
String id=db.getId(i);
db.executeUpdate("delete * from person where id="+id);
}
db.empty();
db.query("select * from person");setTool();
}
else if(e.getSource()==bt_add){
NewPerson np=new NewPerson();
np.setVisible(true);
}
else if(e.getSource()==bt_query){
db.empty();
String a=queryText.getText();
db.query("select * from person where name like'%"+a+"%'");setTool();
}
else if(e.getSource()==bt_return){
db.empty();
db.query("select * from person");
setTool();
}
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){queryText.setText("");}
public static void main(String[] args) {
new MainFrame();
}
}