📄 mainframe.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
class MainFrame extends JFrame
{
public MainFrame()
{
setTitle("电子通讯录");//设置标题栏
Toolkit kit=Toolkit.getDefaultToolkit();//返回默认的工具箱
Dimension screenSize=kit.getScreenSize();//获得窗口大小
int screenHeight=screenSize.height;
int screenWidth=screenSize.width;
setSize(750,550);//设置窗口大小
setLocation(screenWidth/5,screenHeight/5);//设置窗口位置
setResizable(false);//设置窗口大小不可改变
String icons="Images\\Icon.jpg";//"Images\\Icon.jpg"也是可以的
Image img = kit.getImage(icons);
setIconImage(img);//设置图标
InitDlg();//对组件进行设置
}
public void InitDlg()
{
panel.setLayout(null);//设置此容器的布局管理器
add(panel);
addButton.setFont(new Font("Dialog", Font.BOLD, 15));//设置字体
addButton.setText("增加联系人");//设置标签
addButton.setBounds(new Rectangle(10,90,120,30));//设置按钮大小
addButton.addActionListener( new MainFrameAction(this));//增加按钮的一个监听器
panel.add(addButton,null);//将按钮增加到面板
delButton.setFont(new Font("Dialog",Font.BOLD,15));
delButton.setText("删除联系人");
delButton.setBounds(new Rectangle(10,170,120,30));
delButton.addActionListener( new MainFrameAction(this));//增加按钮的一个监听器
panel.add(delButton);
findButton.setFont(new Font("Dialog",Font.BOLD,15));
findButton.setText("查找联系人");
findButton.setBounds(new Rectangle(10,250,120,30));
findButton.addActionListener( new MainFrameAction(this));//增加按钮的一个监听器
panel.add(findButton);
chaButton.setFont(new Font("Dialog",Font.BOLD,15));
chaButton.setText("修改联系人");
chaButton.setBounds(new Rectangle(10,330,120,30));
chaButton.addActionListener( new MainFrameAction(this));//增加按钮的一个监听器
panel.add(chaButton);
refButton.setFont(new Font("Dialog",Font.BOLD,15));
refButton.setText("刷新列表");
refButton.setBounds(new Rectangle(490,410,120,30));
refButton.addActionListener( new MainFrameAction(this));//增加按钮的一个监听器
panel.add(refButton);
//列表
updateTableModel("select * from Book ");
mainTable.setFont(new Font("Dialog",Font.BOLD,12));
mainTable.setRowHeight (30);//设置每行的高度为,setRowHeight (0, 20)设置第1行的高度
mainTable.setRowMargin (5);//设置相邻两行单元格的距离
mainTable.setRowSelectionAllowed (true);//设置可否被选择.默认为false
mainTable.setSelectionBackground (Color.white);//设置所选择行的背景色
mainTable.setSelectionForeground (Color.blue);//设置所选择行的前景色
mainTable.setGridColor (Color.black);//设置网格线的颜色
mainTable.setBackground (Color.lightGray);
tableScroll = new JScrollPane (mainTable);
tableScroll.setBounds(new Rectangle(150,40,530,350));
panel.add(tableScroll);
//选择分组
groLabel.setFont(new Font("Dialog",Font.BOLD,15));//设置字体
groLabel.setBounds(new Rectangle(160,410,60,15));//标签位置
groLabel.setText("分 组:");//设置文字 setEditable(boolean aFlag) 确定 JComboBox 字段是否可编辑
panel.add(groLabel);//标签增加到面板中
groCombo = new JComboBox(new String[]{"家人","同事","朋友","同学","其他"});
groCombo.setFont(new Font("Dialog",Font.BOLD,15));
groCombo.setBounds(new Rectangle(230,410,100,20));
panel.add(groCombo);
groCombo.addActionListener(new MainFrameAction(this));//增加按钮的一个监听器
}
public void updateTableModel(String sql)
{//将记录集读取到表中
final String[] columnNames = {"姓名","电话","手机","QQ","Email","分组" };
DefaultTableModel updatedModel =new DefaultTableModel(columnNames,0);//创建列表
mainTable.setModel(updatedModel);
try{
rst=connDB.executeQuery(sql);
while(rst.next())
{
String name=rst.getString("姓名");
String tel=rst.getString("电话");
String mobile=rst.getString("手机");
String qq=rst.getString("QQ");
String email=rst.getString("Email");
String group=rst.getString("分组");
Object[] str_row ={name,tel,mobile,qq,email,group};
updatedModel.addRow(str_row);
}
}
catch(Exception ae)
{
ae.printStackTrace();
}
}
public void groComboAction()
{
//JOptionPane.showMessageDialog(null, "选择分组");
String group=(String)groCombo.getSelectedItem() ;//返回列表中所选项
String sql="Select * from book where 分组='"+group+"'";
updateTableModel(sql);
}
ResultSet rst;
ConnDB connDB=new ConnDB();//连接数据库
JPanel panel = new JPanel();//面板
JButton addButton = new JButton();//增加信息
JButton delButton = new JButton();//删除信息
JButton findButton = new JButton();//查找信息
JButton chaButton=new JButton();//修改信息
JButton refButton=new JButton();//刷新列表
JTable mainTable=new JTable(){
public boolean isCellEditable(int row, int column){
return false;//重新设置单元格不可编辑
} };//列表
JScrollPane tableScroll;
JLabel groLabel=new JLabel();
JComboBox groCombo;
// private AddDlg adddlg;
private class MainFrameAction implements ActionListener
{//监听器类
public MainFrameAction(MainFrame mainfr)
{
mainframe = mainfr;
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == mainframe.addButton)//当按下的是"增加联系人"按钮
{
AddDlg adddlg = new AddDlg(MainFrame.this,"增加联系人",true);
adddlg.setVisible(true);
}
else if(e.getSource()==mainframe.findButton)//当按下的是"查询联系人"按钮
{
FindDlg finddlg=new FindDlg(MainFrame.this,"查询条件","find");
finddlg.setVisible(true);
}
else if(e.getSource()==mainframe.delButton)//当按下的是"删除联系人"按钮
{
FindDlg finddlg=new FindDlg(MainFrame.this,"查找删除象","delete");
finddlg.setVisible(true);
}
else if(e.getSource()==mainframe.chaButton)
{
FindDlg chadlg=new FindDlg(MainFrame.this,"查找修改对象","change");
chadlg.setVisible(true);
}
else if(e.getSource()==mainframe.refButton)
{//刷新列表
updateTableModel("select * from Book ");
// tableScroll.revalidate();
}
else if(e.getSource()==mainframe.groCombo)
{//当对分组选框进行操作时
groComboAction();
}
}
MainFrame mainframe;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -