“Java任务:商品信息检索之管理端”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
(创建页面,内容为“==任务描述== 任务描述:实现商品检索界面 ===客户端界面=== 客户端实现商品的查询、购买,和查改购买情况 文件:java2020-1…”)
 
第22行: 第22行:
  
 
在src下,或者对应的包下,右键new-other
 
在src下,或者对应的包下,右键new-other
 +
 
[[文件:java9-32.png]]
 
[[文件:java9-32.png]]
  
第125行: 第126行:
 
列名:商品编号,列宽:70</nowiki>
 
列名:商品编号,列宽:70</nowiki>
  
===面板4:购买区域的创建===
+
===面板4:管理区域的创建===
按照下图,完成商品购买区域的构建:
+
按照下图,完成商品管理区域的构建:
  
[[文件:java2020-12-32.png]]
+
[[文件:java2020-12-33.png]]
  
*"购物车商品数": 标签 lblTotal
+
*"商品上架": btnAdd
  
*"0件":标签 lblQuantity
+
*"商品下架":btnDel
  
*"查看详情" : 标签 lblCart
+
*"修改信息" : btnModify
  
*"购买": 标签 btnBuy
+
*"盘点": 标签 btnSum
  
 
==事件响应==
 
==事件响应==
第179行: 第180行:
 
</nowiki>
 
</nowiki>
  
===为"购买”按钮添加响应事件===
+
===为"商品上架”按钮添加响应事件===
 
  <nowiki>
 
  <nowiki>
btnBuy.addActionListener(new ActionListener() {
+
btnAdd.addActionListener(new ActionListener() {
 
public void actionPerformed(ActionEvent e) {
 
public void actionPerformed(ActionEvent e) {
 
//获取用户在商品表格中选择的行
 
//获取用户在商品表格中选择的行
 
int selectRow = table.getSelectedRow();
 
int selectRow = table.getSelectedRow();
 
int gid;
 
int gid;
+
 +
//询问商品名称、价格、数量
 +
String name=JOptionPane.showInputDialog("上架商品的名称是:");
 +
float price = Float.valueOf(JOptionPane.showInputDialog("上架商品的价格是:"));
 +
int quantity=Integer.parseInt(JOptionPane.showInputDialog("上架商品的数量是:"));
 +
 +
JOptionPane.showMessageDialog(null, "商品信息:"+name+","+price+"元,"+quantity, "信息", JOptionPane.INFORMATION_MESSAGE);
 +
 +
}
 +
});
 +
</nowiki>
 +
 
 +
===为"商品下架”按钮添加响应事件===
 +
<nowiki>
 +
btnDel.addActionListener(new ActionListener() {
 +
public void actionPerformed(ActionEvent e) {
 +
//获取用户在商品表格中选择的行
 +
int selectRow = table.getSelectedRow();
 +
int gid;
 +
 
 +
//当用户没有选择商品时
 +
if(selectRow<0) {
 +
JOptionPane.showMessageDialog(null, "请选择你所需要操作的商品","错误信息",JOptionPane.WARNING_MESSAGE);
 +
return;
 +
}else {
 +
String id =(String)table.getModel().getValueAt(selectRow, 0);
 +
gid=Integer.valueOf(id);
 +
String message = "商品"+gid+"下架成功!";
 +
JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);
 +
 +
}
 +
}
 +
});
 +
</nowiki>
 +
===为"修改信息”按钮添加响应事件===
 +
<nowiki>
 +
btnModify.addActionListener(new ActionListener() {
 +
public void actionPerformed(ActionEvent e) {
 +
//获取用户在商品表格中选择的行
 +
int selectRow = table.getSelectedRow();
 +
int gid;
 +
 
 
//当用户没有选择商品时
 
//当用户没有选择商品时
 
if(selectRow<0) {
 
if(selectRow<0) {
JOptionPane.showMessageDialog(null, "请选择你所需要购买的商品","错误信息",JOptionPane.WARNING_MESSAGE);
+
JOptionPane.showMessageDialog(null, "请选择你所需要操作的商品","错误信息",JOptionPane.WARNING_MESSAGE);
 
return;
 
return;
 
}else {
 
}else {
 
String id =(String)table.getModel().getValueAt(selectRow, 0);
 
String id =(String)table.getModel().getValueAt(selectRow, 0);
 
gid=Integer.valueOf(id);
 
gid=Integer.valueOf(id);
JOptionPane.showMessageDialog(null, "您选择的是"+gid+"号商品", "信息", JOptionPane.INFORMATION_MESSAGE);
+
//询问顾客购买商品数量
 +
float price=Integer.parseInt(JOptionPane.showInputDialog("该商品价格修改为:"));
 +
String message = "商品"+gid+"价格修改后的新价格为:"+price;
 +
JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);
 +
 
 
}
 
}
 
//询问顾客购买商品数量
 
int quantity=Integer.parseInt(JOptionPane.showInputDialog("你需要购买数量是:"));
 
JOptionPane.showMessageDialog(null, "您选择的是"+gid+"号商品,购买数量为"+quantity, "信息", JOptionPane.INFORMATION_MESSAGE);
 
 
 
}
 
}
});</nowiki>
+
});
 +
</nowiki>
 +
===为"盘点”按钮添加响应事件===
 +
<nowiki>
 +
btnSum.addActionListener(new ActionListener() {
 +
public void actionPerformed(ActionEvent e) {
 +
 +
String message = "本店共有商品10个,合计价值100 元";
 +
JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);
 +
}
 +
});
 +
</nowiki>
 +
 
 
===退出系统的响应事件===
 
===退出系统的响应事件===
  
第215行: 第268行:
 
});</nowiki>
 
});</nowiki>
  
===其他按钮的响应事件===
+
 
由于还未连接后台,其他按钮如查看详情按钮,可暂不编写响应事件。
 
  
  
第254行: 第306行:
 
import java.awt.event.MouseEvent;
 
import java.awt.event.MouseEvent;
 
import java.awt.Color;
 
import java.awt.Color;
import javax.swing.UIManager;
 
import java.awt.Font;
 
  
 
public class GoodsShow extends JFrame {
 
public class GoodsShow extends JFrame {
第286行: 第336行:
 
public GoodsShow() {
 
public GoodsShow() {
 
          
 
          
setTitle("\u5546\u9662\u9762\u9986\u5BA2\u6237\u7AEF");
+
setTitle("\u5546\u9662\u9762\u9986\u7BA1\u7406\u7CFB\u7EDF");
 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
setBounds(100, 100, 523, 382);
 
setBounds(100, 100, 523, 382);
第300行: 第350行:
 
 
 
JLabel lblUser = new JLabel("\u6B22\u8FCE\u4F60\uFF0C");
 
JLabel lblUser = new JLabel("\u6B22\u8FCE\u4F60\uFF0C");
lblUser.setFont(new Font("宋体", Font.PLAIN, 12));
+
lblUser.setHorizontalAlignment(SwingConstants.CENTER);
lblUser.setHorizontalAlignment(SwingConstants.LEFT);
+
lblUser.setBounds(10, 10, 87, 22);
lblUser.setBounds(21, 11, 87, 22);
 
 
panel1.add(lblUser);
 
panel1.add(lblUser);
JLabel lbllogout = new JLabel("\u9000\u51FA\u7CFB\u7EDF");
+
JLabel lblNewLabel = new JLabel("\u9000\u51FA\u7CFB\u7EDF");
lbllogout.setForeground(Color.BLUE);
+
lblNewLabel.setForeground(Color.BLUE);
lbllogout.addMouseListener(new MouseAdapter() {
+
lblNewLabel.addMouseListener(new MouseAdapter() {
 
@Override
 
@Override
 
public void mouseClicked(MouseEvent e) {
 
public void mouseClicked(MouseEvent e) {
第314行: 第363行:
 
}
 
}
 
});
 
});
lbllogout.setBounds(403, 15, 54, 15);
+
lblNewLabel.setBounds(362, 10, 61, 19);
panel1.add(lbllogout);
+
panel1.add(lblNewLabel);
 
 
 
 
 
JPanel panel2 = new JPanel();
 
JPanel panel2 = new JPanel();
第324行: 第371行:
 
panel2.setLayout(null);
 
panel2.setLayout(null);
 
 
JLabel lblGoodsName = new JLabel("\u5546\u54C1\u540D");
+
JLabel lblNewLabel_2 = new JLabel("\u5546\u54C1\u540D");
lblGoodsName.setBounds(10, 10, 54, 15);
+
lblNewLabel_2.setBounds(10, 10, 54, 15);
panel2.add(lblGoodsName);
+
panel2.add(lblNewLabel_2);
 
 
 
GoodsNameField = new JTextField();
 
GoodsNameField = new JTextField();
第333行: 第380行:
 
GoodsNameField.setColumns(10);
 
GoodsNameField.setColumns(10);
 
 
JLabel lblType = new JLabel("\u5206\u7C7B\uFF1A");
+
JLabel lblNewLabel_3 = new JLabel("\u5206\u7C7B\uFF1A");
lblType.setBounds(181, 10, 54, 15);
+
lblNewLabel_3.setBounds(181, 10, 54, 15);
panel2.add(lblType);
+
panel2.add(lblNewLabel_3);
 
 
 
JComboBox TypeBox = new JComboBox();
 
JComboBox TypeBox = new JComboBox();
第362行: 第409行:
 
btnQuery.setBounds(363, 6, 93, 23);
 
btnQuery.setBounds(363, 6, 93, 23);
 
panel2.add(btnQuery);
 
panel2.add(btnQuery);
 +
 
 
 
JScrollPane scrollPane = new JScrollPane();
 
JScrollPane scrollPane = new JScrollPane();
第396行: 第444行:
 
scrollPane.setViewportView(table);
 
scrollPane.setViewportView(table);
 
 
JPanel panel4 = new JPanel();
 
panel4.setBounds(10, 299, 487, 34);
 
contentPane.add(panel4);
 
panel4.setLayout(null);
 
 
 
JLabel lblTotal = new JLabel("\u8D2D\u7269\u8F66\u5546\u54C1\u6570");
+
JPanel panel = new JPanel();
lblTotal.setBounds(10, 10, 78, 15);
+
panel.setBounds(10, 294, 487, 39);
panel4.add(lblTotal);
+
contentPane.add(panel);
 +
panel.setLayout(null);
 
 
JLabel lblQuantity = new JLabel("0 \u4EF6");
+
JButton btnAdd = new JButton("\u5546\u54C1\u4E0A\u67B6");
lblQuantity.setBounds(98, 10, 77, 15);
+
btnAdd.addActionListener(new ActionListener() {
    panel4.add(lblQuantity);
+
public void actionPerformed(ActionEvent e) {
 +
//获取用户在商品表格中选择的行
 +
int selectRow = table.getSelectedRow();
 +
int gid;
 +
 +
//询问商品名称、价格、数量
 +
String name=JOptionPane.showInputDialog("上架商品的名称是:");
 +
float price = Float.valueOf(JOptionPane.showInputDialog("上架商品的价格是:"));
 +
int quantity=Integer.parseInt(JOptionPane.showInputDialog("上架商品的数量是:"));
 +
 +
JOptionPane.showMessageDialog(null, "商品信息:"+name+","+price+"元,"+quantity, "信息", JOptionPane.INFORMATION_MESSAGE);
 +
 +
}
 +
});
 +
btnAdd.setBounds(10, 10, 93, 23);
 +
panel.add(btnAdd);
 
 
 +
JButton btnDel = new JButton("\u5546\u54C1\u4E0B\u67B6");
 +
btnDel.addActionListener(new ActionListener() {
 +
public void actionPerformed(ActionEvent e) {
 +
//获取用户在商品表格中选择的行
 +
int selectRow = table.getSelectedRow();
 +
int gid;
 +
 +
//当用户没有选择商品时
 +
if(selectRow<0) {
 +
JOptionPane.showMessageDialog(null, "请选择你所需要操作的商品","错误信息",JOptionPane.WARNING_MESSAGE);
 +
return;
 +
}else {
 +
String id =(String)table.getModel().getValueAt(selectRow, 0);
 +
gid=Integer.valueOf(id);
 +
String message = "商品"+gid+"下架成功!";
 +
JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);
 +
 +
}
 +
}
 +
});
 +
btnDel.setBounds(126, 10, 93, 23);
 +
panel.add(btnDel);
 
 
JLabel lblCart = new JLabel("\u67E5\u770B\u8BE6\u60C5");
+
JButton btnModify = new JButton("\u4FEE\u6539\u4FE1\u606F");
 +
btnModify.setBounds(251, 10, 93, 23);
 +
panel.add(btnModify);
 
 
lblCart.setForeground(Color.BLUE);
+
btnModify.addActionListener(new ActionListener() {
lblCart.setBounds(185, 10, 54, 15);
 
panel4.add(lblCart);
 
 
JButton btnBuy = new JButton("\u8D2D\u4E70");
 
btnBuy.addActionListener(new ActionListener() {
 
 
public void actionPerformed(ActionEvent e) {
 
public void actionPerformed(ActionEvent e) {
 
//获取用户在商品表格中选择的行
 
//获取用户在商品表格中选择的行
 
int selectRow = table.getSelectedRow();
 
int selectRow = table.getSelectedRow();
 
int gid;
 
int gid;
+
 
 
//当用户没有选择商品时
 
//当用户没有选择商品时
 
if(selectRow<0) {
 
if(selectRow<0) {
JOptionPane.showMessageDialog(null, "请选择你所需要购买的商品","错误信息",JOptionPane.WARNING_MESSAGE);
+
JOptionPane.showMessageDialog(null, "请选择你所需要操作的商品","错误信息",JOptionPane.WARNING_MESSAGE);
 
return;
 
return;
 
}else {
 
}else {
 
String id =(String)table.getModel().getValueAt(selectRow, 0);
 
String id =(String)table.getModel().getValueAt(selectRow, 0);
 
gid=Integer.valueOf(id);
 
gid=Integer.valueOf(id);
JOptionPane.showMessageDialog(null, "您选择的是"+gid+"号商品", "信息", JOptionPane.INFORMATION_MESSAGE);
+
//询问顾客购买商品数量
 +
float price=Integer.parseInt(JOptionPane.showInputDialog("该商品价格修改为:"));
 +
String message = "商品"+gid+"价格修改后的新价格为:"+price;
 +
JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);
 +
 
 
}
 
}
 
//询问顾客购买商品数量
 
int quantity=Integer.parseInt(JOptionPane.showInputDialog("你需要购买数量是:"));
 
JOptionPane.showMessageDialog(null, "您选择的是"+gid+"号商品,购买数量为"+quantity, "信息", JOptionPane.INFORMATION_MESSAGE);
 
 
}
 
}
 
});
 
});
btnBuy.setBounds(293, 6, 93, 23);
+
panel4.add(btnBuy);
+
JButton btnSum = new JButton("\u76D8\u70B9");
 +
btnSum.addActionListener(new ActionListener() {
 +
public void actionPerformed(ActionEvent e) {
 +
 +
String message = "本店共有商品10个,合计价值100 元";
 +
JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);
 +
}
 +
});
 +
btnSum.setBounds(370, 10, 93, 23);
 +
panel.add(btnSum);
  
 
}
 
}
 
+
 +
 
private void showGoods(ArrayList<Goods> goods){
 
private void showGoods(ArrayList<Goods> goods){
 
     //在表格中显示数据
 
     //在表格中显示数据

2020年6月19日 (五) 11:14的版本

任务描述

任务描述:实现商品检索界面

客户端界面

客户端实现商品的查询、购买,和查改购买情况

Java2020-12-27.png

管理端

管理端实现商品的上架、下架、查询和修改功能

Java2020-12-28.png

任务实现

下面以客户端为例,讲解实现的步骤。

构建初始窗体

打开你的Java Project

在src下,或者对应的包下,右键new-other

Java9-32.png

选择window builder下的 swing下的JFrame或者Application Window

Java9-33.png

给生成的窗体起名字,如我的命名为GoodsShow.

进入视图界面

在类名上单击右键,选择 Open With WindowBuilder Editor,如图2所示

Java9-3.png


在类编写窗口的底部选择【Design】标签。

Java9-4.png

添加各个子面板

设置默认面板的布局方式:

Java9-27.png

这里推荐使用绝对布局(absolute layout),添加组件更方便、自由。

绝对布局,顾名思义,就是给每个元素指定上起始点和长、宽、高,使用它的物理长度值进行定位和布局

根据界面的需要,我们可以向默认面板中4个子面板

其中第1、2、4个面板为普通的JPanal ,注意设置其布局方式为绝对布局

第3个面板为JScrollPane

Java9-14.png

Java2020-12-29.png

面板1、2添加元素

按照下图,把对应控件拖入窗体到指定位置,完成商品检索区域的构建,并设置各个控件的【Variable】属性:

Java2020-12-30.png

各组件参考命名:

欢迎你 -> lblUser ; 退出系统 -> lbllogout 
商品名 -> lblGoodsName ;输入框 ->GoodsNameField  ;分类标签 -> lblType ;分类下拉框-> TypeBox ;查询 -> btnQuery

Java2020-12-31.png

分类下拉框的设置

  • 选中分类组合框,在左侧的Properties窗口中找到Model这栏,如图:

Java9-6.png


  • 点击上图的“…”添加如下图所示的三个分类:

每新添加一个记录,就插入一个新行就行

Java2020-12-26.png

面板3:表格的创建

按照下图,完成商品显示区域的构建:

Java9-8.png

图 1-8

  • 把JScrollPane拖到界面中,并拉伸成上图大小。
  • 把商品列表JTable嵌到JScrollPane:必须要把JTable控件拖入JScrollPane的Viewport区域中才能显示,拖入的区域如下图中的黄色区域所示:

Java9-9.png

图 7 JScollPane布局

  • 设置表头标题:

点击左侧控件列表中的JTable控件。

Java9-10.png

图 1-10

在属性列表中找到【model】,点击【…】打开表格编辑框:

Java9-11.png

图 8 表格编辑对话框

点击【Columns】区域的【Insert】插入一列, 在底部的列属性区域中设置【Title】为总价:,设置列宽【Pref.width】为:164

按照以上步骤,依次添加如下表头信息:

列名:数量,列宽:70
列名:价格,列宽:70
列名:商品名称,列宽:150
列名:商品编号,列宽:70

面板4:管理区域的创建

按照下图,完成商品管理区域的构建:

Java2020-12-33.png

  • "商品上架": btnAdd
  • "商品下架":btnDel
  • "修改信息" : btnModify
  • "盘点": 标签 btnSum

事件响应

为“查询”按钮添加响应事件

双击图形界面中“查询”按钮,即可进入其响应事件的编写:

		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String goodsname = GoodsNameField.getText();
				ArrayList<Goods> goods=new ArrayList<>();
				String type =(String)TypeBox.getSelectedItem();
				JOptionPane.showMessageDialog(null, "您选择的是"+type, "信息", JOptionPane.INFORMATION_MESSAGE);

				//生成几个假数据 来模拟演示一下,真实软件这里应该调用业务逻辑层,
				Goods mygoods = new Goods("01",goodsname,15.0f,2);
				Goods mygoods2 = new Goods("02","山西刀削面",10.0f,2);
				//
				goods.add(mygoods);goods.add(mygoods2);
				showGoods(goods);
				JOptionPane.showMessageDialog(null, "'"+type+"'查询成功!", "信息", JOptionPane.INFORMATION_MESSAGE);
			}
		});

显示数据的函数showGoods():


	private void showGoods(ArrayList<Goods> goods){
    	//在表格中显示数据
 		DefaultTableModel dt=(DefaultTableModel) table.getModel();
 		dt.setRowCount(0);
 		
 		for(int i=0;i<goods.size();i++) {
			Goods g=goods.get(i);
			dt.insertRow(i, new Object[] {
					g.getId(),g.getName(),g.getPrice(),g.getNum(),g.getPrice()*g.getNum()
					
			});
		}
    }

为"商品上架”按钮添加响应事件

btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取用户在商品表格中选择的行
				int selectRow = table.getSelectedRow();
				int gid;
							
				//询问商品名称、价格、数量
				String name=JOptionPane.showInputDialog("上架商品的名称是:");
				float price = Float.valueOf(JOptionPane.showInputDialog("上架商品的价格是:"));
				int quantity=Integer.parseInt(JOptionPane.showInputDialog("上架商品的数量是:"));
				
				JOptionPane.showMessageDialog(null, "商品信息:"+name+","+price+"元,"+quantity, "信息", JOptionPane.INFORMATION_MESSAGE);
			
			}
		});

为"商品下架”按钮添加响应事件

btnDel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取用户在商品表格中选择的行
				int selectRow = table.getSelectedRow();
				int gid;

				//当用户没有选择商品时
				if(selectRow<0) {
					JOptionPane.showMessageDialog(null, "请选择你所需要操作的商品","错误信息",JOptionPane.WARNING_MESSAGE);
					return;
				}else {
					String id =(String)table.getModel().getValueAt(selectRow, 0);
					gid=Integer.valueOf(id);
					String message = "商品"+gid+"下架成功!";
					JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);
					
				}
			}
		});

为"修改信息”按钮添加响应事件

btnModify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取用户在商品表格中选择的行
				int selectRow = table.getSelectedRow();
				int gid;

				//当用户没有选择商品时
				if(selectRow<0) {
					JOptionPane.showMessageDialog(null, "请选择你所需要操作的商品","错误信息",JOptionPane.WARNING_MESSAGE);
					return;
				}else {
					String id =(String)table.getModel().getValueAt(selectRow, 0);
					gid=Integer.valueOf(id);
					//询问顾客购买商品数量
					float price=Integer.parseInt(JOptionPane.showInputDialog("该商品价格修改为:"));
					String message = "商品"+gid+"价格修改后的新价格为:"+price;
					JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);

				}
			}
		});

为"盘点”按钮添加响应事件

btnSum.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String message = "本店共有商品10个,合计价值100 元";
				JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);
			}
		});

退出系统的响应事件

lbllogout.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				GoodsShow.this.dispose();
				
			}
		});



程序代码

以下代码绝大多数为机器自动生成,

在这里显示代码的意图是让大家能够参照比对。

package main;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import dao.MyCon;
import service.*;
import entity.*;

import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;

public class GoodsShow extends JFrame {

	private JPanel contentPane;
	private JTextField GoodsNameField;
	private JTable table;
	GoodsService gs;
	CartsService cs;
	User user;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GoodsShow frame = new GoodsShow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GoodsShow() {
         
		setTitle("\u5546\u9662\u9762\u9986\u7BA1\u7406\u7CFB\u7EDF");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 523, 382);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel1 = new JPanel();
		panel1.setBounds(10, 10, 487, 42);
		contentPane.add(panel1);
		panel1.setLayout(null);
		
		JLabel lblUser = new JLabel("\u6B22\u8FCE\u4F60\uFF0C");
		lblUser.setHorizontalAlignment(SwingConstants.CENTER);
		lblUser.setBounds(10, 10, 87, 22);
		panel1.add(lblUser);
		JLabel lblNewLabel = new JLabel("\u9000\u51FA\u7CFB\u7EDF");
		lblNewLabel.setForeground(Color.BLUE);
		lblNewLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				GoodsShow.this.dispose();
				
			}
		});
		lblNewLabel.setBounds(362, 10, 61, 19);
		panel1.add(lblNewLabel);
		
		JPanel panel2 = new JPanel();
		panel2.setBounds(10, 59, 487, 42);
		contentPane.add(panel2);
		panel2.setLayout(null);
		
		JLabel lblNewLabel_2 = new JLabel("\u5546\u54C1\u540D");
		lblNewLabel_2.setBounds(10, 10, 54, 15);
		panel2.add(lblNewLabel_2);
		
		GoodsNameField = new JTextField();
		GoodsNameField.setBounds(60, 7, 110, 21);
		panel2.add(GoodsNameField);
		GoodsNameField.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("\u5206\u7C7B\uFF1A");
		lblNewLabel_3.setBounds(181, 10, 54, 15);
		panel2.add(lblNewLabel_3);
		
		JComboBox TypeBox = new JComboBox();
		TypeBox.setModel(new DefaultComboBoxModel(new String[] {"\u67E5\u8BE2\u6240\u6709", "\u6309\u540D\u79F0\u67E5\u8BE2", "\u6309ID\u67E5\u8BE2"}));
		TypeBox.setBounds(225, 7, 102, 21);
		panel2.add(TypeBox);
		
		JButton btnQuery = new JButton("\u67E5\u8BE2");
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String goodsname = GoodsNameField.getText();
				ArrayList<Goods> goods=new ArrayList<>();
				String type =(String)TypeBox.getSelectedItem();
				JOptionPane.showMessageDialog(null, "您选择的是"+type, "信息", JOptionPane.INFORMATION_MESSAGE);

				//生成几个假数据 来模拟演示一下,真实软件这里应该调用业务逻辑层,
				Goods mygoods = new Goods("01",goodsname,15.0f,2);
				Goods mygoods2 = new Goods("02","山西刀削面",10.0f,2);
				//
				goods.add(mygoods);goods.add(mygoods2);
				showGoods(goods);
				JOptionPane.showMessageDialog(null, "'"+type+"'查询成功!", "信息", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		
		btnQuery.setBounds(363, 6, 93, 23);
		panel2.add(btnQuery);
		
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 111, 487, 178);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{new Integer(1), "\u5170\u5DDE\u725B\u8089\u9762", new Float(15.0f), null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
			},
			new String[] {
				"id", "\u5546\u54C1\u540D\u79F0", "\u4EF7\u683C", "\u6570\u91CF", "\u603B\u4EF7"
			}
		) {
			Class[] columnTypes = new Class[] {
				Integer.class, Object.class, Float.class, Integer.class, Object.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		scrollPane.setViewportView(table);
		
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 294, 487, 39);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton btnAdd = new JButton("\u5546\u54C1\u4E0A\u67B6");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取用户在商品表格中选择的行
				int selectRow = table.getSelectedRow();
				int gid;
							
				//询问商品名称、价格、数量
				String name=JOptionPane.showInputDialog("上架商品的名称是:");
				float price = Float.valueOf(JOptionPane.showInputDialog("上架商品的价格是:"));
				int quantity=Integer.parseInt(JOptionPane.showInputDialog("上架商品的数量是:"));
				
				JOptionPane.showMessageDialog(null, "商品信息:"+name+","+price+"元,"+quantity, "信息", JOptionPane.INFORMATION_MESSAGE);
			
			}
		});
		btnAdd.setBounds(10, 10, 93, 23);
		panel.add(btnAdd);
		
		JButton btnDel = new JButton("\u5546\u54C1\u4E0B\u67B6");
		btnDel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取用户在商品表格中选择的行
				int selectRow = table.getSelectedRow();
				int gid;

				//当用户没有选择商品时
				if(selectRow<0) {
					JOptionPane.showMessageDialog(null, "请选择你所需要操作的商品","错误信息",JOptionPane.WARNING_MESSAGE);
					return;
				}else {
					String id =(String)table.getModel().getValueAt(selectRow, 0);
					gid=Integer.valueOf(id);
					String message = "商品"+gid+"下架成功!";
					JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);
					
				}
			}
		});
		btnDel.setBounds(126, 10, 93, 23);
		panel.add(btnDel);
		
		JButton btnModify = new JButton("\u4FEE\u6539\u4FE1\u606F");
		btnModify.setBounds(251, 10, 93, 23);
		panel.add(btnModify);
		
		btnModify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取用户在商品表格中选择的行
				int selectRow = table.getSelectedRow();
				int gid;

				//当用户没有选择商品时
				if(selectRow<0) {
					JOptionPane.showMessageDialog(null, "请选择你所需要操作的商品","错误信息",JOptionPane.WARNING_MESSAGE);
					return;
				}else {
					String id =(String)table.getModel().getValueAt(selectRow, 0);
					gid=Integer.valueOf(id);
					//询问顾客购买商品数量
					float price=Integer.parseInt(JOptionPane.showInputDialog("该商品价格修改为:"));
					String message = "商品"+gid+"价格修改后的新价格为:"+price;
					JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);

				}
			}
		});
		
		JButton btnSum = new JButton("\u76D8\u70B9");
		btnSum.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String message = "本店共有商品10个,合计价值100 元";
				JOptionPane.showMessageDialog(null, message, "信息", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		btnSum.setBounds(370, 10, 93, 23);
		panel.add(btnSum);

	}
	
	
	private void showGoods(ArrayList<Goods> goods){
    	//在表格中显示数据
 		DefaultTableModel dt=(DefaultTableModel) table.getModel();
 		dt.setRowCount(0);
 		
 		for(int i=0;i<goods.size();i++) {
			Goods g=goods.get(i);
			dt.insertRow(i, new Object[] {
					g.getId(),g.getName(),g.getPrice(),g.getNum(),g.getPrice()*g.getNum()
					
			});
		}
    }

}


返回 Java程序设计