“Java任务:图书信息检索”的版本间的差异
来自CloudWiki
(→任务实现) |
|||
(未显示同一用户的16个中间版本) | |||
第1行: | 第1行: | ||
==任务描述== | ==任务描述== | ||
− | + | 任务描述:实现商品检索界面 | |
− | + | ===客户端界面=== | |
+ | |||
+ | 客户端实现商品的查询、购买,和查改购买情况 | ||
[[文件:java9-2.png]] | [[文件:java9-2.png]] | ||
− | + | ===管理端=== | |
+ | |||
+ | 管理端实现商品的上架、下架、查询和修改功能 | ||
+ | |||
+ | [[文件:java9-37.png]] | ||
==任务实现== | ==任务实现== | ||
− | === | + | ===初始代码=== |
+ | 下面以客户端为例,讲解实现的步骤。 | ||
+ | |||
第一步:启动Eclipse,导入Java Project,工程名为Project0901。 | 第一步:启动Eclipse,导入Java Project,工程名为Project0901。 | ||
+ | <nowiki>package main; | ||
+ | |||
+ | import java.awt.Dimension; | ||
+ | import java.awt.Toolkit; | ||
+ | import javax.swing.JFrame; | ||
+ | |||
+ | /*实训:Java任务:图书信息检索*/ | ||
+ | public class BookListForm { | ||
+ | private JFrame frame; | ||
+ | /** | ||
+ | * 构造函数 | ||
+ | */ | ||
+ | public BookListForm() { | ||
+ | initialize(); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 窗体初始化 | ||
+ | */ | ||
+ | private void initialize() { | ||
+ | frame = new JFrame(); | ||
+ | |||
+ | frame.setResizable(false); | ||
+ | frame.getContentPane().setLayout(null); | ||
+ | frame.setTitle("电商购物平台-商品查询页面"); | ||
+ | frame.setBounds(100, 100, 434, 430); | ||
+ | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
+ | |||
+ | frame.setVisible(true); | ||
+ | setCenter(frame); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * 窗体自动居中 | ||
+ | * | ||
+ | * @param window | ||
+ | */ | ||
+ | private void setCenter(JFrame window) { | ||
+ | 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) { | ||
+ | this.frame = frame; | ||
+ | } | ||
+ | public static void main(String[] args) { | ||
+ | |||
+ | BookListForm bookForm = new BookListForm(); | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | }</nowiki> | ||
+ | ===布局面板=== | ||
第二步:完善商品信息检索界面。 | 第二步:完善商品信息检索界面。 | ||
第26行: | 第94行: | ||
图 3 | 图 3 | ||
+ | [[文件:java9-14.png|700px]] | ||
+ | ===向面板中添加元素=== | ||
2.按照下图,把对应控件拖入窗体到指定位置,完成商品检索区域的构建,并设置各个控件的【Variable】属性: | 2.按照下图,把对应控件拖入窗体到指定位置,完成商品检索区域的构建,并设置各个控件的【Variable】属性: | ||
第32行: | 第102行: | ||
图 4 组件设置 | 图 4 组件设置 | ||
− | + | ||
+ | |||
+ | ====分类下拉框的设置==== | ||
*选中分类组合框,在左侧的Properties窗口中找到Model这栏,如图: | *选中分类组合框,在左侧的Properties窗口中找到Model这栏,如图: | ||
第46行: | 第118行: | ||
图 6 组合框设置 | 图 6 组合框设置 | ||
− | + | ====表格的创建==== | |
+ | 按照下图,完成商品显示区域的构建: | ||
[[文件:java9-8.png]] | [[文件:java9-8.png]] | ||
第64行: | 第137行: | ||
点击左侧控件列表中的JTable控件。 | 点击左侧控件列表中的JTable控件。 | ||
− | [[java9-10.png]] | + | [[文件:java9-10.png]] |
图 1-10 | 图 1-10 | ||
第83行: | 第156行: | ||
列名:书籍编号,列宽:70</nowiki> | 列名:书籍编号,列宽:70</nowiki> | ||
+ | ====下部购买区域的创建==== | ||
5. 按照下图,完成商品购买区域的构建: | 5. 按照下图,完成商品购买区域的构建: | ||
第89行: | 第163行: | ||
图 9 商品购买区域 | 图 9 商品购买区域 | ||
− | + | ===事件响应=== | |
+ | 为“查询”按钮添加响应事件 | ||
+ | |||
+ | <nowiki>JButton btnNewButton = new JButton("\u67E5\u8BE2"); | ||
+ | btnNewButton.addActionListener(new ActionListener() { | ||
+ | public void actionPerformed(ActionEvent arg0) { | ||
+ | String gName = textField.getText(); | ||
+ | JOptionPane.showMessageDialog(null, gName, "信息", JOptionPane.INFORMATION_MESSAGE); | ||
+ | } | ||
+ | });</nowiki> | ||
+ | |||
+ | 为“查询”按钮添加响应事件 | ||
− | <nowiki> | + | <nowiki>btnNewButton.addActionListener(new ActionListener() { |
− | + | public void actionPerformed(ActionEvent arg0) { | |
− | + | String gName = textField.getText(); | |
− | //在表格中显示数据 | + | JOptionPane.showMessageDialog(null, gName, "信息", JOptionPane.INFORMATION_MESSAGE); |
− | + | Goods mygoods = new Goods("01",gName,15.0f,2); | |
− | + | ArrayList<Goods> goods=new ArrayList<>(); | |
− | + | goods.add(mygoods); | |
− | + | showGoods(goods); | |
− | + | } | |
− | + | });</nowiki> | |
− | + | ||
− | + | <nowiki> | |
− | + | 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[] { | dt.insertRow(i, new Object[] { | ||
− | + | g.getId(),g.getName(),g.getNum(),g.getPrice(), | |
− | + | ||
}); | }); | ||
} | } | ||
− | } | + | }</nowiki> |
− | + | ||
− | + | ==程序代码== | |
− | + | <nowiki>package main; | |
− | + | ||
− | + | import java.awt.*; | |
− | |||
− | |||
− | + | import javax.swing.*; | |
+ | import javax.swing.table.DefaultTableModel; | ||
− | |||
− | |||
− | |||
+ | /*实训:Java任务:图书信息检索 (代码编程版) */ | ||
+ | public class BookListForm { | ||
private JFrame frame; | private JFrame frame; | ||
private JLabel lblName; | private JLabel lblName; | ||
private JLabel lblCity; | private JLabel lblCity; | ||
+ | |||
private JTextField txtBookName; | private JTextField txtBookName; | ||
private JLabel lblCategory; | private JLabel lblCategory; | ||
private JTable tblBook; | private JTable tblBook; | ||
− | |||
− | |||
− | |||
− | |||
− | |||
/** | /** | ||
− | * | + | * 构造函数 |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
*/ | */ | ||
public BookListForm() { | public BookListForm() { | ||
− | |||
initialize(); | initialize(); | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
+ | |||
/** | /** | ||
− | * | + | * 窗体初始化 |
*/ | */ | ||
private void initialize() { | private void initialize() { | ||
frame = new JFrame(); | frame = new JFrame(); | ||
− | frame.setResizable( | + | |
− | frame | + | frame.setResizable(true); |
− | frame. | + | //frame.setLayout(null); |
− | + | frame.setLayout(null); | |
− | + | ||
− | + | //上半部分 | |
− | + | JPanel panel1=new JPanel(); | |
− | + | panel1.setLayout(null); | |
− | + | panel1.setBounds(0, 0, 550, 45); | |
− | + | ||
− | + | lblName = new JLabel("您好"); | |
− | lblName = new JLabel("" | + | lblName.setBounds(10, 10, 131, 15); |
− | + | panel1.add(lblName); | |
− | lblName.setBounds( | + | |
− | |||
− | |||
lblCity = new JLabel("来自于:"); | lblCity = new JLabel("来自于:"); | ||
− | + | lblCity.setBounds(271, 10, 90, 15); | |
− | lblCity.setBounds( | + | panel1.add(lblCity); |
− | + | ||
− | |||
JSeparator separator = new JSeparator(); | JSeparator separator = new JSeparator(); | ||
− | separator.setBounds(10, 35, | + | separator.setBounds(10, 35, 550, 10); |
− | + | panel1.add(separator); | |
− | + | frame.getContentPane().add(panel1); | |
− | + | /* | |
− | + | String[] names = { "姓名", "语文", "数学", "总分", "作弊" }; | |
+ | Object[][] scores = { { "张三", new Integer(66), new Integer(32), new Integer(98), new Boolean(false) }, | ||
+ | { "李四", new Integer(82), new Integer(69), new Integer(128), new Boolean(true) }, }; | ||
+ | JTable table=new JTable(scores,names);//创建表格对象 | ||
− | + | JScrollPane scrollPane = new JScrollPane(table);//将表格添加到(滚动)面板中 | |
− | + | table.setFillsViewportHeight(true); | |
− | frame | + | frame.add(scrollPane,BorderLayout.CENTER); |
− | |||
− | + | */ | |
− | |||
− | |||
− | + | JPanel panel2=new JPanel(); | |
− | + | panel2.setLayout(null); | |
− | + | panel2.setBounds(0, 45, 550, 20); | |
− | |||
+ | //书籍名的 标签 | ||
+ | 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("查询"); | JButton btnQuery = new JButton("查询"); | ||
− | btnQuery.setBounds(441, | + | btnQuery.setBounds(441, 2, 78, 20); |
− | + | panel2.add(btnQuery); | |
− | + | ||
− | + | frame.getContentPane().add(panel2); | |
− | |||
− | |||
− | |||
− | |||
JScrollPane scrollPane = new JScrollPane(); | JScrollPane scrollPane = new JScrollPane(); | ||
− | scrollPane.setBounds(10, | + | scrollPane.setBounds(10, 90, 550, 265); |
frame.getContentPane().add(scrollPane); | frame.getContentPane().add(scrollPane); | ||
− | tblBook = new JTable(); | + | //tblBook = new JTable(); |
− | + | String[] names = { "编号","商品名称", "价格", "数量", "标签" }; | |
− | + | Object[][] scores = { { "01", "think in java", new Float(50.0f), new Integer(50), "软件编程" }, | |
− | + | { "03", "think in java", new Float(50.0f), new Integer(40), "软件编程"}, | |
− | + | {"03", "射雕英雄传", new Float(30.0f), new Integer(30), "文学经典" }, | |
− | + | }; | |
− | + | ||
− | + | ||
− | + | JTable tblBook=new JTable(scores,names);//创建表格对象 | |
− | |||
− | |||
− | |||
− | tblBook | ||
scrollPane.setViewportView(tblBook); | scrollPane.setViewportView(tblBook); | ||
− | + | ||
− | + | JPanel panel3=new JPanel(); | |
+ | panel3.setLayout(null); | ||
+ | panel3.setBounds(0, 355, 550, 45); | ||
JLabel lblCart = new JLabel("购物车商品数:"); | JLabel lblCart = new JLabel("购物车商品数:"); | ||
− | lblCart.setBounds(10, | + | lblCart.setBounds(10, 10, 91, 15); |
− | + | panel3.add(lblCart); | |
+ | |||
JLabel lblQuantity = new JLabel("0件"); | JLabel lblQuantity = new JLabel("0件"); | ||
− | lblQuantity.setBounds(104, | + | lblQuantity.setBounds(104, 10, 54, 15); |
− | + | panel3.add(lblQuantity); | |
+ | |||
JLabel lblCartDetail = new JLabel("查看详情"); | JLabel lblCartDetail = new JLabel("查看详情"); | ||
lblCartDetail.setForeground(Color.BLUE); | lblCartDetail.setForeground(Color.BLUE); | ||
− | lblCartDetail.setBounds(179, | + | lblCartDetail.setBounds(179, 10, 54, 15); |
− | + | panel3.add(lblCartDetail); | |
JButton btnBuy = new JButton("购买"); | JButton btnBuy = new JButton("购买"); | ||
− | btnBuy.setBounds(441, | + | btnBuy.setBounds(441, 10, 78, 23); |
− | frame.getContentPane().add( | + | 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); | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
} | } | ||
− | + | ||
/** | /** | ||
* 窗体自动居中 | * 窗体自动居中 | ||
第286行: | 第368行: | ||
int width = window.getWidth(); | int width = window.getWidth(); | ||
int height = window.getHeight(); | int height = window.getHeight(); | ||
− | window.setBounds((d.width - width) / 2, (d.height - height) / 2, width, | + | window.setBounds((d.width-width)/2, (d.height-height)/2, width, height); |
− | + | } | |
+ | public JFrame getFrame() { | ||
+ | return frame; | ||
+ | } | ||
+ | public void setFrame(JFrame frame) { | ||
+ | this.frame = frame; | ||
} | } | ||
+ | public static void main(String[] args) { | ||
+ | |||
+ | BookListForm bookForm = new BookListForm(); | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | |||
} | } | ||
</nowiki> | </nowiki> | ||
+ | |||
+ | 返回 [[Java程序设计]] |
2020年6月12日 (五) 02:11的最新版本
目录
任务描述
任务描述:实现商品检索界面
客户端界面
客户端实现商品的查询、购买,和查改购买情况
管理端
管理端实现商品的上架、下架、查询和修改功能
任务实现
初始代码
下面以客户端为例,讲解实现的步骤。
第一步:启动Eclipse,导入Java Project,工程名为Project0901。
package main; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; /*实训:Java任务:图书信息检索*/ public class BookListForm { private JFrame frame; /** * 构造函数 */ public BookListForm() { initialize(); } /** * 窗体初始化 */ private void initialize() { frame = new JFrame(); frame.setResizable(false); frame.getContentPane().setLayout(null); frame.setTitle("电商购物平台-商品查询页面"); frame.setBounds(100, 100, 434, 430); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); setCenter(frame); } /** * 窗体自动居中 * * @param window */ private void setCenter(JFrame window) { 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) { this.frame = frame; } public static void main(String[] args) { BookListForm bookForm = new BookListForm(); } }
布局面板
第二步:完善商品信息检索界面。
1. 在BookListForm类上单击右键,选择 Open With WindowBuilder Editor,如图2所示
图 2 打开菜单
在类编写窗口的底部选择【Design】标签。
图 3
向面板中添加元素
2.按照下图,把对应控件拖入窗体到指定位置,完成商品检索区域的构建,并设置各个控件的【Variable】属性:
图 4 组件设置
分类下拉框的设置
- 选中分类组合框,在左侧的Properties窗口中找到Model这栏,如图:
图 5 组合框设置
- 点击上图的“…”添加如下图所示的三个分类:
图 6 组合框设置
表格的创建
按照下图,完成商品显示区域的构建:
图 1-8
- 把JScrollPane拖到界面中,并拉伸成上图大小。
- 把商品列表JTable嵌到JScrollPane:必须要把JTable控件拖入JScrollPane的Viewport区域中才能显示,拖入的区域如下图中的黄色区域所示:
图 7 JScollPane布局
- 设置表头标题:
点击左侧控件列表中的JTable控件。
图 1-10
在属性列表中找到【model】,点击【…】打开表格编辑框:
图 8 表格编辑对话框
点击【Columns】区域的【Insert】插入一列, 在底部的列属性区域中设置【Title】为:书籍分类,设置列宽【Pref.width】为:164
按照以上步骤,依次添加如下表头信息:
列名:库存,列宽:70 列名:书籍作者,列宽:70 列名:书籍名称,列宽:150 列名:书籍编号,列宽:70
下部购买区域的创建
5. 按照下图,完成商品购买区域的构建:
图 9 商品购买区域
事件响应
为“查询”按钮添加响应事件
JButton btnNewButton = new JButton("\u67E5\u8BE2"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String gName = textField.getText(); JOptionPane.showMessageDialog(null, gName, "信息", JOptionPane.INFORMATION_MESSAGE); } });
为“查询”按钮添加响应事件
btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String gName = textField.getText(); JOptionPane.showMessageDialog(null, gName, "信息", JOptionPane.INFORMATION_MESSAGE); Goods mygoods = new Goods("01",gName,15.0f,2); ArrayList<Goods> goods=new ArrayList<>(); goods.add(mygoods); showGoods(goods); } });
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.getNum(),g.getPrice(), }); } }
程序代码
package main; import java.awt.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; /*实训:Java任务:图书信息检索 (代码编程版) */ public class BookListForm { private JFrame frame; private JLabel lblName; private JLabel lblCity; private JTextField txtBookName; private JLabel lblCategory; private JTable tblBook; /** * 构造函数 */ public BookListForm() { initialize(); } /** * 窗体初始化 */ private void initialize() { frame = new JFrame(); frame.setResizable(true); //frame.setLayout(null); frame.setLayout(null); //上半部分 JPanel panel1=new JPanel(); panel1.setLayout(null); panel1.setBounds(0, 0, 550, 45); lblName = new JLabel("您好"); lblName.setBounds(10, 10, 131, 15); panel1.add(lblName); lblCity = new JLabel("来自于:"); lblCity.setBounds(271, 10, 90, 15); panel1.add(lblCity); JSeparator separator = new JSeparator(); separator.setBounds(10, 35, 550, 10); panel1.add(separator); frame.getContentPane().add(panel1); /* String[] names = { "姓名", "语文", "数学", "总分", "作弊" }; Object[][] scores = { { "张三", new Integer(66), new Integer(32), new Integer(98), new Boolean(false) }, { "李四", new Integer(82), new Integer(69), new Integer(128), new Boolean(true) }, }; JTable table=new JTable(scores,names);//创建表格对象 JScrollPane scrollPane = new JScrollPane(table);//将表格添加到(滚动)面板中 table.setFillsViewportHeight(true); frame.add(scrollPane,BorderLayout.CENTER); */ JPanel panel2=new JPanel(); panel2.setLayout(null); panel2.setBounds(0, 45, 550, 20); //书籍名的 标签 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); frame.getContentPane().add(panel2); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(10, 90, 550, 265); frame.getContentPane().add(scrollPane); //tblBook = new JTable(); String[] names = { "编号","商品名称", "价格", "数量", "标签" }; Object[][] scores = { { "01", "think in java", new Float(50.0f), new Integer(50), "软件编程" }, { "03", "think in java", new Float(50.0f), new Integer(40), "软件编程"}, {"03", "射雕英雄传", new Float(30.0f), new Integer(30), "文学经典" }, }; JTable tblBook=new JTable(scores,names);//创建表格对象 scrollPane.setViewportView(tblBook); JPanel panel3=new JPanel(); panel3.setLayout(null); panel3.setBounds(0, 355, 550, 45); JLabel lblCart = new JLabel("购物车商品数:"); lblCart.setBounds(10, 10, 91, 15); panel3.add(lblCart); JLabel lblQuantity = new JLabel("0件"); lblQuantity.setBounds(104, 10, 54, 15); panel3.add(lblQuantity); JLabel lblCartDetail = new JLabel("查看详情"); lblCartDetail.setForeground(Color.BLUE); lblCartDetail.setBounds(179, 10, 54, 15); panel3.add(lblCartDetail); 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); } /** * 窗体自动居中 * * @param window */ private void setCenter(JFrame window) { 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) { this.frame = frame; } public static void main(String[] args) { BookListForm bookForm = new BookListForm(); } }
返回 Java程序设计