📄 addsell.java
字号:
import java.awt.Toolkit;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Date;
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.Combo;
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 AddSell extends Dialog {
private Combo combo_5;
private Combo combo_4;
private Combo combo_3;
protected Object result;
protected Shell shell;
private MessageBox messageBox;
private Combo combo_2;
private Combo combo_1;
private Combo combo;
private Text text_2;
private Text text_1;
private Text text;
private String id;
private String pwd;
private Date datetime;
private int productID;
private int customerID;
private int staffID;
private double price;
private int sellCount;
private boolean flag = true;
/**
* Create the dialog
* @param parent
* @param style
*/
public AddSell(Shell parent, int style) {
super(parent, style);
}
/**
* Create the dialog
* @param parent
*/
public AddSell(Shell parent,String id,String pwd) {
this(parent, SWT.NONE);
this.id = id;
this.pwd = 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(346, 385);
shell.setText("添加新的销售信息");
//
final Label label = new Label(shell, SWT.CENTER);
label.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
label.setText("产品名称");
label.setBounds(55, 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(55, 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(55, 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(55, 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(55, 228, 65, 17);
final Label emailLabel = new Label(shell, SWT.CENTER);
emailLabel.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
emailLabel.setText("成交量");
emailLabel.setBounds(55, 266, 65, 17);
combo = new Combo(shell, SWT.READ_ONLY);
combo.setVisibleItemCount(10);
combo.setBounds(140, 60, 111, 22);
combo_1 = new Combo(shell, SWT.READ_ONLY);
combo_1.setVisibleItemCount(10);
combo_1.setBounds(140, 99, 111, 22);
combo_2 = new Combo(shell, SWT.READ_ONLY);
combo_2.setVisibleItemCount(10);
combo_2.setBounds(140, 136, 111, 22);
combo_3 = new Combo(shell, SWT.READ_ONLY);
combo_3.setBounds(257, 62, 53, 20);
combo_3.setVisible(false);
combo_4 = new Combo(shell, SWT.READ_ONLY);
combo_4.setBounds(257, 99, 53, 20);
combo_4.setVisible(false);
combo_5 = new Combo(shell, SWT.READ_ONLY);
combo_5.setBounds(257, 136, 53, 20);
combo_5.setVisible(false);
getAllInfor("Product",combo,combo_3);
getAllInfor("Customer",combo_1,combo_4);
getAllInfor("Staff",combo_2,combo_5);
text = new Text(shell, SWT.BORDER);
text.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
text.setBounds(140, 171, 111, 25);
text_1 = new Text(shell, SWT.BORDER);
text_1.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
text_1.setBounds(140, 225, 111, 25);
text_2 = new Text(shell, SWT.BORDER);
text_2.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
text_2.setBounds(140, 263, 111, 25);
final Button button_2 = new Button(shell, SWT.NONE);
button_2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
flag = true;
if(combo.getSelectionIndex() == -1 || combo_1.getSelectionIndex() == -1
|| combo_2.getSelectionIndex() == -1)
{
messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
messageBox.setMessage("请选择[产品名称][客户姓名][销售人员]");
messageBox.setText("添加销售信息");
messageBox.open();
flag = false;
}
if(flag)
try{
datetime = Date.valueOf(text.getText());
}
catch(Exception px)
{
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("日期格式错误,请重新输入");
messageBox.setText("添加销售信息");
messageBox.open();
flag = false;
}
if(flag)
try
{
price = Double.parseDouble(text_1.getText());
sellCount = Integer.parseInt(text_2.getText());
}
catch(NumberFormatException m)
{
messageBox = new MessageBox(shell,SWT.ICON_ERROR);
messageBox.setMessage("价格和销售量输入错误");
messageBox.setText("添加销售信息");
messageBox.open();
flag = false;
}
if(flag)
{
productID = Integer.parseInt(combo_3.getItem(combo.getSelectionIndex()));
customerID = Integer.parseInt(combo_4.getItem(combo_1.getSelectionIndex()));
staffID = Integer.parseInt(combo_5.getItem(combo_2.getSelectionIndex()));
messageBox = new MessageBox(shell,SWT.ICON_QUESTION|SWT.OK|SWT.CANCEL);
messageBox.setMessage("产品名称: " + combo.getItem(combo.getSelectionIndex()) +
"\n客户姓名: " + combo_1.getItem(combo_1.getSelectionIndex()) +
"\n销售人员: " + combo_2.getItem(combo_2.getSelectionIndex()) +
"\n销售日期: " + datetime +
"\n单价(元): " + price +
"\n销售量: " + sellCount +
"\n\n请确认销售信息是否正确");
messageBox.setText("信息确认");
if(messageBox.open() == SWT.OK)
{
if(addSell(productID,customerID,staffID,datetime,price,sellCount))
{
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();
flag = false;
}
}
}
if(flag)
clearAll();
}
});
button_2.setText("提 交");
button_2.setBounds(55, 300, 65, 23);
final Button button_3 = new Button(shell, SWT.NONE);
button_3.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
text.setText("");
text_1.setText("");
text_2.setText("");
combo.deselectAll();
combo_1.deselectAll();
combo_2.deselectAll();
}
});
button_3.setText("重 填");
button_3.setBounds(126, 300, 65, 23);
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(220, 300, 65, 23);
final Label label_5 = new Label(shell, SWT.NONE);
label_5.setFont(SWTResourceManager.getFont("新宋体", 14, SWT.BOLD));
label_5.setAlignment(SWT.CENTER);
label_5.setText("请填写新销售信息");
label_5.setBounds(0, 18, 340, 24);
final Label label_6 = new Label(shell, SWT.NONE);
label_6.setText("例如 2008-06-26");
label_6.setBounds(140, 202, 123, 17);
}
private void clearAll(){
text.setText("");
text_1.setText("");
text_2.setText("");
combo.deselectAll();
combo_1.deselectAll();
combo_2.deselectAll();
combo_3.deselectAll();
combo_4.deselectAll();
combo_5.deselectAll();
}
public void getAllInfor(String table,Combo combo,Combo comboID){//sql
Connection con;
Statement stmt;
try{
combo.removeAll();
comboID.removeAll();
DBCon dbc = new DBCon();
con = DriverManager.getConnection(dbc.url,id,pwd);
stmt = con.createStatement();
String query = "select ID,Name from " + table +" order by Name";
ResultSet rs = stmt.executeQuery(query);
while( rs.next()){
combo.add(rs.getString("Name"));
comboID.add(rs.getString("ID"));
}
dbc.dbClose(con,rs);
}
catch(Exception e){
e.printStackTrace();
}
}
public boolean addSell(int pname,int cname,int sname,Date date,double price,int count){//sql
Connection con;
try{
DBCon dbc = new DBCon();
con = DriverManager.getConnection(dbc.url,id,pwd);
CallableStatement cs = con.prepareCall("{call dbo.sp_AddSell(?,?,?,?,?,?)}");
cs.setInt(1, pname);
cs.setInt(2, cname);
cs.setInt(3, sname);
cs.setDate(4, date);
cs.setDouble(5, price);
cs.setInt(6, count);
cs.execute();
dbc.dbClose(con);
return true;
}
catch(Exception e){
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -