📄 reportpane.java
字号:
/* Copyright 2002 MonkeyStar SoftWare, Inc. All rights reserved.
* The Start of the ATM Project for Product Company
* @author Wujun, Student Number:09800134
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.BorderFactory.*;
import javax.swing.border.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.io.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.table.*;
public class ReportPane extends JPanel {
Component f;
Socket Winsock;
String[] s = {"南瓜","柚子","葡萄","弥猴桃","流连","水蜜桃","仙人果","凤梨","荔枝","草莓","蕃茄","西瓜"};
Font fontword = new Font("楷体_GB2312",Font.BOLD,18);
tableModel tm=new tableModel();
JTable tbReport=new JTable(tm);
Vector VecReport;
JScrollPane tbScroll = new JScrollPane(tbReport);
Icon imgBtBegin = new ImageIcon(".\\img\\btbegin.jpg");
Icon imgBtBeginOn = new ImageIcon(".\\img\\btbegin_over.jpg");
Icon imgBtBeginDown= new ImageIcon(".\\img\\btbegin_down.jpg");
Icon imgBtProfit= new ImageIcon(".\\img\\btprofit.jpg");
Icon imgBtProfitOn= new ImageIcon(".\\img\\btprofit_over.jpg");
Icon imgBtProfitDown= new ImageIcon(".\\img\\btprofit_down.jpg");
JLabel l1 = new JLabel("最好销的商品是:");
JLabel l2 = new JLabel("最冷落的商品是:");
JLabel l3 = new JLabel("");
JLabel l4 = new JLabel("");
JLabel PicMax=new JLabel();
JLabel PicMin=new JLabel();
JRadioButton r1 = new JRadioButton("");
JRadioButton r2 = new JRadioButton("");
JRadioButton r3 = new JRadioButton("");
JRadioButton r4 = new JRadioButton("");
JRadioButton r5 = new JRadioButton("");
JRadioButton r6 = new JRadioButton("");
JButton btStart = new JButton();
JButton btProfit = new JButton();
private ActionListener listener=new ActionResponse();
public ReportPane(Component ComponentF)
{
this.setLayout(null);
f=ComponentF;
tbReport.setRowHeight(30);
tbScroll.setBounds(0,0,360,300);
btStart.setBounds(380,10,122,35);
btProfit.setBounds(380,50,122,35);
l1.setBounds(365,100,150,20);
l3.setBounds(380,125,80,20);
l2.setBounds(365,150,150,20);
l4.setBounds(380,175,80,20);
l1.setForeground(Color.red);
l3.setForeground(Color.red);
l2.setForeground(Color.blue);
l4.setForeground(Color.blue);
l1.setFont(fontword);
l2.setFont(fontword);
l3.setFont(fontword);
l4.setFont(fontword);
tbScroll.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black,1),"Sales Report",TitledBorder.CENTER,TitledBorder.TOP));
btStart.setRolloverEnabled(true);
btStart.setIcon(imgBtBegin);
btStart.setRolloverIcon(imgBtBeginOn);
btStart.setPressedIcon(imgBtBeginDown);
btStart.setBorder(null);
btStart.addActionListener(listener);
btProfit.setRolloverEnabled(true);
btProfit.setIcon(imgBtProfit);
btProfit.setRolloverIcon(imgBtProfitOn);
btProfit.setPressedIcon(imgBtProfitDown);
btProfit.setBorder(null);
btProfit.addActionListener(listener);
PicMax.setBounds(430,120,50,30);
PicMin.setBounds(430,170,50,30);
this.add(tbScroll);
this.add(btStart);
this.add(btProfit);
this.add(l1);
this.add(l2);
this.add(l3);
this.add(l4);
this.add(PicMax);
this.add(PicMin);
}
public String name2pic(String name)
{
int point=0;
for (int i=0;i<s.length;i++)
{
if (s[i].toString().equals(name))
point=i;
}
return String.valueOf(point+1);
}
private void CalcLog()
{
boolean Find=false;
int tmpAmount=0;
int max=0;
int min=0;
int MaxPoint=0;
int MinPoint=0;
double tmpPrice=0;
String tmpProduct="";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:company","","");
Statement s = c.createStatement();
ResultSet r = s.executeQuery("SELECT * FROM log");
VecReport = new Vector();
while(r.next())
{
tmpAmount=r.getInt("Amount");
tmpProduct=r.getString("Product");
tmpPrice=r.getDouble("Cash");
//System.out.println(tmpProduct+String.valueOf(tmpAmount)+String.valueOf(tmpPrice));
for(int i=0;i<VecReport.size();i++)
{
if (tmpProduct.equals(((tableItem)VecReport.elementAt(i)).Name))
{
Find=true;
((tableItem)VecReport.elementAt(i)).BuyNum+=tmpAmount;
((tableItem)VecReport.elementAt(i)).Cash+=tmpPrice;
}
}
if(Find==false)
{
VecReport.addElement(new tableItem(tmpProduct,tmpAmount,tmpPrice));
}
Find=false;
}
s.close();
for(int i=0;i<VecReport.size();i++)
{
if (i==0)
{
max=((tableItem)VecReport.elementAt(i)).BuyNum;
min=((tableItem)VecReport.elementAt(i)).BuyNum;
}
else
{
if (max<((tableItem)VecReport.elementAt(i)).BuyNum)
{
max=((tableItem)VecReport.elementAt(i)).BuyNum;
MaxPoint=i;
}
if(min>((tableItem)VecReport.elementAt(i)).BuyNum)
{
min=((tableItem)VecReport.elementAt(i)).BuyNum;
MinPoint=i;
}
}
}
l3.setText(((tableItem)VecReport.elementAt(MaxPoint)).Name);
l4.setText(((tableItem)VecReport.elementAt(MinPoint)).Name);
PicMax.setIcon(new ImageIcon(".\\img\\small\\list"+name2pic(((tableItem)VecReport.elementAt(MaxPoint)).Name)+".jpg"));
PicMin.setIcon(new ImageIcon(".\\img\\small\\list"+name2pic(((tableItem)VecReport.elementAt(MinPoint)).Name)+".jpg"));
tm.setItem(VecReport);}catch(SQLException Exp)
{System.out.println(Exp.toString()); }
catch(ClassNotFoundException Exp)
{JOptionPane.showMessageDialog(f, Exp.toString(), "错误:", JOptionPane.INFORMATION_MESSAGE);}
catch(Exception Exp)
{System.out.println(Exp.toString()); }
}
public void Msgbox(String msg)
{
JOptionPane.showMessageDialog(f, "在联机过程中发生了网络故障,请检查网络地址是否有效。\n出错信息:\n"+msg, "错误:", JOptionPane.ERROR_MESSAGE);
}
class ActionResponse implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if (e.getSource()==btStart)
{
CalcLog();
}
if (e.getSource()==btProfit)
{
String answer=JOptionPane.showInputDialog(f,"即将向银行服务器查询帐目\n请输入银行服务器地址:","联机提示",JOptionPane.QUESTION_MESSAGE);
if (answer.length()>8){
try{
Winsock = new Socket(answer,9000);
SendThread st = new SendThread(Winsock,f);
st.start();
}
catch(IOException exp)
{Msgbox(exp.toString());}
catch(Exception exp)
{Msgbox(exp.toString());}
}
}
}
}
}
class tableModel extends AbstractTableModel{
Object[][] p = {};
String[] Title = {"图片","商品名","已售数量","销售金额"};
String[] s = {"南瓜","柚子","葡萄","弥猴桃","流连","水蜜桃","仙人果","凤梨","荔枝","草莓","蕃茄","西瓜"};
public String name2pic(String name)
{
int point=0;
for (int i=0;i<s.length;i++)
{
if (s[i].toString().equals(name))
point=i;
}
return String.valueOf(point+1);
}
public void setItem(Vector m_Report)
{
Vector VecProduct=m_Report;
p=new Object[VecProduct.size()][4];
for (int i=0;i<VecProduct.size();i++)
{
p[i][0]=new ImageIcon(".\\img\\small\\list"+name2pic(((tableItem)VecProduct.elementAt(i)).Name)+".jpg");
p[i][1]=((tableItem)VecProduct.elementAt(i)).Name;
p[i][2]=new Integer((int)((tableItem)VecProduct.elementAt(i)).BuyNum);
p[i][3]=new Double((double)((tableItem)VecProduct.elementAt(i)).Cash);
}
this.fireTableDataChanged();
}
public int getColumnCount() {
return Title.length;
}
public int getRowCount() {
return p.length;
}
public String getColumnName(int col) {
return Title[col];
}
public Object getValueAt(int row, int col) {
return p[row][col];
}
public boolean isCellEditable(int row, int col) {return true;}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
class tableItem
{
public String Name;
public int BuyNum;
public double Cash;
public tableItem(String name,int buynum,double price){
Name=name;
BuyNum=buynum;
Cash=price;}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -