📄 mainframe.java
字号:
{
JOptionPane.showMessageDialog(f, Exp.toString(), "错误:", JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception Exp)
{
}
}
class ActionResponse implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if (e.getSource()==btPay)
{
if(m_Bill.size()>0){
String title = "提示:";
int type = JOptionPane.INFORMATION_MESSAGE;
String[] options={"确定","取消"};
int optionType=JOptionPane.YES_NO_OPTION;
int messageType=JOptionPane.QUESTION_MESSAGE;
String message = "确实要购买选择的商品吗?\n如是请插入ATM卡,并按‘确定’按钮。\n不买请按‘取消’按钮。";
int result = JOptionPane.showOptionDialog(f,message, title, optionType,messageType,null,options,options[1]);
if (result == JOptionPane.YES_OPTION)
{
ReadAtmID AtmReader = new ReadAtmID(f);
atmCard.setText(String.valueOf(AtmReader.ReadCardID(MainF)));
}
}
else
{JOptionPane.showMessageDialog(f,"你还没有选择任何要买商品,无法进行交易!","提示:",JOptionPane.INFORMATION_MESSAGE);}
}
if (e.getSource()==btPick)
{
String title = "提示:";
int type = JOptionPane.INFORMATION_MESSAGE;
String[] options={"付款","取消"};
int optionType=JOptionPane.YES_NO_OPTION;
int messageType=JOptionPane.QUESTION_MESSAGE;
String message = "共需要"+String.valueOf(SumCost)+" 元,是否付款?";
int result = JOptionPane.showOptionDialog(f,message, title, optionType,messageType,null,options,options[1]);
if (result == JOptionPane.YES_OPTION)
{
try {
Clientsock = new Socket((new ReadConfig()).getBankSrvIP(),8848);
Companysock = new Socket((new ReadConfig()).getCompanyIP(),8000);
st = new CustomerThread(Clientsock,Companysock,f,MainF);
st.start();
st.SendBuy(SumCost,atmCard.getText().trim(),String.valueOf(atmPass.getPassword()));
}
catch(IOException exp)
{JOptionPane.showMessageDialog(f,"与银行或公司服务器连接出错,无法刷卡,交易失败!\n"+exp.toString(), "提示:",JOptionPane.INFORMATION_MESSAGE);}
}
}
if (e.getSource()==btBack)
{
if(m_Bill.size()>0){
String title = "注意:";
int type = JOptionPane.INFORMATION_MESSAGE;
String[] options={"是的","取消"};
int optionType=JOptionPane.YES_NO_OPTION;
int messageType=JOptionPane.QUESTION_MESSAGE;
String message = "你真的要把购物袋中所选的商品全部放回原处,并重新选择吗?\n(您也可以在列表中双击要取消的某样商品以放回原处)";
int result = JOptionPane.showOptionDialog(f,message, title, optionType,messageType,null,options,options[1]);
if (result == JOptionPane.YES_OPTION)
{
for(int i=0;i<ListModel.getSize();i++)
{
for (int j=0;j<12;j++)
{
if ((String)ListModel.elementAt(i)==((Product)m_Product.elementAt(j)).Name)
lblProNum[j].setText("数量:"+String.valueOf(++((Product)m_Product.elementAt(j)).CurNum)+" 个");
}
}
ListModel.removeAllElements();
BagList.setModel(ListModel);
m_Bill.removeAllElements();
tm.setItem(m_Bill);
}
}
}
for (int i=0;i<12;i++)
{
if (e.getSource()==BuyButton[i])
{
pickProduct(i);
}
}
getCost();
}
}
public void pickProduct(int j)
{
boolean FindFlag=false;
int Num=((Product)m_Product.elementAt(j)).CurNum;
if (Num>0)
{
String Name=((Product)m_Product.elementAt(j)).Name;
double Price=((Product)m_Product.elementAt(j)).Price;
lblProNum[j].setText("数量:"+String.valueOf(--((Product)m_Product.elementAt(j)).CurNum)+" 个");
ListModel.addElement(Name);
BagList.setModel(ListModel);
for(int i=0;i<m_Bill.size();i++)
{
if (Name==((BillItem)m_Bill.elementAt(i)).Name)
{
((BillItem)m_Bill.elementAt(i)).buyNum++;
((BillItem)m_Bill.elementAt(i)).Money=((BillItem)m_Bill.elementAt(i)).buyNum*Price;
FindFlag=true;
}
}
if (!FindFlag)
m_Bill.addElement(new BillItem(Name,1,Price));
FindFlag=false;
tm.setItem(m_Bill);
}
else
{
JOptionPane.showMessageDialog(f,((Product)m_Product.elementAt(j)).Name+"在售货机上已无更多存货了!\n我们会通知生产商补充该商品。\n一定很快到货!请您过一段时间再来购买。\n 谢谢合作!", "抱歉", JOptionPane.INFORMATION_MESSAGE);
}
if(Num<=5)
{
try{
RequestSock=new Socket((new ReadConfig()).getCompanyIP(),8000);
DataIn = new DataInputStream(RequestSock.getInputStream());
DataOut=new DataOutputStream(RequestSock.getOutputStream());
DataOut.writeUTF("SEND:|NAME:"+((Product)m_Product.elementAt(j)).Name+"|CONT:100|HAVE:"+((Product)m_Product.elementAt(j)).CurNum);
RequestSock.close();}catch (IOException e){}
}
}
public void mouseClicked(MouseEvent e)
{
int index,Num;
if (e.getClickCount()==2)
{
index=BagList.locationToIndex(e.getPoint());
String Name = (String)ListModel.getElementAt(index);
for (int i=0;i<12;i++)
{
if (Name==((Product)m_Product.elementAt(i)).Name)
{
lblProNum[i].setText("数量:"+String.valueOf(++((Product)m_Product.elementAt(i)).CurNum)+" 个");
double Price=((Product)m_Product.elementAt(i)).Price;
for(int x=0;x<m_Bill.size();x++)
{
if (Name==((BillItem)m_Bill.elementAt(x)).Name)
{
((BillItem)m_Bill.elementAt(x)).buyNum--;
((BillItem)m_Bill.elementAt(x)).Money=((BillItem)m_Bill.elementAt(x)).buyNum*Price;
}
}
}
}
int y=0;
while (y<m_Bill.size())
{
if (((BillItem)m_Bill.elementAt(y)).buyNum==0)
{m_Bill.removeElementAt(y);}
y++;
}
tm.setItem(m_Bill);
ListModel.removeElementAt(index);
BagList.setModel(ListModel);
getCost();
}
}
public MainFrame() {
contentPane.setLayout(null);
setShell();
f.setTitle("Auto Services Machine");
f.setLocation(30,20);
f.setSize(760,550);
f.setVisible(true);
StartNet();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
class CellRenderer extends JLabel implements ListCellRenderer
{
CellRenderer()
{
setOpaque(true);
}
public String picListName(String picName)
{
String tmpName=new String(".\\food\\small\\list1.jpg");
for (int i=0;i<12;i++)
{
if (picName.equals(((Product)m_Product.elementAt(i)).Name))
tmpName=new String(".\\food\\small\\list"+String.valueOf(i+1)+".jpg");
}
return tmpName;
}
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
if (value != null)
{
setText(value.toString());
setIcon(new ImageIcon(picListName(value.toString())));
}
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
}
else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
return this;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -