?? useraddframe.java
字號:
package cg;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class UseraddFrame extends JFrame implements ActionListener{
private javax.swing.JButton btnOk,btnexit;
private javax.swing.JLabel lab1,lab2,lab3;
// private javax.swing.JTextField txtName;
// private javax.swing.JPasswordField pwd;
// private javax.swing.JComboBox cbo;
public DatabaseUser dbo;
public Connection con;
public JTextField txtName = new JTextField();
public JPasswordField pwd = new JPasswordField();
public JComboBox cbo = new JComboBox();
public UseraddFrame(){
java.awt.Container me = this.getContentPane();
me.setLayout(null);
lab1 = new JLabel("新用戶");
lab1.setForeground(Color.BLUE);
lab1.setFont(new java.awt.Font("Dialog", Font.BOLD, 20));
lab1.setHorizontalAlignment(SwingConstants.CENTER);
lab1.setBounds(new Rectangle(21, 15, 80, 29));
//txtName = new JTextField();
txtName.setForeground(Color.BLUE);
txtName.setFont(new java.awt.Font("Dialog", Font.BOLD, 20));
txtName.setBounds(new Rectangle(100, 15, 100, 29));
lab2 = new JLabel("新密碼");
lab2.setForeground(Color.BLUE);
lab2.setFont(new java.awt.Font("Dialog", Font.BOLD, 20));
lab2.setHorizontalAlignment(SwingConstants.CENTER);
lab2.setBounds(new Rectangle(21, 65, 80, 29));
//pwd = new JPasswordField();
pwd.setForeground(Color.BLUE);
pwd.setFont(new java.awt.Font("Dialog", Font.BOLD, 20));
pwd.setBounds(new Rectangle(100, 65, 100, 29));
lab3 = new JLabel("類 型");
lab3.setForeground(Color.BLUE);
lab3.setFont(new java.awt.Font("Dialog", Font.BOLD, 20));
lab3.setHorizontalAlignment(SwingConstants.CENTER);
lab3.setBounds(new Rectangle(21, 115, 80, 29));
//cbo = new JComboBox();
cbo.addItem("普通用戶");
cbo.addItem("超級用戶");
cbo.setBounds(new Rectangle(100, 115, 100, 29));
btnOk = new JButton("確定");
btnOk.setForeground(Color.BLUE);
btnOk.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
btnOk.setBounds(new Rectangle(30, 165, 70, 25));
btnexit = new JButton("退出");
btnexit.setForeground(Color.BLUE);
btnexit.setFont(new java.awt.Font("Dialog", Font.BOLD, 15));
btnexit.setBounds(new Rectangle(120, 165, 70, 25));
me.add(lab1);
me.add(txtName);
me.add(lab2);
me.add(pwd);
me.add(lab3);
me.add(cbo);
me.add(btnOk);
me.add(btnexit);
btnOk.addActionListener(this);
btnOk.setActionCommand("ok");
btnexit.addActionListener(this);
btnexit.setActionCommand("exit");
this.setTitle("添加用戶");
this.setSize(300,260);
this.setResizable(false);
// this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLocationRelativeTo(this);//居中顯示
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String strcmd = e.getActionCommand();
if(strcmd.equals("ok")){
String txtName1=txtName.getText();
String pwd1=pwd.getText();
String cbo1=(String)cbo.getSelectedItem();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:myOdbc", "sa", "");
Statement sta = con.createStatement();
String strSql = "insert into [user](用戶名,密碼,類型) values (?,?,?)";
PreparedStatement ps=con.prepareStatement(strSql);
ps.setString(1,txtName1);
ps.setString(2,pwd1);
ps.setString(3,cbo1);
int count = ps.executeUpdate();
JOptionPane.showMessageDialog(this, "登記成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
sta.close();
}
catch(Exception ea){
ea.printStackTrace();
JOptionPane.showMessageDialog(this, "寫入數據失敗!", "失敗", JOptionPane.ERROR_MESSAGE);
}
}
if(strcmd.equals("exit")){
this.setVisible(false);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -