public class ProductFrame extends JFrame{
//第一参数为表格加入的容器,第二参数为表头
//第三个参数为表头对应的实体类中的属性名
private LovoTable table = new LovoTable(this,
new String[]{"编号","商品名","厂家","单价"},
new String[]{"id","name","factory","price"},"" );
//商品集合
private FilePress fPr = new FilePress();
public static List<ProductBean> list = new ArrayList<ProductBean>();
static{
FilePress fp = new FilePress();
list = fp.readFile();
}
private LovoTxt findTxt = new LovoTxt("商品名", 50, 300, this);
public ProductFrame(){
this.setLayout(null);
//设置表格的大小和位置
table.setSizeAndLocation(30, 30, 400, 200);
//更新表格数据
table.updateLovoTable(list);
//添加按钮
LovoButton addButton = new LovoButton("添加", 50,250, this);
LovoButton delButton = new LovoButton("删除", 150,250, this);
LovoButton updateButton = new LovoButton("修改", 250,250, this);
addButton.addActionListener(new ActionListener() {//添加
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub /ProductFrame.this 访问外部类对象
//卸载当前窗体
ProductFrame.this.dispose();
//生成一个新窗体
AddFrame addFrame = new AddFrame();
}
});
delButton.addActionListener(new ActionListener() {//删除
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int row=table.getSelectedRow();//选中行
if(row == -1){
JOptionPane.showMessageDialog(null, "请选中行");
return;
}
list.remove(row);//将list集合中下标为row的进行当前删除
fPr.writerFile(list);
table.updateLovoTable(list);//删除后重新获得新的集合
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {//修改
// TODO Auto-generated method stub
int row = table.getSelectedRow();//选行
if(row==-1){
JOptionPane.showMessageDialog(null, "请进行选行");
return;
}
//得到指定行
ProductBean pro = list.get(row);
ProductFrame.this.dispose();
UpdateFrame u = new UpdateFrame(pro);
}
});
LovoButton findButton = new LovoButton("查询", 300, 300, this);
findButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String str = findTxt.getText();//得到商品名的字符串
List<ProductBean> flist = new ArrayList<ProductBean>();//建个新的集合
for(ProductBean b:list){//遍历list集合,将所有的元素放入b中
if(b.getName().contains(str)){//找到商品中包含字符串的元素是否包含
flist.add(b);//将其放入新的集合
}
}
table.updateLovoTable(flist);//获得新的集合放入表格中
}
});
this.setSize(600, 400);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
BufferedReader br = null;
FileReader fr = null;
}
//第一参数为表格加入的容器,第二参数为表头
//第三个参数为表头对应的实体类中的属性名
private LovoTable table = new LovoTable(this,
new String[]{"编号","商品名","厂家","单价"},
new String[]{"id","name","factory","price"},"" );
//商品集合
private FilePress fPr = new FilePress();
public static List<ProductBean> list = new ArrayList<ProductBean>();
static{
FilePress fp = new FilePress();
list = fp.readFile();
}
private LovoTxt findTxt = new LovoTxt("商品名", 50, 300, this);
public ProductFrame(){
this.setLayout(null);
//设置表格的大小和位置
table.setSizeAndLocation(30, 30, 400, 200);
//更新表格数据
table.updateLovoTable(list);
//添加按钮
LovoButton addButton = new LovoButton("添加", 50,250, this);
LovoButton delButton = new LovoButton("删除", 150,250, this);
LovoButton updateButton = new LovoButton("修改", 250,250, this);
addButton.addActionListener(new ActionListener() {//添加
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub /ProductFrame.this 访问外部类对象
//卸载当前窗体
ProductFrame.this.dispose();
//生成一个新窗体
AddFrame addFrame = new AddFrame();
}
});
delButton.addActionListener(new ActionListener() {//删除
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
int row=table.getSelectedRow();//选中行
if(row == -1){
JOptionPane.showMessageDialog(null, "请选中行");
return;
}
list.remove(row);//将list集合中下标为row的进行当前删除
fPr.writerFile(list);
table.updateLovoTable(list);//删除后重新获得新的集合
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {//修改
// TODO Auto-generated method stub
int row = table.getSelectedRow();//选行
if(row==-1){
JOptionPane.showMessageDialog(null, "请进行选行");
return;
}
//得到指定行
ProductBean pro = list.get(row);
ProductFrame.this.dispose();
UpdateFrame u = new UpdateFrame(pro);
}
});
LovoButton findButton = new LovoButton("查询", 300, 300, this);
findButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String str = findTxt.getText();//得到商品名的字符串
List<ProductBean> flist = new ArrayList<ProductBean>();//建个新的集合
for(ProductBean b:list){//遍历list集合,将所有的元素放入b中
if(b.getName().contains(str)){//找到商品中包含字符串的元素是否包含
flist.add(b);//将其放入新的集合
}
}
table.updateLovoTable(flist);//获得新的集合放入表格中
}
});
this.setSize(600, 400);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
BufferedReader br = null;
FileReader fr = null;
}