📄 mainframe.java
字号:
/* Copyright 2002 MonkeyStar SoftWare, Inc. All rights reserved.
* The Start of the ATM Project for Auto Services
* @author Wujun, Student Number:09800134
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.BorderFactory.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.io.*;
public class MainFrame extends MouseAdapter{
Font fontword = new Font("宋体",Font.BOLD,13);
JFrame f = new JFrame();
MainFrame MainF=this;
Socket Clientsock;
Socket Companysock;
Socket RequestSock;
DataInputStream DataIn;
DataOutputStream DataOut;
CustomerThread st;
JLabel txtPrice = new JLabel("0.0 元",JLabel.CENTER);
Container contentPane = f.getContentPane();
JLabel lblCard = new JLabel("卡号:");
JLabel lblPass = new JLabel("密码:");
JTextField atmCard = new JTextField(10);
JPasswordField atmPass = new JPasswordField(new PasswordRestrict(6),"",6);
JButton BuyButton[] = new JButton[12];
JLabel lblProName[] = new JLabel[12];
JLabel lblProNum[] = new JLabel[12];
JLabel lblPrice[] = new JLabel[12];
JLabel picProduct[] = new JLabel[12];
Icon imgPick = new ImageIcon(".\\food\\btpick.jpg");
Icon imgPickOn = new ImageIcon(".\\food\\btpick_over.jpg");
Icon imgPickDown= new ImageIcon(".\\food\\btpick_down.jpg");
Icon imgBuy = new ImageIcon(".\\food\\btbuy.jpg");
Icon imgBuyOn = new ImageIcon(".\\food\\btbuy_over.jpg");
Icon imgButDown = new ImageIcon(".\\food\\btbuy_down.jpg");
JLabel imgBanner = new JLabel(new ImageIcon(".\\food\\banner.jpg"));
JLabel imgBag = new JLabel(new ImageIcon(".\\food\\baglist.jpg"));
JLabel imgHand = new JLabel(new ImageIcon(".\\food\\hand.jpg"));
Icon imgBack = new ImageIcon(".\\food\\btback.jpg");
Icon imgBackOn = new ImageIcon(".\\food\\btback_over.jpg");
Icon imgBackDown = new ImageIcon(".\\food\\btback_down.jpg");
Icon imgPay = new ImageIcon(".\\food\\btPay.jpg");
Icon imgPayOn = new ImageIcon(".\\food\\btPay_over.jpg");
Icon imgPayDown = new ImageIcon(".\\food\\btPay_down.jpg");
JLabel lblTable = new JLabel("购物明细表");
JButton btBack = new JButton();
JButton btPay = new JButton();
JButton btPick = new JButton();
tableModel tm=new tableModel();
JTable billTable=new JTable(tm);
JScrollPane tableScroll = new JScrollPane(billTable);
DefaultListModel ListModel = new DefaultListModel();
JList BagList = new JList(ListModel);
JScrollPane ListScroll = new JScrollPane(BagList);
private ActionListener listener=new ActionResponse();
Vector m_Product = new Vector();
Vector m_Bill = new Vector();
public double SumCost;
public void StartNet()
{
try{
ServerSocket ListenCompany=new ServerSocket(6000);
System.out.println("The Auto Services begin to listen Company Require!");
(new ListenService(ListenCompany,MainF)).start();
}catch(IOException exp)
{System.out.println("Listen Company Require Failed!");}
}
public void getCost()
{
SumCost=0;
for(int i=0;i<m_Bill.size();i++)
{
SumCost+=((BillItem)m_Bill.elementAt(i)).Money;
}
txtPrice.setText(String.valueOf(SumCost)+" 元");
}
public void payState(boolean state)
{
lblCard.setVisible(state);
lblPass.setVisible(state);
atmCard.setVisible(state);
atmPass.setVisible(state);
btPick.setVisible(state);
btPay.setVisible(!state);
tableScroll.setVisible(!state);
if(state)
lblTable.setText("输入卡密码");
else
{
lblTable.setText("购物明细表");
txtPrice.setText("0.0 元");
atmCard.setText("");
atmPass.setText("");
}
}
public void setShell()
{
int x,y;
x=0;y=0;
imgBanner.setBounds(35,5,500,50);
ListScroll.setBounds(620,65,120,145);
tableScroll.setBounds(610,310,135,150);
btBack.setBounds(630,210,100,36);
btBack.setToolTipText("您可以双击列表中不要的商品以放回原处");
lblTable.setBounds(636,288,110,20);
lblTable.setForeground(Color.blue);
lblTable.setFont(new Font("楷体_GB2312",Font.BOLD,15));
txtPrice.setBorder(BorderFactory.createTitledBorder("Sum Cost:"));
txtPrice.setBounds(625,250,105,40);
txtPrice.setForeground(Color.red);
btPick.setBounds(625,400,105,40);
btPay.setBounds(625,460,105,40);
imgBag.setBounds(620,5,120,60);
imgHand.setBounds(550,10,50,50);
BagList.setBorder(BorderFactory.createTitledBorder("选购的商品"));
BagList.setCellRenderer(new CellRenderer());
BagList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
BagList.addMouseListener(this);
BagList.setToolTipText("双击项目放回所选商品");
btBack.setRolloverEnabled(true);
btBack.setIcon(imgBack);
btBack.setRolloverIcon(imgBackOn);
btBack.setPressedIcon(imgBackDown);
btBack.setBorder(null);
btPay.setRolloverEnabled(true);
btPay.setIcon(imgPay);
btPay.setRolloverIcon(imgPayOn);
btPay.setPressedIcon(imgPayDown);
btPay.setBorder(null);
btPick.setRolloverEnabled(true);
btPick.setIcon(imgPick);
btPick.setRolloverIcon(imgPickOn);
btPick.setPressedIcon(imgPickDown);
btPick.setBorder(null);
lblCard.setBounds(610,330,50,20);
lblPass.setBounds(610,360,50,20);
lblCard.setFont(new Font("楷体_GB2312",Font.BOLD,15));
lblPass.setFont(new Font("楷体_GB2312",Font.BOLD,15));
lblCard.setForeground(Color.red);
lblPass.setForeground(Color.red);
atmCard.setFont(new Font("宋体",Font.BOLD,15));
atmCard.setForeground(Color.blue);
atmPass.setFont(new Font("楷体_GB2312",Font.BOLD,22));
atmCard.setBounds(655,330,80,20);
atmCard.setEditable(false);
atmPass.setBounds(655,360,80,20);
btBack.addActionListener(listener);
btPay.addActionListener(listener);
btPick.addActionListener(listener);
contentPane.add(imgBanner);
contentPane.add(ListScroll);
contentPane.add(imgBag);
contentPane.add(btBack);
contentPane.add(imgHand);
contentPane.add(btPay);
contentPane.add(lblTable);
contentPane.add(txtPrice);
contentPane.add(tableScroll);
contentPane.add(lblCard);
contentPane.add(lblPass);
contentPane.add(atmCard);
contentPane.add(atmPass);
contentPane.add(btPick);
payState(false);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:autosrv","","");
Statement s = c.createStatement();
for (int i=0;i<12;i++)
{
BuyButton[i]=new JButton();
ResultSet r = s.executeQuery("SELECT * FROM Product WHERE ProductID='"+String.valueOf(i+1)+"'");
while(r.next())
{
String tmpName=r.getString("Product");
int tmpNum=r.getInt("Amount");
double tmpPrice=r.getDouble("Price");
lblProName[i]=new JLabel("品名:"+tmpName);
lblProNum[i]=new JLabel("数量:"+String.valueOf(tmpNum)+" 个");
lblPrice[i]=new JLabel("单价:"+String.valueOf(tmpPrice)+" 元");
Product product = new Product(tmpName,tmpPrice,tmpNum);
m_Product.addElement(product);
}
lblProName[i].setFont(fontword);
lblProNum[i].setFont(fontword);
lblPrice[i].setFont(fontword);
picProduct[i]=new JLabel(new ImageIcon(".\\food\\pic"+String.valueOf(i+1)+".jpg"));
x=i%3*200;y=i/3*110;
BuyButton[i].setBounds(x,y+80+60,110,30);
lblProName[i].setBounds(x+105,y+5+60,120,25);
lblProNum[i].setBounds(x+105,y+30+60,120,25);
lblPrice[i].setBounds(x+105,y+55+60,120,25);
picProduct[i].setBounds(x+5,y+60,100,80);
BuyButton[i].setRolloverEnabled(true);
BuyButton[i].setIcon(imgBuy);
BuyButton[i].setRolloverIcon(imgBuyOn);
BuyButton[i].setPressedIcon(imgButDown);
BuyButton[i].setBorder(null);
BuyButton[i].addActionListener(listener);
contentPane.add(BuyButton[i]);
contentPane.add(lblProName[i]);
contentPane.add(lblProNum[i]);
contentPane.add(lblPrice[i]);
contentPane.add(picProduct[i]);
}
s.close();
} catch(SQLException Exp)
{
System.out.println(Exp.toString());
}
catch(ClassNotFoundException Exp)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -