用SWT-designer 寫的使用者管理介面
阿新 • • 發佈:2019-01-30
本設計運用java語言編輯出基於swing的使用者管理介面,並與mysql連線,對資料進行增刪查改。新增:點選按鈕按鈕,跳出一個adddialog,並且可以通過絕對路徑手動載入圖片,顯示在label上;刪除:滑鼠右鍵彈出選單框可刪除介面和資料庫中資料。
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(); } } }