用SWT-designer 写的用户管理界面
2015-11-11 15:01
417 查看
本设计运用java语言编辑出基于swing的用户管理界面,并与mysql连接,对数据进行增删查改。添加:点击按钮按钮,跳出一个adddialog,并且可以通过绝对路径手动加载图片,显示在label上;删除:鼠标右键弹出菜单框可删除界面和数据库中数据。
1、登陆界面代码:
1、登陆界面代码:
public class AddDialog extends JDialog { private static final long serialVersionUID = 1L; private final JPanel contentPanel = new JPanel(); protected JComboBox<String> comboBox; private JTextField txtUserName; private JTextField txtTelephone; private JTextField txtEmail; protected String Usertp=null; private String username=null,email=null,telenumber=null,sex=null,time=null; private Manager managerframe; private Connection addconn; private Statement stmt; private JPanel imagepanel ; private String addsql; private JLabel imagelabel; private String filepath; private ButtonGroup btnGroup; private JRadioButton Rbtn_nan,Rbtn_nv; private JFileChooser filechooser; protected JFrame fileframe; /** * Launch the application. */ public static void main(String[] args) { try { AddDialog dialog = new AddDialog(); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setTitle("新增人员"); dialog.setVisible(true); // } catch (Exception e) { e.printStackTrace(); } } /** * Create the dialog. */ public AddDialog() { Date date=new Date(); //获得当前系统时间 DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); time=df.format(date); setBounds(100, 100, 522, 352); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); contentPanel.setLayout(null); JLabel lblNewLabel = new JLabel("\u7528\u6237\u7C7B\u578B\uFF1A"); lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT); lblNewLabel.setBounds(20, 23, 72, 26); contentPanel.add(lblNewLabel); JLabel label = new JLabel("\u7528\u6237\u540D\uFF1A"); label.setHorizontalAlignment(SwingConstants.LEFT); label.setBounds(20, 72, 72, 26); contentPanel.add(label); JLabel label_1 = new JLabel("\u6027\u522B\uFF1A"); label_1.setHorizontalAlignment(SwingConstants.LEFT); label_1.setBounds(20, 170, 72, 26); contentPanel.add(label_1); JLabel label_2 = new JLabel("\u5934\u50CF\uFF1A"); label_2.setHorizontalAlignment(SwingConstants.LEFT); label_2.setBounds(293, 23, 72, 26); contentPanel.add(label_2); JLabel label_4 = new JLabel("\u7535\u5B50\u90AE\u7BB1\uFF1A"); label_4.setHorizontalAlignment(SwingConstants.LEFT); label_4.setBounds(20, 219, 72, 26); contentPanel.add(label_4); JLabel label_5 = new JLabel("\u624B\u673A\u53F7\uFF1A"); label_5.setHorizontalAlignment(SwingConstants.LEFT); label_5.setBounds(20, 121, 72, 26); contentPanel.add(label_5); comboBox = new JComboBox<String>(); comboBox.setBounds(104, 23, 111, 26); comboBox.addItem("请选择..."); comboBox.addItem("管理员"); comboBox.addItem("普通用户"); contentPanel.add(comboBox); comboBox.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ Usertp = (String) comboBox.getSelectedItem(); } } }); JButton btnconfirm = new JButton("\u786E\u5B9A"); btnconfirm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { InsertInfo(); AddDialog.this.dispose(); setManagerframe(new Manager()); } }); btnconfirm.setBounds(293, 255, 77, 34); contentPanel.add(btnconfirm); JButton btncancel = new JButton("\u53D6\u6D88"); btncancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { AddDialog.this.dispose(); setManagerframe(new Manager()); } }); btncancel.setBounds(386, 255, 77, 34); contentPanel.add(btncancel); txtUserName = new JTextField(); txtUserName.setBounds(102, 75, 113, 26); contentPanel.add(txtUserName); txtUserName.setColumns(10); txtTelephone = new JTextField(); txtTelephone.setColumns(10); txtTelephone.setBounds(102, 124, 113, 26); contentPanel.add(txtTelephone); txtEmail = new JTextField(); txtEmail.setColumns(10); txtEmail.setBounds(102, 220, 179, 26); contentPanel.add(txtEmail); imagepanel = new JPanel(); imagelabel = new JLabel(); JButton btnPicture = new JButton("..\u6587\u4EF6"); btnPicture.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {//查找图片文件并显示 fileframe = new JFrame(); filechooser = new JFileChooser(); if(filechooser.showOpenDialog(fileframe)==JFileChooser.APPROVE_OPTION ){ filepath=filechooser.getSelectedFile().getAbsolutePath(); //System.out.println(filepath); imagepanel.add(imagelabel); imagelabel.setIcon(new ImageIcon(filepath)); } } }); btnPicture.setBounds(355, 23, 72, 26); contentPanel.add(btnPicture); imagepanel.setBorder(new LineBorder(new Color(0, 0, 0))); imagepanel.setBounds(305, 86, 136, 155); contentPanel.add(imagepanel); Label label_3 = new Label("\u7528\u6237\u5934\u50CF"); imagepanel.add(label_3); Rbtn_nan = new JRadioButton("\u7537"); Rbtn_nan.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { sex="男"; } }); Rbtn_nan.setBounds(104, 172, 53, 23); contentPanel.add(Rbtn_nan); Rbtn_nv = new JRadioButton("\u5973"); Rbtn_nv.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { sex="女"; } }); Rbtn_nv.setBounds(159, 172, 58, 23); contentPanel.add(Rbtn_nv); btnGroup = new ButtonGroup(); btnGroup.add(Rbtn_nan); btnGroup.add(Rbtn_nv); } public Manager getManagerframe() { return managerframe; } public void setManagerframe(Manager managerframe) { this.managerframe = managerframe; } public void InsertInfo() { //System.out.println(time); username=txtUserName.getText(); email=txtEmail.getText(); telenumber=txtTelephone.getText(); //sex=txtSex.getText(); try{ DBC();//连接数据库 addsql="insert into userif(用户名,用户类型,电子邮箱,电话,性别,修改时间) " + "values("+"'"+username+"'"+",'"+Usertp+"'"+"," + "'"+email+"'"+",'"+telenumber+"'"+",'"+sex+"'"+",'"+time+"'"+")"; addconn.createStatement(); stmt=(Statement) addconn.prepareStatement(addsql); stmt.execute(addsql);//插入多个数据 stmt.close(); addconn.close(); } catch (SQLException e) { e.printStackTrace(); } } public void DBC(){//连接数据库 try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/userinfo"; //JDBC的URL try { addconn = DriverManager.getConnection(url,"root","123456"); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
2、管理界面代码
<pre name="code" class="html">import javax.swing.JFrame; public class Manager { protected JPanel topPanel; private JFrame frame; protected JComboBox<String> comboBox; private String deleteMeg_name=null,deleteMeg_email=null,deleteMeg_telephone=null; protected String Usertype=null; private String sql,sqldelete; private JMenuItem menuIt; private MouseInputListener mouselistener; private Connection conn; private Statement stmt; private PreparedStatement pstmt; private ResultSet rs; private JPopupMenu popupmenu; private int row_value; private int deteleStatement; private JTable table; Object[] rowVector=new Object[6]; Object[][] allUserInfo={}; DefaultTableModel defaluttablemodel; /** * Launch the application. */ public static void main(String[] args) { SubstanceLookAndFeel.setSkin(new OfficeSilver2007Skin()); EventQueue.invokeLater(new Runnable() { public void run() { try { //Manager window = new Manager(); //window.frame.setVisible(true); //window.frame.setTitle("用户管理"); //window.frame } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. * @wbp.parser.entryPoint */ public Manager() { createGUI(); DBSearch(); delete(); } private void DBSearch() { // 连接数据库 try{ //调用Class.forName()方法加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/userinfo"; //JDBC的URL conn = DriverManager.getConnection(url,"root","123456"); stmt = conn.createStatement(); //创建Statement对象 //设定条件 if(Usertype=="管理员"){ defaluttablemodel.setRowCount(0);//清空table数据 sql = "select * from userif where 用户类型='管理员'"; } else if(Usertype=="普通用户"){ defaluttablemodel.setRowCount(0); sql = "select * from userif where 用户类型='普通用户'"; } else { defaluttablemodel.setRowCount(0); sql = "select * from userif where 用户类型=''"; //默认不显示 //sql = "select * from userif";//全部显示 } rs = stmt.executeQuery(sql);//创建数据对象 SQL查询语句 while (rs.next()){ String r1=rs.getString(1); String r2=rs.getString(2); String r3=rs.getString(3); String r4=rs.getString(4); String r5=rs.getString(5); String r6=rs.getString(6); Object[] userinfoRow={r1,r2,r3,r4,r5,r6}; defaluttablemodel.addRow(userinfoRow); } rs.close(); stmt.close(); conn.close(); }catch(Exception e) { System.out.println("请启动mysql数据库"); System.out.println("请启动mysql数据库"); System.out.println("请启动mysql数据库"); System.out.println("请启动mysql数据库"); e.printStackTrace(); } } @SuppressWarnings("static-access") private void createGUI() { frame = new JFrame(); frame.setDefaultLookAndFeelDecorated(true); frame.setTitle("用户管理"); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(100, 100, 757, 383); frame.getContentPane().setLayout(null); Label label = new Label("\u7528\u6237\u7C7B\u578B"); label.setFont(new Font("Dialog", Font.PLAIN, 16)); label.setAlignment(Label.CENTER); label.setBounds(105, 26, 85, 33); frame.getContentPane().add(label); JButton btnCheck = new JButton("\u67E5\u8BE2"); btnCheck.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {//查询按钮 DBSearch(); //连接数据库,按条件查询并显示 } }); btnCheck.setBounds(334, 26, 75, 33); frame.getContentPane().add(btnCheck); JButton btnAdd = new JButton("\u65B0\u589E"); btnAdd.setBounds(435, 26, 69, 33); frame.getContentPane().add(btnAdd); btnAdd.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { AddDialog add= new AddDialog();//弹出添加界面 add.setVisible(true); add.setTitle("新增人员"); frame.dispose(); } }); JButton btnExit = new JButton("\u9000\u51FA"); btnExit.setBounds(524, 26, 69, 33); frame.getContentPane().add(btnExit); btnExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0);//退出系统 } }); comboBox = new JComboBox<String>(); comboBox.addItem("请选择..."); comboBox.addItem("管理员"); comboBox.addItem("普通用户"); comboBox.setBounds(196, 25, 108, 34); frame.getContentPane().add(comboBox); comboBox.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ Usertype = (String) comboBox.getSelectedItem(); } } }); Object[] infoHead={"用户名","用户类型","电子邮箱","电话","性别","修改时间"}; defaluttablemodel=new DefaultTableModel(allUserInfo, infoHead); table = new JTable(defaluttablemodel); table.setCellSelectionEnabled(false); table.setSurrendersFocusOnKeystroke(true); final JScrollPane scrollPane = new JScrollPane(table); scrollPane.setEnabled(false); scrollPane.setBounds(9, 83, 720, 236); frame.getContentPane().add(scrollPane, BorderLayout.CENTER); table.setPreferredScrollableViewportSize(new Dimension(0,120)); table.setFillsViewportHeight(true); JPanel btnpanel = new JPanel(); btnpanel.setBounds(103, 10, 502, 63); frame.getContentPane().add(btnpanel); scrollPane.setViewportView(table); adjustColumn(); } private void delete() { //删除表格所选数据 //table.setBackground(new Color(206, 231, 255)); //设置偶数行底色 popupmenu = new JPopupMenu(); menuIt=new JMenuItem("删除"); menuIt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { deteleStatement=JOptionPane.showConfirmDialog(null, "确定删除?", "提示:", JOptionPane.YES_NO_OPTION) ; if(deteleStatement==0){ deleteDB();//删除所选行的数据 DBSearch();//刷新表格界面 } } }); popupmenu.add(menuIt); mouselistener = new MouseInputListener(){ //鼠标的监听 @Override public void mouseClicked(MouseEvent e) { processEvent(e); } @Override public void mousePressed(MouseEvent e) { processEvent(e); } @Override public void mouseReleased(MouseEvent e) { processEvent(e); if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0 && !e.isControlDown() && !e.isShiftDown()) { row_value=table.getSelectedRow(); deleteMeg_name=table.getModel().getValueAt(row_value, 0).toString(); deleteMeg_email=table.getModel().getValueAt(row_value, 2).toString(); deleteMeg_telephone=table.getModel().getValueAt(row_value, 3).toString(); popupmenu.show(table, e.getX(), e.getY()); } } @Override public void mouseEntered(MouseEvent e) { processEvent(e); } @Override public void mouseExited(MouseEvent e) { processEvent(e); } @Override public void mouseDragged(MouseEvent e) { processEvent(e); } public void mouseMoved(MouseEvent e) { processEvent(e); } private void processEvent(MouseEvent e) { if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) { int modifiers = e.getModifiers(); modifiers -= MouseEvent.BUTTON3_MASK; modifiers |= MouseEvent.BUTTON1_MASK; MouseEvent ne = new MouseEvent(e.getComponent(), e.getID(), e.getWhen(), modifiers, e.getX(), e .getY(), e.getClickCount(), false); table.dispatchEvent(ne); } } }; table.addMouseListener(mouselistener); table.addMouseMotionListener(mouselistener); } private void deleteDB() { //MySQL数据库数据删除 try{ Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/userinfo"; //JDBC的URL conn = DriverManager.getConnection(url,"root","123456"); conn.createStatement(); sqldelete = "delete from userif where 用户名=? and 电子邮箱=?and 电话=?";//删除语句 pstmt = (PreparedStatement) conn.prepareStatement(sqldelete); pstmt.setString(1, deleteMeg_name); pstmt.setString(2, deleteMeg_email); pstmt.setString(3, deleteMeg_telephone); pstmt.executeUpdate();//对数据库修改后更新 pstmt.close(); }catch(Exception e){ e.printStackTrace(); } } private void adjustColumn(){//表格属性调整 //内容居中 DefaultTableCellRenderer r = new DefaultTableCellRenderer(); r.setHorizontalAlignment(JLabel.CENTER); table.setDefaultRenderer(Object.class,r); //改变列宽 TableColumnModel colmodel = table.getColumnModel(); colmodel.getColumn(0).setPreferredWidth(20); colmodel.getColumn(1).setPreferredWidth(20); colmodel.getColumn(2).setPreferredWidth(80); colmodel.getColumn(4).setPreferredWidth(20); colmodel.getColumn(5).setPreferredWidth(100); } }
3、添加数据界面
<pre name="code" class="html">import java.awt.BorderLayout;
public class AddDialog extends JDialog { private static final long serialVersionUID = 1L; private final JPanel contentPanel = new JPanel(); protected JComboBox<String> comboBox; private JTextField txtUserName; private JTextField txtTelephone; private JTextField txtEmail; protected String Usertp=null; private String username=null,email=null,telenumber=null,sex=null,time=null; private Manager managerframe; private Connection addconn; private Statement stmt; private JPanel imagepanel ; private String addsql; private JLabel imagelabel; private String filepath; private ButtonGroup btnGroup; private JRadioButton Rbtn_nan,Rbtn_nv; private JFileChooser filechooser; protected JFrame fileframe; /** * Launch the application. */ public static void main(String[] args) { try { AddDialog dialog = new AddDialog(); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setTitle("新增人员"); dialog.setVisible(true); // } catch (Exception e) { e.printStackTrace(); } } /** * Create the dialog. */ public AddDialog() { Date date=new Date(); //获得当前系统时间 DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); time=df.format(date); setBounds(100, 100, 522, 352); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); contentPanel.setLayout(null); JLabel lblNewLabel = new JLabel("\u7528\u6237\u7C7B\u578B\uFF1A"); lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT); lblNewLabel.setBounds(20, 23, 72, 26); contentPanel.add(lblNewLabel); JLabel label = new JLabel("\u7528\u6237\u540D\uFF1A"); label.setHorizontalAlignment(SwingConstants.LEFT); label.setBounds(20, 72, 72, 26); contentPanel.add(label); JLabel label_1 = new JLabel("\u6027\u522B\uFF1A"); label_1.setHorizontalAlignment(SwingConstants.LEFT); label_1.setBounds(20, 170, 72, 26); contentPanel.add(label_1); JLabel label_2 = new JLabel("\u5934\u50CF\uFF1A"); label_2.setHorizontalAlignment(SwingConstants.LEFT); label_2.setBounds(293, 23, 72, 26); contentPanel.add(label_2); JLabel label_4 = new JLabel("\u7535\u5B50\u90AE\u7BB1\uFF1A"); label_4.setHorizontalAlignment(SwingConstants.LEFT); label_4.setBounds(20, 219, 72, 26); contentPanel.add(label_4); JLabel label_5 = new JLabel("\u624B\u673A\u53F7\uFF1A"); label_5.setHorizontalAlignment(SwingConstants.LEFT); label_5.setBounds(20, 121, 72, 26); contentPanel.add(label_5); comboBox = new JComboBox<String>(); comboBox.setBounds(104, 23, 111, 26); comboBox.addItem("请选择..."); comboBox.addItem("管理员"); comboBox.addItem("普通用户"); contentPanel.add(comboBox); comboBox.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e) { if(e.getStateChange()==ItemEvent.SELECTED){ Usertp = (String) comboBox.getSelectedItem(); } } }); JButton btnconfirm = new JButton("\u786E\u5B9A"); btnconfirm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { InsertInfo(); AddDialog.this.dispose(); setManagerframe(new Manager()); } }); btnconfirm.setBounds(293, 255, 77, 34); contentPanel.add(btnconfirm); JButton btncancel = new JButton("\u53D6\u6D88"); btncancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { AddDialog.this.dispose(); setManagerframe(new Manager()); } }); btncancel.setBounds(386, 255, 77, 34); contentPanel.add(btncancel); txtUserName = new JTextField(); txtUserName.setBounds(102, 75, 113, 26); contentPanel.add(txtUserName); txtUserName.setColumns(10); txtTelephone = new JTextField(); txtTelephone.setColumns(10); txtTelephone.setBounds(102, 124, 113, 26); contentPanel.add(txtTelephone); txtEmail = new JTextField(); txtEmail.setColumns(10); txtEmail.setBounds(102, 220, 179, 26); contentPanel.add(txtEmail); imagepanel = new JPanel(); imagelabel = new JLabel(); JButton btnPicture = new JButton("..\u6587\u4EF6"); btnPicture.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {//查找图片文件并显示 fileframe = new JFrame(); filechooser = new JFileChooser(); if(filechooser.showOpenDialog(fileframe)==JFileChooser.APPROVE_OPTION ){ filepath=filechooser.getSelectedFile().getAbsolutePath(); //System.out.println(filepath); imagepanel.add(imagelabel); imagelabel.setIcon(new ImageIcon(filepath)); } } }); btnPicture.setBounds(355, 23, 72, 26); contentPanel.add(btnPicture); imagepanel.setBorder(new LineBorder(new Color(0, 0, 0))); imagepanel.setBounds(305, 86, 136, 155); contentPanel.add(imagepanel); Label label_3 = new Label("\u7528\u6237\u5934\u50CF"); imagepanel.add(label_3); Rbtn_nan = new JRadioButton("\u7537"); Rbtn_nan.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { sex="男"; } }); Rbtn_nan.setBounds(104, 172, 53, 23); contentPanel.add(Rbtn_nan); Rbtn_nv = new JRadioButton("\u5973"); Rbtn_nv.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { sex="女"; } }); Rbtn_nv.setBounds(159, 172, 58, 23); contentPanel.add(Rbtn_nv); btnGroup = new ButtonGroup(); btnGroup.add(Rbtn_nan); btnGroup.add(Rbtn_nv); } public Manager getManagerframe() { return managerframe; } public void setManagerframe(Manager managerframe) { this.managerframe = managerframe; } public void InsertInfo() { //System.out.println(time); username=txtUserName.getText(); email=txtEmail.getText(); telenumber=txtTelephone.getText(); //sex=txtSex.getText(); try{ DBC();//连接数据库 addsql="insert into userif(用户名,用户类型,电子邮箱,电话,性别,修改时间) " + "values("+"'"+username+"'"+",'"+Usertp+"'"+"," + "'"+email+"'"+",'"+telenumber+"'"+",'"+sex+"'"+",'"+time+"'"+")"; addconn.createStatement(); stmt=(Statement) addconn.prepareStatement(addsql); stmt.execute(addsql);//插入多个数据 stmt.close(); addconn.close(); } catch (SQLException e) { e.printStackTrace(); } } public void DBC(){//连接数据库 try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/userinfo"; //JDBC的URL try { addconn = DriverManager.getConnection(url,"root","123456"); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
相关文章推荐
- mysql常见基础语法
- Linux NFS Root and PXE-Boot
- [转]How Hash Algorithms Work
- 如何在Centos上安装python3.4
- 【算法】基数排序
- js处理时间戳
- IE6不能用class命名!IE6不能用class命名!IE6不能用class命名! 重要的事情说3遍
- js
- POJ 1166:The Clocks
- MyBatis学习总结01——概述
- 【设计模式】——六大原则
- AdaBoost中利用Haar特征进行人脸识别算法分析与总结1——Haar特征与积分图
- Android.Hook框架Cydia篇
- Apache Tomcat安装教程
- 从30岁到35岁:为你的生命多积累一些厚度
- 用NFS挂载root出现:NFS: failed to create MNT RPC client, status=-101(-110)
- 解决Dex超出方法数的限制问题,让你的应用不再爆棚
- 深入理解Java4:Singleton单例模式七种方式
- 在Centos上安装代码评审工具ReviewBoard的相关支持插件点滴(第二篇)
- HDU 2546 饭卡(01背包)