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

来自CloudWiki
跳转至: 导航搜索
退出系统的响应事件
 
(未显示同一用户的4个中间版本)
第22行: 第22行:
  
 
在src下,或者对应的包下,右键new-other
 
在src下,或者对应的包下,右键new-other
 +
 
[[文件:java9-32.png]]
 
[[文件:java9-32.png]]
  
第90行: 第91行:
 
按照下图,完成商品显示区域的构建:
 
按照下图,完成商品显示区域的构建:
  
[[文件:java9-8.png]]
+
[[文件:java2020-12-27.png]]
 
 
图 1-8
 
  
 
*把JScrollPane拖到界面中,并拉伸成上图大小。
 
*把JScrollPane拖到界面中,并拉伸成上图大小。
第220行: 第219行:
  
 
==程序代码==
 
==程序代码==
<nowiki>package main;
+
以下代码绝大多数为机器自动生成,
 +
 
 +
在这里显示代码的意图是让大家能够参照比对。
  
import java.awt.*;
+
<nowiki>
 +
package main;
 +
import java.awt.BorderLayout;
 +
import java.awt.EventQueue;
  
import javax.swing.*;
+
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 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;
 +
import javax.swing.UIManager;
 +
import java.awt.Font;
  
/*实训:Java任务:图书信息检索  (代码编程版) */
+
public class GoodsShow extends JFrame {
public class BookListForm  {
+
 
private JFrame frame;
+
private JPanel contentPane;
private JLabel lblName;
+
private JTextField GoodsNameField;
private JLabel lblCity;
+
private JTable table;
+
GoodsService gs;
private JTextField txtBookName;
+
CartsService cs;
private JLabel lblCategory;
+
User user;
private JTable tblBook;
 
 
/**
 
/**
* 构造函数
+
* Launch the application.
 
*/
 
*/
public BookListForm() {
+
public static void main(String[] args) {
initialize();
+
EventQueue.invokeLater(new Runnable() {
 +
public void run() {
 +
try {
 +
GoodsShow frame = new GoodsShow();
 +
frame.setVisible(true);
 +
} catch (Exception e) {
 +
e.printStackTrace();
 +
}
 +
}
 +
});
 
}
 
}
+
 
 
/**
 
/**
* 窗体初始化
+
* Create the frame.
 
*/
 
*/
private void initialize() {
+
public GoodsShow() {
frame = new JFrame();
+
       
 +
setTitle("\u5546\u9662\u9762\u9986\u5BA2\u6237\u7AEF");
 +
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);
 
 
frame.setResizable(true);
+
JPanel panel1 = new JPanel();
//frame.setLayout(null);
+
panel1.setBounds(10, 10, 487, 42);
frame.setLayout(null);
+
contentPane.add(panel1);
 +
panel1.setLayout(null);
 
 
//上半部分
+
JLabel lblUser = new JLabel("\u6B22\u8FCE\u4F60\uFF0C");
JPanel  panel1=new JPanel();
+
lblUser.setFont(new Font("宋体", Font.PLAIN, 12));
panel1.setLayout(null);
+
lblUser.setHorizontalAlignment(SwingConstants.LEFT);
panel1.setBounds(0, 0, 550, 45);
+
lblUser.setBounds(21, 11, 87, 22);
 +
panel1.add(lblUser);
 +
JLabel lbllogout = new JLabel("\u9000\u51FA\u7CFB\u7EDF");
 +
lbllogout.setForeground(Color.BLUE);
 +
lbllogout.addMouseListener(new MouseAdapter() {
 +
@Override
 +
public void mouseClicked(MouseEvent e) {
 +
 +
GoodsShow.this.dispose();
 +
 +
}
 +
});
 +
lbllogout.setBounds(403, 15, 54, 15);
 +
panel1.add(lbllogout);
 
 
lblName = new JLabel("您好");
+
lblName.setBounds(10, 10, 131, 15);
 
panel1.add(lblName);
 
 
 
lblCity = new JLabel("来自于:");
+
JPanel panel2 = new JPanel();
lblCity.setBounds(271, 10, 90, 15);
+
panel2.setBounds(10, 59, 487, 42);
panel1.add(lblCity);
+
contentPane.add(panel2);
 +
panel2.setLayout(null);
 
 
JSeparator separator = new JSeparator();
+
JLabel lblGoodsName = new JLabel("\u5546\u54C1\u540D");
separator.setBounds(10, 35, 550, 10);
+
lblGoodsName.setBounds(10, 10, 54, 15);
panel1.add(separator);
+
panel2.add(lblGoodsName);
 
 
frame.getContentPane().add(panel1);
+
GoodsNameField = new JTextField();
/*
+
GoodsNameField.setBounds(60, 7, 110, 21);
String[] names = { "姓名", "语文", "数学", "总分", "作弊" };
+
panel2.add(GoodsNameField);
Object[][] scores = { { "张三", new Integer(66), new Integer(32), new Integer(98), new Boolean(false) },
+
GoodsNameField.setColumns(10);
{ "李四", new Integer(82), new Integer(69), new Integer(128), new Boolean(true) }, };
 
JTable table=new JTable(scores,names);//创建表格对象
 
 
 
JScrollPane scrollPane = new JScrollPane(table);//将表格添加到(滚动)面板中
+
JLabel lblType = new JLabel("\u5206\u7C7B\uFF1A");
table.setFillsViewportHeight(true);
+
lblType.setBounds(181, 10, 54, 15);
frame.add(scrollPane,BorderLayout.CENTER);
+
panel2.add(lblType);
 
 
*/
+
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);
 
 
JPanel panel2=new JPanel();
+
JButton btnQuery = new JButton("\u67E5\u8BE2");
panel2.setLayout(null);
+
btnQuery.addActionListener(new ActionListener() {
panel2.setBounds(0, 45, 550, 20);
+
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);
JLabel lblBookName = new JLabel("书籍名:");
 
lblBookName.setFont(new Font("微软雅黑", Font.PLAIN, 14));
 
lblBookName.setBounds(10, 2, 50, 20);
 
panel2.add(lblBookName);
 
//输入书籍名的文本框
 
txtBookName = new JTextField();
 
txtBookName.setFont(new Font("微软雅黑", Font.PLAIN, 14));
 
txtBookName.setBounds(65, 2, 130, 20);
 
panel2.add(txtBookName);
 
txtBookName.setColumns(10);
 
 
//分类的标签
 
JLabel lblCategory = new JLabel("分类:");
 
lblCategory.setFont(new Font("微软雅黑", Font.PLAIN, 14));
 
lblCategory.setBounds(225, 2, 65, 20);
 
panel2.add(lblCategory);
 
 
//选择分类的复选框
 
    JComboBox cboCategory= new JComboBox();
 
cboCategory.setModel(new DefaultComboBoxModel(new String[] {"- 请选择 -", "工具类 > 软件编程", "小说类 > 历史"}));
 
cboCategory.setFont(new Font("微软雅黑", Font.PLAIN, 14));
 
cboCategory.setBounds(260, 2, 145, 20);
 
panel2.add(cboCategory);
 
JButton btnQuery = new JButton("查询");
 
btnQuery.setBounds(441, 2, 78, 20);
 
 
panel2.add(btnQuery);
 
panel2.add(btnQuery);
 
frame.getContentPane().add(panel2);
 
 
 
 
JScrollPane scrollPane = new JScrollPane();
 
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 90, 550, 265);
+
scrollPane.setBounds(10, 111, 487, 178);
frame.getContentPane().add(scrollPane);
+
contentPane.add(scrollPane);
 
 
//tblBook = new JTable();
+
table = new JTable();
String[] names = { "编号","商品名称", "价格", "数量",  "标签" };
+
table.setModel(new DefaultTableModel(
Object[][] scores = { { "01", "think in java", new Float(50.0f), new Integer(50), "软件编程" },
+
new Object[][] {
{ "03", "think in java", new Float(50.0f), new Integer(40), "软件编程"},  
+
{new Integer(1), "\u5170\u5DDE\u725B\u8089\u9762", new Float(15.0f), null, null},
{"03", "射雕英雄传", new Float(30.0f), new Integer(30), "文学经典" },
+
{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 panel4 = new JPanel();
 +
panel4.setBounds(10, 299, 487, 34);
 +
contentPane.add(panel4);
 +
panel4.setLayout(null);
 
 
JTable tblBook=new JTable(scores,names);//创建表格对象
+
JLabel lblTotal = new JLabel("\u8D2D\u7269\u8F66\u5546\u54C1\u6570");
scrollPane.setViewportView(tblBook);
+
lblTotal.setBounds(10, 10, 78, 15);
 +
panel4.add(lblTotal);
 
 
JPanel panel3=new JPanel();
+
JLabel lblQuantity = new JLabel("0 \u4EF6");
panel3.setLayout(null);
+
lblQuantity.setBounds(98, 10, 77, 15);
panel3.setBounds(0, 355, 550, 45);
+
    panel4.add(lblQuantity);
 
 
JLabel lblCart = new JLabel("购物车商品数:");
 
lblCart.setBounds(10, 10, 91, 15);
 
panel3.add(lblCart);
 
 
 
 +
JLabel lblCart = new JLabel("\u67E5\u770B\u8BE6\u60C5");
 
 
JLabel lblQuantity = new JLabel("0件");
+
lblCart.setForeground(Color.BLUE);
lblQuantity.setBounds(104, 10, 54, 15);
+
lblCart.setBounds(185, 10, 54, 15);
panel3.add(lblQuantity);
+
panel4.add(lblCart);
 
 
+
JButton btnBuy = new JButton("\u8D2D\u4E70");
JLabel lblCartDetail = new JLabel("查看详情");
+
btnBuy.addActionListener(new ActionListener() {
lblCartDetail.setForeground(Color.BLUE);
+
public void actionPerformed(ActionEvent e) {
lblCartDetail.setBounds(179, 10, 54, 15);
+
//获取用户在商品表格中选择的行
panel3.add(lblCartDetail);
+
int selectRow = table.getSelectedRow();
+
int gid;
JButton btnBuy = new JButton("购买");
 
btnBuy.setBounds(441, 10, 78, 23);
 
panel3.add(btnBuy);
 
frame.getContentPane().add(panel3);
 
 
 
frame.setTitle("电商购物平台-商品查询页面");
 
frame.setBounds(100, 100, 550, 450);
 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
frame.setVisible(true);
 
setCenter(frame);
 
 
 
+
//当用户没有选择商品时
+
if(selectRow<0) {
+
JOptionPane.showMessageDialog(null, "请选择你所需要购买的商品","错误信息",JOptionPane.WARNING_MESSAGE);
+
return;
+
}else {
+
String id =(String)table.getModel().getValueAt(selectRow, 0);
+
gid=Integer.valueOf(id);
+
JOptionPane.showMessageDialog(null, "您选择的是"+gid+"号商品", "信息", JOptionPane.INFORMATION_MESSAGE);
+
}
}
+
+
//询问顾客购买商品数量
/**
+
int quantity=Integer.parseInt(JOptionPane.showInputDialog("你需要购买数量是:"));
* 窗体自动居中
+
JOptionPane.showMessageDialog(null, "您选择的是"+gid+"号商品,购买数量为"+quantity, "信息", JOptionPane.INFORMATION_MESSAGE);
*
+
}
* @param window
+
});
*/
+
btnBuy.setBounds(293, 6, 93, 23);
private void setCenter(JFrame window) {
+
panel4.add(btnBuy);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
+
 
int width = window.getWidth();
 
int height = window.getHeight();
 
window.setBounds((d.width-width)/2, (d.height-height)/2, width, height);
 
}
 
public JFrame getFrame() {
 
return frame;
 
 
}
 
}
  
public void setFrame(JFrame frame) {
+
private void showGoods(ArrayList<Goods> goods){
this.frame = frame;
+
    //在表格中显示数据
}
+
DefaultTableModel dt=(DefaultTableModel) table.getModel();
public static void main(String[] args) {
+
dt.setRowCount(0);
+
BookListForm bookForm = new BookListForm();
+
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()
+
+
});
 +
}
 +
    }
 +
 
 
}
 
}
 +
 
</nowiki>
 
</nowiki>
  
 
返回 [[Java程序设计]]
 
返回 [[Java程序设计]]

2020年6月19日 (五) 11:17的最新版本

任务描述

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

客户端界面

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

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:表格的创建

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

Java2020-12-27.png

  • 把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-32.png

  • "购物车商品数": 标签 lblTotal
  • "0件":标签 lblQuantity
  • "查看详情" : 标签 lblCart
  • "购买": 标签 btnBuy

事件响应

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

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

		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()
					
			});
		}
    }

为"购买”按钮添加响应事件

btnBuy.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);
					JOptionPane.showMessageDialog(null, "您选择的是"+gid+"号商品", "信息", JOptionPane.INFORMATION_MESSAGE);
				}
								
				//询问顾客购买商品数量
				int quantity=Integer.parseInt(JOptionPane.showInputDialog("你需要购买数量是:"));
				JOptionPane.showMessageDialog(null, "您选择的是"+gid+"号商品,购买数量为"+quantity, "信息", 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;
import javax.swing.UIManager;
import java.awt.Font;

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\u5BA2\u6237\u7AEF");
		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.setFont(new Font("宋体", Font.PLAIN, 12));
		lblUser.setHorizontalAlignment(SwingConstants.LEFT);
		lblUser.setBounds(21, 11, 87, 22);
		panel1.add(lblUser);
		JLabel lbllogout = new JLabel("\u9000\u51FA\u7CFB\u7EDF");
		lbllogout.setForeground(Color.BLUE);
		lbllogout.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				GoodsShow.this.dispose();
				
			}
		});
		lbllogout.setBounds(403, 15, 54, 15);
		panel1.add(lbllogout);
		
			
		
		JPanel panel2 = new JPanel();
		panel2.setBounds(10, 59, 487, 42);
		contentPane.add(panel2);
		panel2.setLayout(null);
		
		JLabel lblGoodsName = new JLabel("\u5546\u54C1\u540D");
		lblGoodsName.setBounds(10, 10, 54, 15);
		panel2.add(lblGoodsName);
		
		GoodsNameField = new JTextField();
		GoodsNameField.setBounds(60, 7, 110, 21);
		panel2.add(GoodsNameField);
		GoodsNameField.setColumns(10);
		
		JLabel lblType = new JLabel("\u5206\u7C7B\uFF1A");
		lblType.setBounds(181, 10, 54, 15);
		panel2.add(lblType);
		
		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 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");
		lblTotal.setBounds(10, 10, 78, 15);
		panel4.add(lblTotal);
		
		JLabel lblQuantity = new JLabel("0 \u4EF6");
		lblQuantity.setBounds(98, 10, 77, 15);
	    panel4.add(lblQuantity);
		
		
		JLabel lblCart = new JLabel("\u67E5\u770B\u8BE6\u60C5");
		
		lblCart.setForeground(Color.BLUE);
		lblCart.setBounds(185, 10, 54, 15);
		panel4.add(lblCart);
		
		JButton btnBuy = new JButton("\u8D2D\u4E70");
		btnBuy.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);
					JOptionPane.showMessageDialog(null, "您选择的是"+gid+"号商品", "信息", 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);

	}

	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程序设计