📄 addcustomer.java
字号:
import java.awt.Toolkit;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.swtdesigner.SWTResourceManager;
public class AddCustomer extends Dialog {
protected Object result;
protected Shell shell;
private Text text_4;
private Text text_3;
private Text text_2;
private Text text_1;
private Text text;
private String id;
private String pwd;
private MessageBox messageBox;
private String name = "",phone = "",address = "",code = "",email = "",sex = "";
private boolean gender,flag = true;
/**
* Create the dialog
* @param parent
* @param style
*/
public AddCustomer(Shell parent, int style,String id,String pwd) {
super(parent, style);
this.id = id;
this.pwd = pwd;
}
/**
* Create the dialog
* @param parent
*/
public AddCustomer(Shell parent,String id,String pwd) {
this(parent, SWT.NONE,id,pwd);
}
public void setCenter(){
// 得到屏幕的宽度和高度
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
// 得到Shell窗口的宽度和高度
int shellHeight = this.shell.getBounds().height;
int shellWidth = this.shell.getBounds().width;
// 如果窗口大小超过屏幕大小,让窗口与屏幕等大
if (shellHeight > screenHeight)
shellHeight = screenHeight;
if (shellWidth > screenWidth)
shellWidth = screenWidth;
// 让窗口在屏幕中间显示
this.shell.setLocation(((screenWidth - shellWidth) / 2),
((screenHeight - shellHeight) / 2));
}
/**
* Open the dialog
* @return the result
*/
public Object open() {
createContents();
setCenter();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
/**
* Create contents of the dialog
*/
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setSize(387, 375);
shell.setText("添加新客户");
//
final Label label = new Label(shell, SWT.CENTER);
label.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
label.setText("姓 名");
label.setBounds(78, 62, 65, 17);
final Label label_1 = new Label(shell, SWT.CENTER);
label_1.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
label_1.setText("性 别");
label_1.setBounds(78, 99, 65, 17);
final Label label_2 = new Label(shell, SWT.CENTER);
label_2.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
label_2.setText("联系电话");
label_2.setBounds(78, 136, 65, 17);
final Label label_3 = new Label(shell, SWT.CENTER);
label_3.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
label_3.setText("地 址");
label_3.setBounds(78, 174, 65, 17);
final Label label_4 = new Label(shell, SWT.CENTER);
label_4.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
label_4.setText("邮 编");
label_4.setBounds(78, 215, 65, 17);
final Label emailLabel = new Label(shell, SWT.CENTER);
emailLabel.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
emailLabel.setText("E-mail");
emailLabel.setBounds(78, 253, 65, 17);
text = new Text(shell, SWT.BORDER);
text.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
text.setBounds(163, 55, 97, 25);
text_1 = new Text(shell, SWT.BORDER);
text_1.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
text_1.setBounds(163, 130, 142, 25);
text_2 = new Text(shell, SWT.BORDER);
text_2.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
text_2.setBounds(163, 170, 142, 25);
text_3 = new Text(shell, SWT.BORDER);
text_3.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
text_3.setBounds(163, 210, 142, 25);
text_4 = new Text(shell, SWT.BORDER);
text_4.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
text_4.setBounds(163, 248, 142, 25);
final Button button = new Button(shell, SWT.RADIO);
button.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
button.setText("男");
button.setBounds(166, 100, 38, 17);
final Button button_1 = new Button(shell, SWT.RADIO);
button_1.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
button_1.setText("女");
button_1.setBounds(222, 100, 38, 17);
final Button button_2 = new Button(shell, SWT.NONE);
button_2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
if((text.getText()).length() > 20)
{
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("客户姓名过长\n请重新输入姓名");
messageBox.setText("客户姓名");
messageBox.open();
flag = false;
}
else
if((text_1.getText()).length() > 20)
{
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("联系电话过长\n请重新输入联系电话");
messageBox.setText("联系电话");
messageBox.open();
flag = false;
}
else
if((text_2.getText()).length() > 20)
{
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("地址过长\n请重新输入地址");
messageBox.setText("地址");
messageBox.open();
flag = false;
}
else
if((text_3.getText()).length() > 10)
{
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("邮编过长\n请重新输入邮编");
messageBox.setText("邮编");
messageBox.open();
flag = false;
}
else
if((text_4.getText()).length() > 20)
{
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("E-mail地址过长\n请重新输入E-mail");
messageBox.setText("E-mail");
messageBox.open();
flag = false;
}
else
{
//获取姓名
if(flag)
{
if((text.getText()).equals(""))
{
messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
messageBox.setMessage("请输入客户姓名");
messageBox.setText("客户姓名");
messageBox.open();
flag = false;
}
else
name = text.getText();
}
//获取性别
if(flag)
{
if(button_1.getSelection())
{
gender = false;
sex = "女";
}
else
{
gender = true;
sex = "男";
}
}
//获取联系电话
if(flag)
phone = text_1.getText();
//获取地址
if(flag)
address = text_2.getText();
//获取邮编
if(flag)
code = text_3.getText();
//获取E-mail
if(flag)
email = text_4.getText();
if(flag)
{
messageBox = new MessageBox(shell,SWT.ICON_QUESTION|SWT.OK|SWT.CANCEL);
messageBox.setMessage("客户姓名: " + name +
"\n性别: " + sex +
"\n联系电话: " + phone +
"\n地址: " + address +
"\n邮编: " + code +
"\nE-mail: " + email +
"\n\n请确认客户信息是否正确");
messageBox.setText("新客户添加确认");
if(messageBox.open() == SWT.OK)
{
if(addCustomer(name,gender,phone,address,code,email))
{
messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
messageBox.setMessage("新客户添加完成");
messageBox.setText("完成");
messageBox.open();
}
else
{
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("新客户添加失败,请确认输入信息是否正确");
messageBox.setText("添加失败");
messageBox.open();
}
}
}
if(flag)
clearAll();
flag = true;
}
}
});
button_2.setText("提 交");
button_2.setBounds(65, 291, 65, 25);
final Button button_3 = new Button(shell, SWT.NONE);
button_3.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
button.setSelection(true);
clearAll();
}
});
button_3.setText("清 空");
button_3.setBounds(136, 291, 65, 25);
final Button button_4 = new Button(shell, SWT.NONE);
button_4.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
shell.dispose();
}
});
button_4.setText("返 回");
button_4.setBounds(240, 291, 65, 25);
final Label label_5 = new Label(shell, SWT.NONE);
label_5.setAlignment(SWT.CENTER);
label_5.setFont(SWTResourceManager.getFont("新宋体", 14, SWT.BOLD));
label_5.setText("请填写新客户信息");
label_5.setBounds(0, 20, 381, 25);
}
private void clearAll(){
text.setText("");
text_1.setText("");
text_2.setText("");
text_3.setText("");
text_4.setText("");
name = "";
phone = "";
address = "";
email = "";
code = "";
sex = "";
}
public boolean addCustomer(String name,boolean gender,String phone,String address,String code,String email){
Connection con;
try{
DBCon dbc = new DBCon();
con = DriverManager.getConnection(dbc.url,id,pwd);
CallableStatement cs = con.prepareCall("{call dbo.sp_AddCustomer(?,?,?,?,?,?)}");
cs.setString(1, name);
cs.setBoolean(2, gender);
cs.setString(3, phone);
cs.setString(4, address);
cs.setString(5, code);
cs.setString(6, email);
cs.execute();
dbc.dbClose(con);
return true;
}
catch(Exception e){
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -