/*******************************
* @author shael
* @data 22/12/2013
*******************************/
import java.io.File;
// awt
import java.awt.Container;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// swing
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
//jxl (external jar! excel files operation lib)
import jxl.Workbook;
import jxl.Sheet;
import jxl.Cell;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
// class PersonalHomepageFrame
public class PersonalHomepageFrame extends JFrame {
// public
// construct
public PersonalHomepageFrame(String getName) {
multiTag = false;
tags = "";
init();
this.setTitle("Welcome " + getName + "!");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null); // layout style: absolute location
this.setBounds(0, 0, 355, 265);
// set header's image
Image image = new ImageIcon("img/b.jpg").getImage();
this.setIconImage(image);
this.setResizable(false); // can't change size of window
this.setLocationRelativeTo(null); // display in center
this.setVisible(true);
}
/*********************************
* Specify: initial of frame
* Name: init
* Para: void
* return: void
*********************************/
public void init()
{
// create a container
Container con = this.getContentPane();
// set background image
jBackground = new JLabel();
Image background = new ImageIcon("img/a.jpg").getImage();
jBackground.setIcon(new ImageIcon(background));
jBackground.setBounds(0, 0, 355, 265);
// set TextField for username and password
productName = new JTextField();
productName.setBounds(100, 20, 150, 20);
productTag = new JTextField();
productTag.setBounds(100, 160, 210, 20);
newTag = new JTextField();
newTag.setBounds(250, 115, 80, 20);
// hint text
jl1 = new JLabel("商品名");
jl1.setBounds(40, 20, 70, 20);
jl2 = new JLabel("标签");
jl2.setBounds(40, 160, 70, 20);
jl3 = new JLabel("请选择商品标签");
jl3.setBounds(40, 70, 100, 20);
jl4 = new JLabel("自定义标签");
jl4.setBounds(160, 115, 100, 20);
// check box
jc1 = new JCheckBox("是否多选");
jc1.setBounds(160, 70, 80, 20);
// ComboBox
jcb = new JComboBox();
jcb.addItem("请选择");
Workbook excel;
Sheet sheet;
try{
excel = Workbook.getWorkbook(new File("database/product.xls")); // open the excel file
sheet = excel.getSheet(0); // open the first sheet
for(int i=0;i<sheet.getColumns();i++)
{
jcb.addItem(sheet.getCell(i, 0).getContents());
}
}catch (Exception e) {
System.out.println(e);
}
jcb.setBounds(40, 115, 90, 20);
// set button login
bu1 = new JButton("保存");
bu1.setBounds(240, 200, 100, 20);
bu2 = new JButton("搜索商品");
bu2.setBounds(100, 200, 100, 20);
bu3 = new JButton("添加标签");
bu3.setBounds(250, 70, 90, 20);
bu4 = new JButton("预览");
bu4.setBounds(30, 200, 60, 20);
// add event listener
bu1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String product = productName.getText();
storeProduct(product, tags);
}
});
bu2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String product = productName.getText();
search(product, tags);
}
});
bu3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
checkNewTag();
String tag = jcb.getSelectedItem().toString();
if(checkTag(tag) == 0)
{
if(multiTag)
{
tags = tags + tag + ";";
}else{
tags = tag + ";";
}
productTag.setText(tags);
}
}
});
bu4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String product = productName.getText();
generate(product, tags);
}
});
jc1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(multiTag == true)
{
multiTag = false;
}else{
multiTag = true;
}
}
});
// contain all elements
jBackground.add(jl1);
jBackground.add(jl2);
jBackground.add(jl3);
jBackground.add(jl4);
jBackground.add(bu1);
jBackground.add(bu2);
jBackground.add(bu3);
jBackground.add(bu4);
jBackground.add(jc1);
jBackground.add(jcb);
jBackground.add(productName);
jBackground.add(productTag);
jBackground.add(newTag);
con.add(jBackground);
}
/*********************************
* Specify: search the product in database
* Name: generate
* Para: String product, String tags
* return: void
*********************************/
protected void search(String product, String tags) {
if(tags.isEmpty())
{
JOptionPane.showMessageDialog(this, "别闹,请选择标签,再保存= =。\n");
return;
}
if(product.isEmpty())
{
JOptionPane.showMessageDialog(this, "别闹,请填好商品名,再保存= =。\n");
return;
}
String[] ss = tags.split(";");
String msg = "searching...\n";
Workbook excel;
Sheet sheet;
for(int i=0;i<ss.length;i++)
{
if(!ss[i].isEmpty())
{
try {
excel = Workbook.getWorkbook(new File("database/product.xls")); // open the excel file
sheet = excel.getSheet(0); // open the first sheet
for(int j=0;j<sheet.getColumns();j++)
{// find out if the tag is already in database
if(ss[i].equals(sheet.getCell(j, 0).getContents()))
{// enter each tag to find out if product's already in it
int row = checkSheet(sheet, product, j);
if(row == 0)
{
msg = msg + "productname: <" + product + "> is found in tag <" + ss[i] + ">\n";
}
}
}
excel.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
msg = msg + "search finish!\n";
JOptionPane.showMessageDialog(this, msg);
}
/*********************************
* Specify: generate info of the product
* Name: generate
* Para: String product, String tags
* return: void
*********************************/
protected void generate(String product, String tags) {
JOptionPane.showMessageDialog(this, "productname: " + product + "\ntags: " + tags);
}
/*********************************
* Specify: check the input tag
* Name: checkTag
* Para: String tag
* return: 0:success;1:false
*********************************/
public int checkTag(String tag)
{
tags = productTag.getText(); // in case of delete tag(s)!!!!!!
if(tag.equals("请选择"))
{
JOptionPane.showMessageDialog(this, "别闹,请选择标签= =。\n");
return 1;
}
if(multiTag)
{
String[] ss = tags.split(";");
for(int i=0;i<ss.length;i++)
{
if(ss[i].equals(tag))
{
JOptionPane.showMessageDialog(this, "别闹,标签重复= =。\n");
return 1;
}
}
}
return 0;
}
/*********************************
* Specify: check NewTag's text
* Name: checkNewTag
* Para: void
* return: 0:add nwe tag in jcb;1:old tag
*********************************/
public int checkNewTag()
{
String newtag = newTag.getText();
if(!newtag.isEmpty())
{
for(int i=0;i<jcb.getItemCount();i++)
{
if(newtag.equals(jcb.getItemAt(i)))
{// old tag
return 1;
}
}
// add nwe tag in jcb
jcb.addItem(newtag);
jcb.setSelectedItem(newtag);
return 0;
}
return 1;
}
/*********************************
* Specify: ch
没有合适的资源?快使用搜索试试~ 我知道了~
java课程作业--包含用户注册、登录和商品管理
共24个文件
class:12个
java:4个
jpg:2个
5星 · 超过95%的资源 需积分: 10 16 下载量 109 浏览量
2013-12-23
16:26:59
上传
评论
收藏 769KB ZIP 举报
温馨提示
一个java写的小程序,包含额功能有:用户注册、登录和商品管理。 每个商品可以选择任意多的标签,程序根据标签讲商品数据存储。 用户信息和商品数据存储在excel文件里面并进行管理。
资源推荐
资源详情
资源评论
收起资源包目录
ProductDataBase.zip (24个子文件)
ProductDataBase
bin
RegisterFrame$1.class 655B
RegisterFrame.class 5KB
LogInFrame$1.class 1KB
LogInFrame$2.class 710B
PersonalHomepageFrame$1.class 1KB
PersonalHomepageFrame$3.class 2KB
Main.class 482B
PersonalHomepageFrame.class 9KB
PersonalHomepageFrame$4.class 1021B
PersonalHomepageFrame$2.class 1019B
LogInFrame.class 4KB
PersonalHomepageFrame$5.class 844B
img
a.jpg 76KB
b.jpg 51KB
database
product.xls 15KB
user.xls 15KB
.settings
org.eclipse.jdt.core.prefs 598B
src
PersonalHomepageFrame.java 12KB
RegisterFrame.java 5KB
LogInFrame.java 5KB
Main.java 479B
.project 391B
.classpath 380B
lib
jxl.jar 688KB
共 24 条
- 1
资源评论
- qq_289295912015-06-11很好,值得一看
ShaelLancelot
- 粉丝: 19
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Flume进阶-自定义拦截器jar包
- Dubins曲线算法讲解和在运动规划中的使用.pdf
- 上市公司-股票性质数据-工具变量(民企、国企、央企)2003-2022年.dta
- 上市公司-股票性质数据-工具变量(民企、国企、央企)2003-2022年.xlsx
- Reeds+Shepp曲线算法讲解和实现.pdf
- 毕业设计基于SpringBoot+MyBatisPlus+MySQL+Vue的外卖配送信息系统源代码+数据库
- 词向量(Word Embeddings)是自然语言处理(NLP)领域的一种重要技术.txt
- Surfer,线性函数
- MyBatis 的动态 SQL 是其核心特性之一.txt
- 时代的sdddsddsddsd
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功