📄 salesystempanel.java
字号:
package saleSystem;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
//import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SaleSystemPanel extends JPanel{
private static double totalPrice=0.0; //定义应付总金额变量
int n=5;
/*Product [] products=new Product[n];{
for (int i = 0; i < products.length; i++)
{
products[i]=new Product();}
}*/
ArrayList<Product> products=new ArrayList<Product>();//用一个数组列表装商品对象,以此来动态添加商品项目
private JLabel codeBarLabel=new JLabel("商品条码");
private JLabel nameLabel=new JLabel("商品名称");
private JLabel priceLabel=new JLabel("商品价格");
private JLabel manufacturerLabel=new JLabel("商品厂家");
//private JLabel manufacturerLabel=new JLabel("商品厂家");
private JLabel totalPriceLabel=new JLabel("购买总额(元)");
private JLabel encourageLabel=new JLabel("奖励金额(元)");
private JLabel shouldPayLabel=new JLabel("应付(元)");
private JLabel payLabel=new JLabel("实付(元)");
private JLabel chargeLabel=new JLabel("找零");
private JTextField codeBarText=new JTextField();//显示商品条码的文本区
private JTextField nameText=new JTextField();//显示商品名称的文本区
private JTextField priceText=new JTextField();//显示商品价格的文本区
private JTextField manufacturerText=new JTextField();//显示商品厂家的文本区
private JTextField totalPriceText=new JTextField("0.0");//显示购买总费用的文本区
private JTextField encouragePriceText=new JTextField("0.0");//显示奖励金额的文本区
private JTextField shoulePayText=new JTextField("0.0");//显示购买总费用的文本区
private JTextField PayText=new JTextField("0.0");//显示客户支付现金的文本区
private JTextField chargeText=new JTextField("0.0");//显示找零的文本区
private JButton addButton=new JButton("添加");
private JButton clear1Button=new JButton("清空1");
private JButton clear2Button=new JButton("清空2");
private JButton productButton=new JButton("商品详细说明");
//private JButton totalPriceButton=new JButton("计算商品总额");
private JButton totalPriceButton=new JButton("总金额计算");
private JButton encouragePriceButton=new JButton("奖励金额计算");
private JButton shouldPayButton=new JButton("应付金额计算");
private JButton computeButton=new JButton("找零金额计算");
//add(new SaleSystemPanel1,BorderLayout.NORTH);
public SaleSystemPanel()//将各面板加入总面板中
{
add(new SaleSystemPanel1(),BorderLayout.NORTH);
add(new SaleSystemCentered(),BorderLayout.CENTER);
add(new SaleSystemPanel4(),BorderLayout.SOUTH);
}
//SaleSystemPanel1 panel1=new SaleSystemPanel1();
//add(panel1,BorderLayou.NORTH);
private class SaleSystemPanel1 extends JPanel{ //私有类SaleSystemPanel1,此面板中添加显示商品属性的组件
public SaleSystemPanel1(){
setLayout(new GridLayout(3,4));
add(codeBarLabel);
add(codeBarText);
add(nameLabel);
add(nameText);
add(priceLabel);
add(priceText);
add(manufacturerLabel);
add(manufacturerText);
add(addButton);
add(clear1Button);
add(productButton);
clear1Button.addActionListener(new ActionListener()//点击"清空"时,所有文本区的内容为空
{
public void actionPerformed(ActionEvent e)
{
codeBarText.setText("");
nameText.setText("");
priceText.setText("");
manufacturerText.setText("");
//totalPriceText.setText("");
//PayText.setText("");
//chargeText.setText("");
}
});
/*addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
});*/
//for( i=0;i<products.length;i++) //throws ArrayIndexOutOfBoundsException
//{
//while(i<products.length){
addButton.addActionListener(new ActionListener()//点击”添加“按钮时,添加商品项目(商品数可以无限添加)
{
//int i=products.length;
public void actionPerformed(ActionEvent e)
{
//String buttonStr=(String)e.getSource();
//addButton.setLabel("添加下一个?");
String codeBar=codeBarText.getText();//获得商品的条码
String name=nameText.getText();//获得商品的名称
double price=Double.parseDouble(priceText.getText());//获得商品的价格
String manufacturer=manufacturerText.getText();//获得商品厂家
Product aProduct=new Product();
//products[products.length].setProduct(name, codeBar, price, manufacturer);
products.add(aProduct);
aProduct.setProduct(name, codeBar, price, manufacturer);//设置商品对象
//i++;
}
//i=i+1;
});
//}
productButton.addActionListener(new ActionListener()//试用方法:先输入了条码再按按钮……
{ //给”商品详细说明“按钮添加事件监听器,按该按钮时,根据所输入的条码,对应的文本区中将显示商品属性
public void actionPerformed(ActionEvent e)
{
//int i=products.size();
String cBar=codeBarText.getText();
for(int i=0;i<products.size();i++)
{
Product p=products.get(i);
if(p.getProductCodeBar().equals(cBar))
{
nameText.setText(p.getProductName());
String pr=Double.toString(p.getProductPrice());
priceText.setText(pr);
manufacturerText.setText(p.getProductManufacturer());
break;
}
/*if(products[i].getProductCodeBar().equals(cBar))
{
nameText.setText(p.getProductName());
String pr=Double.toString(p.getProductPrice());
priceText.setText(pr);
manufacturerText.setText(p.getProductManufacturer());
break;
}*/
}
}
});
}
//private int i=0;
}
private class SaleSystemCentered extends JPanel{//私有类SaleSystemCentered,此类中有一面板,其中包含两个面板
public SaleSystemCentered()
{
add(new SaleSystemPanel2(),BorderLayout.WEST);
add(new SaleSystemPanel3(),BorderLayout.EAST);
}
private class SaleSystemPanel2 extends JPanel{//私有类SaleSystemPanel2,此类中有一面板,其中添加显示付款情况的组件
public SaleSystemPanel2()
{
setLayout(new GridLayout(5,2));
//add(totalPriceLabel);
//add(totalPriceText);
add(totalPriceLabel);
add(totalPriceText);
add(encourageLabel);
add(encouragePriceText);
add(shouldPayLabel);
add(shoulePayText);
add(payLabel);
add(PayText);
add(chargeLabel);
add(chargeText);
}
}
private class SaleSystemPanel3 extends JPanel{//私有类SaleSystemPanel3,此类中有一面板,其中添加一个”金额计算"的按钮,点击该按钮时,
//double totalPrice=0.0; //“找零”文本区中将显示所应给客户找还的金额
public SaleSystemPanel3()
{
setLayout(new GridLayout(5,1));
add(totalPriceButton);
add(encouragePriceButton);
add(shouldPayButton);
add(computeButton);
add(clear2Button);
totalPriceButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//double totalPrice=0.0;
double price=Double.parseDouble(priceText.getText());
totalPrice+=price;
//totalPriceText.setText(Double.toString(price));
//double tPrice=Double.parseDouble(totalPriceText.getText());
//totalPrice=tPrice;
//totalPrice+=tPrice;
//doublerealTotle
totalPriceText.setText(""+totalPrice);
}
});
encouragePriceButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String ePrice=totalPriceText.getText();
double enPrice=Double.parseDouble(ePrice);
double encouragePrice=enPrice*0.10;
String price=Double.toString(encouragePrice);
encouragePriceText.setText(price);
}
});
shouldPayButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
double tPrice=Double.parseDouble(totalPriceText.getText());
double ePrice=Double.parseDouble(encouragePriceText.getText());
shoulePayText.setText(Double.toString(tPrice-ePrice));
}
});
computeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//double tPrice=Double.parseDouble(totalPriceText.getText());
//double ePrice=Double.parseDouble(encouragePriceText.getText());
//shoulePayText.setText(Double.toString(tPrice-ePrice));
double pay=Double.parseDouble(PayText.getText());
//double price=Double.parseDouble(priceText.getText());
double charge=pay-(Double.parseDouble(shoulePayText.getText()));
String ch=Double.toString(charge);
//ch.toString(charge);
chargeText.setText(ch);
//double singlePrice=Double.parseDouble(priceText.getText());
}
});
clear2Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
totalPriceText.setText("0.0");
encouragePriceText.setText("0.0");
shoulePayText.setText("0.0");
PayText.setText("0.0");
chargeText.setText("0.0");
totalPrice=0.0;
}
});
}
}
}
private class SaleSystemPanel4 extends JPanel{ //暂未实现
public SaleSystemPanel4()
{
this.setBackground(Color.yellow);
JLabel welcomLabel=new JLabel("欢迎使用本系统!");
add(welcomLabel);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -