📄 salemanger.java
字号:
/** * @(#)SaleManger.java 1.0 2007/08/24 * /** * 销售简易程序 * @version 1.0 2007/08/24 * @author 傅意哲 */import java.awt.*;import java.awt.event.*;import java.awt.Dimension;import java.awt.GridLayout;import javax.swing.*;import javax.swing.table.*;import javax.swing.event.*;import javax.swing.border.*;import javax.swing.text.*;import javax.swing.event.TableModelEvent;import java.util.Vector;import java.text.*;import java.io.*;import java.util.*;/** * 销售系统主页面类 * */public class SaleManger implements LayoutManager { static String[] ConnectOptionNames = { "登陆" }; static String ConnectTitle = "登陆"; Dimension origin = new Dimension(0, 0); Icon icon=new ImageIcon("image/lib.gif"); Icon icon1=new ImageIcon("image/suc.GIF"); Icon payIcon=new ImageIcon("image/Pay.GIF"); Icon BuyIcon=new ImageIcon("image/Buy.GIF"); Icon DelIcon=new ImageIcon("image/Del.GIF");//? //创建菜单 JMenu About=new JMenu("系统"); JMenuItem AboutSystem=new JMenuItem("关于系统",icon1); JMenuBar bar=new JMenuBar();//创建菜单栏 JButton BuyButton; //购买按钮 JButton ManagerButton; //管理按钮 JButton PayButton; //结算按钮 JPanel connectionPanel; JFrame frame; // The query/results window. JLabel userNameLabel; //用户名标签 JTextField userNameField; //用户名输入框 JLabel passwordLabel; //密码标签 JPasswordField passwordField; //密码输入框 // JLabel queryLabel; // JTextArea queryTextArea; // JComponent queryAggregate; // JLabel serverLabel; // JTextField serverField; // JLabel driverLabel; // JTextField driverField; JPanel fieldPane3; JLabel PIdLabel; //商品编码标签 JTextField TIdField; //商品编码输入框 JLabel PNameLabel; //商品名称标签 JTextField TNameField; //商品名称输入框 JLabel PPriceLabel; //商品单价标签 JMoneyField TPriceField; //商品单价输入框 JLabel PQualityLabel; //商品购买数量标签 JMoneyField TQualityField; //商品购买数量输入框 JPanel RightPanel1; JLabel PCostLabel; //商品需付标签 JLabel PTotalCostLabel; //商品总价标签 JMoneyField TTotalCostField; //商品总价输入框 JMoneyField TCostField; //商品需付输入框 JLabel PPayLabel; //商品支付标签 JMoneyField TPayField; //商品支付输入框 JLabel PChangeLabel; //商品找零标签 JMoneyField TChangeField; //商品找零输入框 JLabel TChangeMethodLable; //商品找零组合 JPanel BottomPanel1; JPanel BottomPanel2; JButton DelButton; //取消商品按钮 JPanel mainPanel; JTable table; //table列表 DefaultTableModel model; //table列表模式 // TableSorter sorter; JDBCAdapter dataBase; //数据库 JScrollPane tableAggregate; User SystemUser; //用户 /** * 获取找零 * */ public void GetChangeResult(){ double money=StrtoDouble(TPayField.getText())-StrtoDouble(TCostField.getText()); DecimalFormat df1=new DecimalFormat("0.0"); String str=df1.format(money);//format? money=StrtoDouble(str); TChangeMethodLable.setText(getChangeMethod(money)); TChangeField.setText(str);//这里可以是money } /** *@(#)用户信息类 */ public class User{ String Uid; String Uname; int Utype; public String getUserId(){ return Uid; } public void setUserId(String UserId){ Uid=UserId; } public String getUserName(){ return Uname; } public void setUserName(String UserName){ Uname=UserName; } public int getUserType(){ return Utype; } public void setUserType(int UserType){ Utype=UserType; } } /** * 新建JTextField Document,实现输入格式化,响应JTextField文本内容更改事件//? * */ class GetMoneyDocument extends PlainDocument { int maxLen=Integer.MAX_VALUE; /*[输入框最大长度]*/ int Digitale=100; /*[输入框小数位最大长度]*/ int thisLen=0; /*[当前输入框字符串长度]*/ /** * 构造GetMoneyDocument类 * @param maxLength 输入框最大长度 * @param Dig 输入框小数位最大长度 * */ public GetMoneyDocument(int maxLength,int Dig) { this.maxLen=maxLength; this.Digitale=Dig; } /**输入字符影响事件 * @param offs 起始偏移量,该值 >= 0 * @param str 要插入的字符串;null/空字符串不执行任何操作 * @param a 插入内容的属性 * */ public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{ if(str==null||thisLen>=maxLen) return; char ch=str.charAt(0); thisLen=this.getContent().length(); String str_all=this.getContent().getString(0,thisLen); if(ch>='0'&&ch<='9'){ if(str_all.indexOf('.')!=-1&&(thisLen-str_all.indexOf('.')-1>Digitale)){ return; } thisLen++; super.insertString(offs, str, a); GetChangeResult(); return; } if(str.equals(".")){ if(str_all.indexOf('.')==-1){ thisLen++; super.insertString(offs, str, a); GetChangeResult(); } } } /** * 移除字符响应事件 * @param offs 起始偏移量,该值 >= 0 * @param len 移除字符串长度 * */ public void remove(int offs,int len) throws BadLocationException{ thisLen-=len; super.remove(offs, len); GetChangeResult(); } } /** * 限定输入框可输入字符长度 * {@param maxlen 输入框最大长度} * */ class MaxLenDocument extends PlainDocument{ int maxlen=0; public MaxLenDocument(int len){ maxlen=len; } /** * 输入字符响应事件 * @param offset 起始偏移量,该值 >= 0 * @param s 要插入的字符串;null/空字符串不执行任何操作 * @param attrSet 插入内容的属性 * */ public void insertString(int offset, String s, AttributeSet attrSet) throws BadLocationException { if(this.getContent().length()>maxlen){/*[商品编码长度超过编码长度,禁止输入]*/ return; } if(this.getContent().length()==maxlen){/*[商品编码达到编码长度]*/ super.insertString(offset,s,attrSet); String PId=this.getText(0, this.getContent().length()); if(dataBase.executeQuery("select ID,NAME,Price from product where ID='"+PId.trim()+"'")){ Vector Vc=dataBase.GetVector(); if(Vc.isEmpty()){/*[数据库中存在该商品编码的商品]*/ TNameField.setEditable(true); TPriceField.setEditable(true); return; } else{/*[数据库中不存在该商品编码的商品]*/ Vector newRow=(Vector)Vc.get(0); TNameField.setText(newRow.get(1).toString()); TPriceField.setText(newRow.get(2).toString()); return; } } return; } else{/*[商品编码没有达到编码长度]*/ TNameField.setEditable(false); TPriceField.setEditable(false); } TNameField.setEditable(false); TPriceField.setEditable(false); super.insertString(offset,s,attrSet); } /** * 移除字符响应事件 * @param offs 起始偏移量,该值 >= 0 * @param len 移除字符串长度 * */ public void remove(int offs,int len) throws BadLocationException{ TNameField.setEditable(false); TPriceField.setEditable(false); TNameField.setText(""); TPriceField.setText("0"); super.remove(offs, len); } } /** * Brigs up a JDialog using JOptionPane containing the connectionPanel. * 输入用户名密码后电击“登陆”,则尝试登陆。 */ boolean activateConnectionDialog() { if(JOptionPane.showOptionDialog(tableAggregate, connectionPanel, ConnectTitle, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, ConnectOptionNames, ConnectOptionNames[0]) == 0) { // connect(); if(!dataBase.executeQuery("select uid,uname,type from user where uid='"+userNameField.getText() +"' and PassWord='"+passwordField.getText()+"'")){ JOptionPane.showMessageDialog(tableAggregate,"数据库操作失败","错误",JOptionPane.ERROR_MESSAGE, new ImageIcon("image/caidanx.gif")); return true; } Vector Vt=dataBase.GetVector(); if(Vt.isEmpty()){ JOptionPane.showMessageDialog(tableAggregate,"用户名或密码错误","提示",JOptionPane.ERROR_MESSAGE, new ImageIcon("image/caidanx.gif")); return true; } else{ SystemUser=new User(); Vector newRow=(Vector)Vt.get(0); SystemUser.setUserId(newRow.get(0).toString()); SystemUser.setUserName(newRow.get(1).toString()); SystemUser.setUserType(Integer.parseInt(newRow.get(2).toString())); } frame.setVisible(true); return false; } else if(!frame.isVisible()){ System.exit(0); return true; } return false; } /** * Creates the connectionPanel, which will contain all the fields for * the connection information. */ public void createConnectionDialog() { // Create the labels and text fields. userNameLabel = new JLabel("用户名: ", JLabel.LEFT); userNameField = new JTextField(""); passwordLabel = new JLabel("密 码: ", JLabel.LEFT); passwordField = new JPasswordField(""); connectionPanel = new JPanel(false); connectionPanel.setLayout(new BoxLayout(connectionPanel, BoxLayout.X_AXIS)); JPanel namePanel = new JPanel(false); namePanel.setLayout(new GridLayout(2, 1)); namePanel.add(userNameLabel); namePanel.add(passwordLabel); JPanel fieldPanel = new JPanel(false); fieldPanel.setLayout(new GridLayout(4, 1)); fieldPanel.add(userNameLabel); fieldPanel.add(userNameField); fieldPanel.add(passwordLabel); fieldPanel.add(passwordField); //fieldPanel.add(serverField); // fieldPanel.add(driverField); connect();// ionPanel.add(namePanel); connectionPanel.add(fieldPanel); } /** * 字符串转换为双精度数字 * @param str 要转换的字符串
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -