“Java任务:图书信息检索”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
任务实现
实施思路
第12行: 第12行:
 
第一步:启动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>
 
第二步:完善商品信息检索界面。
 
第二步:完善商品信息检索界面。
  

2018年5月27日 (日) 07:57的版本

任务描述

任务描述:图书信息检索

实现商品信息检索,如图9-1所示。

Java9-2.png

图 1商品信息检索

任务实现

实施思路

第一步:启动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所示

Java9-3.png

图 2 打开菜单

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

Java9-4.png

图 3

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

Java9-13.png

图 4 组件设置

3.设置分类下拉框:

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

Java9-6.png

图 5 组合框设置

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

Java9-7.png

图 6 组合框设置

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

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

5. 按照下图,完成商品购买区域的构建:

Java9-12.png

图 9 商品购买区域

第三步:定义显示书籍数据的函数:

private void showBooks(String name,int category) {
		BookDao dao=new BookDaoTesting();
		List<Book> books=dao.queryBook(name, category);
		//在表格中显示数据
		DefaultTableModel dt=(DefaultTableModel) tblBook.getModel();
		dt.setRowCount(0);
		//判断是否查询到了满足条件的书籍信息
		if(books==null||books.size()==0) {
			JOptionPane.showMessageDialog(frame, "没有满足条件的书籍信息","警告信息",JOptionPane.WARNING_MESSAGE);
			return;
		}
		for(int i=0;i<books.size();i++) {
			Book book=books.get(i);
			dt.insertRow(i, new Object[] {
					book.getBid(),book.getName(),book.getAuthor(),book.getNumber(),
					book.getCategory().toString()
			});
		}
} 
第四步:为查询按钮添加事件处理程序
		btnQuery.addActionListener(e->{
			String name=txtBookName.getText();
			int category=cboCategory.getSelectedIndex();
			//显示书籍
			showBooks(name,category);
		});

第五步:设置初始化窗口时,加载所有数据

showBooks(null,0);

程序代码

public class BookListForm {

	private JFrame frame;
	private JLabel lblName;
	private JLabel lblCity;
	private JTextField txtBookName;
	private JLabel lblCategory;
	private JTable tblBook;

	public JFrame getFrame() {
		return frame;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookListForm window = new BookListForm();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public BookListForm() {

		initialize();
		setCenter(frame);
	}
	private void showBooks(String name,int category) {
		BookDao dao=new BookDaoTesting();
		List<Book> books=dao.queryBook(name, category);
		//在表格中显示数据
		DefaultTableModel dt=(DefaultTableModel) tblBook.getModel();
		dt.setRowCount(0);
		//判断是否查询到了满足条件的书籍信息
		if(books==null||books.size()==0) {
			JOptionPane.showMessageDialog(frame, "没有满足条件的书籍信息","警告信息",JOptionPane.WARNING_MESSAGE);
			return;
		}
		for(int i=0;i<books.size();i++) {
			Book book=books.get(i);
			dt.insertRow(i, new Object[] {
					book.getBid(),book.getName(),book.getAuthor(),book.getNumber(),
					book.getCategory().toString()
			});
		}
	}
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.getContentPane().setLayout(null);
		frame.setTitle("\u7535\u5546\u8D2D\u7269\u5E73\u53F0-\u5546\u54C1\u67E5\u8BE2\u9875\u9762");
		frame.setBounds(100, 100, 550, 450);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JLabel lblWelcome = new JLabel("您好,");
		lblWelcome.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		lblWelcome.setBounds(10, 10, 42, 15);
		frame.getContentPane().add(lblWelcome);

		lblName = new JLabel("");
		lblName.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		lblName.setBounds(55, 10, 91, 15);
		frame.getContentPane().add(lblName);

		lblCity = new JLabel("来自于:");
		lblCity.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		lblCity.setBounds(434, 10, 100, 15);
		frame.getContentPane().add(lblCity);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 35, 524, 2);
		frame.getContentPane().add(separator);
		
		JLabel lblBookName = new JLabel("书籍名:");
		lblBookName.setBounds(10, 47, 54, 21);
		frame.getContentPane().add(lblBookName);
		
		txtBookName = new JTextField();
		txtBookName.setBounds(65, 47, 133, 21);
		frame.getContentPane().add(txtBookName);
		txtBookName.setColumns(20);
		
		lblCategory = new JLabel("分类:");
		lblCategory.setBounds(226, 47, 66, 21);
		frame.getContentPane().add(lblCategory);
		
		JComboBox cboCategory = new JComboBox();
		cboCategory.setModel(new DefaultComboBoxModel(new String[] {"-请选择-", "工具类>软件编程", "小说类>历史"}));
		cboCategory.setBounds(260, 47, 147, 21);
		frame.getContentPane().add(cboCategory);
		
		JButton btnQuery = new JButton("查询");
		btnQuery.setBounds(441, 46, 78, 23);
		frame.getContentPane().add(btnQuery);
		btnQuery.addActionListener(e->{
			String name=txtBookName.getText();
			int category=cboCategory.getSelectedIndex();
			//显示书籍
			showBooks(name,category);
		});
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 89, 524, 266);
		frame.getContentPane().add(scrollPane);
		
		tblBook = new JTable();
		tblBook.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u4E66\u7C4D\u7F16\u53F7", "\u4E66\u7C4D\u540D\u79F0", "\u4E66\u7C4D\u4F5C\u8005", "\u5E93\u5B58", "\u4E66\u7C4D\u5206\u7C7B"
			}
		));
		tblBook.getColumnModel().getColumn(0).setPreferredWidth(70);
		tblBook.getColumnModel().getColumn(1).setPreferredWidth(150);
		tblBook.getColumnModel().getColumn(2).setPreferredWidth(70);
		tblBook.getColumnModel().getColumn(3).setPreferredWidth(70);
		tblBook.getColumnModel().getColumn(4).setPreferredWidth(164);
		scrollPane.setViewportView(tblBook);
		//初始化显示所有书籍
		showBooks(null,0);
		
		JLabel lblCart = new JLabel("购物车商品数:");
		lblCart.setBounds(10, 379, 91, 15);
		frame.getContentPane().add(lblCart);
		
		JLabel lblQuantity = new JLabel("0件");
		lblQuantity.setBounds(104, 379, 54, 15);
		frame.getContentPane().add(lblQuantity);
		
		JLabel lblCartDetail = new JLabel("查看详情");
		lblCartDetail.setForeground(Color.BLUE);
		lblCartDetail.setBounds(179, 379, 54, 15);
		frame.getContentPane().add(lblCartDetail);
		
		JButton btnBuy = new JButton("购买");
		btnBuy.setBounds(441, 375, 78, 23);
		frame.getContentPane().add(btnBuy);

	}

	/**
	 * 窗体自动居中
	 * 
	 * @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);

	}
}