“Java任务:商品信息检索之客户端”的版本间的差异
(未显示同一用户的6个中间版本) | |||
第22行: | 第22行: | ||
在src下,或者对应的包下,右键new-other | 在src下,或者对应的包下,右键new-other | ||
+ | |||
[[文件:java9-32.png]] | [[文件:java9-32.png]] | ||
第42行: | 第43行: | ||
[[文件:java9-4.png]] | [[文件:java9-4.png]] | ||
− | === | + | ===添加各个子面板=== |
+ | |||
设置默认面板的布局方式: | 设置默认面板的布局方式: | ||
第89行: | 第91行: | ||
按照下图,完成商品显示区域的构建: | 按照下图,完成商品显示区域的构建: | ||
− | [[文件: | + | [[文件:java2020-12-27.png]] |
− | |||
− | |||
*把JScrollPane拖到界面中,并拉伸成上图大小。 | *把JScrollPane拖到界面中,并拉伸成上图大小。 | ||
第138行: | 第138行: | ||
==事件响应== | ==事件响应== | ||
− | |||
− | + | ===为“查询”按钮添加响应事件=== | |
− | + | 双击图形界面中“查询”按钮,即可进入其响应事件的编写: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | <nowiki> 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 | + | goods.add(mygoods);goods.add(mygoods2); |
− | |||
− | |||
showGoods(goods); | showGoods(goods); | ||
+ | JOptionPane.showMessageDialog(null, "'"+type+"'查询成功!", "信息", JOptionPane.INFORMATION_MESSAGE); | ||
} | } | ||
});</nowiki> | });</nowiki> | ||
+ | |||
+ | 显示数据的函数showGoods(): | ||
+ | |||
<nowiki> | <nowiki> | ||
第170行: | 第171行: | ||
Goods g=goods.get(i); | Goods g=goods.get(i); | ||
dt.insertRow(i, new Object[] { | dt.insertRow(i, new Object[] { | ||
− | g.getId(),g.getName(),g.getNum(),g.getPrice() | + | g.getId(),g.getName(),g.getPrice(),g.getNum(),g.getPrice()*g.getNum() |
}); | }); | ||
} | } | ||
− | }</nowiki> | + | } |
+ | </nowiki> | ||
+ | |||
+ | ===为"购买”按钮添加响应事件=== | ||
+ | <nowiki> | ||
+ | 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); | ||
+ | |||
+ | } | ||
+ | });</nowiki> | ||
+ | ===退出系统的响应事件=== | ||
+ | |||
+ | <nowiki> | ||
+ | lbllogout.addMouseListener(new MouseAdapter() { | ||
+ | @Override | ||
+ | public void mouseClicked(MouseEvent e) { | ||
+ | |||
+ | GoodsShow.this.dispose(); | ||
+ | |||
+ | } | ||
+ | });</nowiki> | ||
+ | |||
+ | ===其他按钮的响应事件=== | ||
+ | 由于还未连接后台,其他按钮如查看详情按钮,可暂不编写响应事件。 | ||
+ | |||
==程序代码== | ==程序代码== | ||
− | + | 以下代码绝大多数为机器自动生成, | |
− | 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; | ||
+ | public class GoodsShow extends JFrame { | ||
− | + | private JPanel contentPane; | |
− | + | private JTextField GoodsNameField; | |
− | private | + | private JTable table; |
− | private | + | GoodsService gs; |
− | private | + | CartsService cs; |
− | + | User user; | |
− | |||
− | |||
− | |||
/** | /** | ||
− | * | + | * Launch the application. |
*/ | */ | ||
− | public | + | 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); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | btnQuery.setBounds( | ||
panel2.add(btnQuery); | panel2.add(btnQuery); | ||
− | |||
− | |||
JScrollPane scrollPane = new JScrollPane(); | JScrollPane scrollPane = new JScrollPane(); | ||
− | scrollPane.setBounds(10, | + | 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 | + | JLabel lblQuantity = new JLabel("0 \u4EF6"); |
− | + | lblQuantity.setBounds(98, 10, 77, 15); | |
− | + | panel4.add(lblQuantity); | |
− | JLabel | + | 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(); | |
− | JButton btnBuy = new JButton(" | + | int gid; |
− | btnBuy. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | //当用户没有选择商品时 | |
− | + | 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() | |
− | + | ||
− | + | }); | |
+ | } | ||
+ | } | ||
+ | |||
} | } | ||
+ | |||
</nowiki> | </nowiki> | ||
返回 [[Java程序设计]] | 返回 [[Java程序设计]] |
2020年6月19日 (五) 11:17的最新版本
目录
任务描述
任务描述:实现商品检索界面
客户端界面
客户端实现商品的查询、购买,和查改购买情况
管理端
管理端实现商品的上架、下架、查询和修改功能
任务实现
下面以客户端为例,讲解实现的步骤。
构建初始窗体
打开你的Java Project
在src下,或者对应的包下,右键new-other
选择window builder下的 swing下的JFrame或者Application Window
给生成的窗体起名字,如我的命名为GoodsShow.
进入视图界面
在类名上单击右键,选择 Open With WindowBuilder Editor,如图2所示
在类编写窗口的底部选择【Design】标签。
添加各个子面板
设置默认面板的布局方式:
这里推荐使用绝对布局(absolute layout),添加组件更方便、自由。
绝对布局,顾名思义,就是给每个元素指定上起始点和长、宽、高,使用它的物理长度值进行定位和布局
根据界面的需要,我们可以向默认面板中4个子面板
其中第1、2、4个面板为普通的JPanal ,注意设置其布局方式为绝对布局
第3个面板为JScrollPane
面板1、2添加元素
按照下图,把对应控件拖入窗体到指定位置,完成商品检索区域的构建,并设置各个控件的【Variable】属性:
各组件参考命名:
欢迎你 -> lblUser ; 退出系统 -> lbllogout 商品名 -> lblGoodsName ;输入框 ->GoodsNameField ;分类标签 -> lblType ;分类下拉框-> TypeBox ;查询 -> btnQuery
分类下拉框的设置
- 选中分类组合框,在左侧的Properties窗口中找到Model这栏,如图:
- 点击上图的“…”添加如下图所示的三个分类:
每新添加一个记录,就插入一个新行就行
面板3:表格的创建
按照下图,完成商品显示区域的构建:
- 把JScrollPane拖到界面中,并拉伸成上图大小。
- 把商品列表JTable嵌到JScrollPane:必须要把JTable控件拖入JScrollPane的Viewport区域中才能显示,拖入的区域如下图中的黄色区域所示:
图 7 JScollPane布局
- 设置表头标题:
点击左侧控件列表中的JTable控件。
图 1-10
在属性列表中找到【model】,点击【…】打开表格编辑框:
图 8 表格编辑对话框
点击【Columns】区域的【Insert】插入一列, 在底部的列属性区域中设置【Title】为总价:,设置列宽【Pref.width】为:164
按照以上步骤,依次添加如下表头信息:
列名:数量,列宽:70 列名:价格,列宽:70 列名:商品名称,列宽:150 列名:商品编号,列宽:70
面板4:购买区域的创建
按照下图,完成商品购买区域的构建:
- "购物车商品数": 标签 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程序设计