📄 editcustomer.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 EditCustomer 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 int c_id;
private String name = "",phone = "",address = "",code = "",email = "",sex = "";
private boolean gender,flag = true;
/**
* Create the dialog
* @param parent
* @param style
*/
public EditCustomer(Shell parent, int style,String id,String pwd) {
super(parent, style);
}
/**
* Create the dialog
* @param parent
*/
public EditCustomer(Shell parent,String id,String pwd,int c_id,
String name,boolean gender,String phone,String address,String code,String email) {
this(parent, SWT.NONE,id,pwd);
this.id = id;
this.pwd = pwd;
this.c_id = c_id;
this.name = name;
this.phone = phone;
this.address = address;
this.code = code;
this.email = email;
this.gender = gender;
}
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);
button.setSelection(this.gender);
setAll();
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_INFORMATION);
messageBox.setMessage("客户姓名过长\n请重新输入姓名");
messageBox.setText("客户姓名");
messageBox.open();
flag = false;
}
else
if((text_1.getText()).length() > 20)
{
messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
messageBox.setMessage("联系电话过长\n请重新输入联系电话");
messageBox.setText("联系电话");
messageBox.open();
flag = false;
}
else
if((text_2.getText()).length() > 20)
{
messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
messageBox.setMessage("地址过长\n请重新输入地址");
messageBox.setText("地址");
messageBox.open();
flag = false;
}
else
if((text_3.getText()).length() > 10)
{
messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
messageBox.setMessage("邮编过长\n请重新输入邮编");
messageBox.setText("邮编");
messageBox.open();
flag = false;
}
else
if((text_4.getText()).length() > 20)
{
messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
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)
{
addCustomer(c_id,name,gender,phone,address,code,email);
messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
messageBox.setMessage("客户信息修改完成");
messageBox.setText("修改");
messageBox.open();
shell.dispose();
}
}
}
}
});
button_2.setText("提 交");
button_2.setBounds(105, 290, 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(215, 290, 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 setAll(){
text.setText(name);
text_1.setText(phone);
text_2.setText(address);
text_3.setText(code);
text_4.setText(email);
}
public void addCustomer(int c_id,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_UpdateCustomer(?,?,?,?,?,?,?)}");
cs.setInt(1, c_id);
cs.setString(2, name);
cs.setBoolean(3, gender);
cs.setString(4, phone);
cs.setString(5, address);
cs.setString(6, code);
cs.setString(7, email);
cs.execute();
dbc.dbClose(con);
}
catch(Exception e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -