java7

(一)学习总结

1.对图片标签的初始化

private JLabel titleLabel = new JLabel(new ImageIcon("pet.jpg"));

2.对按钮的初始化

private JButton btn1 = new JButton("进入系统");

3.对整个窗口的初始化

(1)设置标题

this.setTitle()

(2)设置窗口的尺寸

this.setSize(x,y)

(3)窗体大小固定

this.setResizable(false);

(4)窗口可关闭

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

4.窗体的布局

(1)在Swing中主要使用5种布局管理器:FlowLayout,BorderLayout
GridLayout,CardLayout,绝对定位。
(2)一般的窗体都默认为FlowLayout布局,如果想取消布局,可以使用以下方法:

btnPanel.setLayout(null);

(3)使用add方法向窗口里添加组件。
(4)设置边界的方法

setBounds(x,y,hight,width)

5.事件处理

(1)对按钮的监听

btn1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				showAdminDialog();
			}
		});

还可以使用键盘去处理:

btn1.addKeyListener(new KeyAdapter(){
			public void keyReleased(KeyEvent e){
				int KeyCode = e.getKeyCode();
				if(KeyCode==KeyEvent.VK_ENTER){
					showAdminDialog();
				}

			}
		});

(2)
addActionListener:在进行某一个操作的时候触发某项功能。

addActionListener:用于接收操作事件的监听器接口。

ActionEvent:按下按钮、在文本框中回车、双击一个列表项或选中某个菜单时发生
(3)加入滚动条

private JTextArea text=new JTextArea();
JScrollPane scr=new JScrollPane(text);

(二)实验总结

1.程序设计思路:在主界面中设置5个按钮,分别是进入系统,登陆,注册,退出,查看用户信息。

当点击进入系统后,宠物的种类信息会显示

当点击注册后

点击查看用户信息,用户的信息会显示出来

问题一:显示购买的宠物
1.首先创建一个宠物数据库

public class shoppingase {
	public static ArrayList<Petshop> data=new ArrayList<Petshop>();
}

2.对购买的宠物编号进行查找

public void search(){
		String addCno = addCnoText.getText();
		boolean searchsuccess=adminService.searchPetItem(addCno);
		if(searchsuccess){
			list.queryPetItem();
			JOptionPane.showMessageDialog(this, "加入成功");
		}
		else{
			JOptionPane.showMessageDialog(this, "加入失败");
		}
	}
public boolean searchPetItem(String cno){
	ArrayList<Petshop> data=queryPetItem();
	for(int i=0;i<data.size();i++){
		Petshop pet=data.get(i);
		if(pet.getCno().equals(cno)){
			Petshop thisPet=new Petshop(pet.getCno(),pet.getKind(),pet.getNumber(),pet.getPrice(),pet.getAge());
			admin.addbuypet(thisPet);//添加成功
			return true;
		}
	}
	return false;
}

3.将所买宠物显示到表中

public void queryPetItem() {
		// 定义表格头
		String[] thead = { "编号", "种类", "数量", "价格", "年龄" };
		// 调用adminService的查询服务
		ArrayList<Petshop> dataList = adminService.searchPetItem();
		// 将查询到的集合转为数组,方便为JTable赋值
		String[][] tbody = listToArray(dataList);
		// 将查询到的结果为table赋值
		TableModel dataModel = new DefaultTableModel(tbody, thead);
		table.setModel(dataModel);
	}
private String[][] listToArray(ArrayList<Petshop> list) {
		String[][] tbody = new String[list.size()][5];
		for (int i = 0; i < list.size(); i++) {
			Petshop pet = list.get(i);
			tbody[i][0] = pet.getCno();
			tbody[i][1] = pet.getKind();
			tbody[i][2] = pet.getNumber() + "";
			tbody[i][3] = pet.getPrice() + "";
			tbody[i][4] = pet.getAge() + "";
		}
		return tbody;

(三)代码托管

[https://git.oschina.net/hebau_cs15/hebau-cs02wzt/tree/master/java作业7?dir=1&filepath=java作业7&oid=1b033d8ca694031e4ce697158260de9112c1d00b&sha=5ffafae36873f43468297f225fd1cafc849b3feb][1]

posted on 2017-05-06 20:02  子腾  阅读(273)  评论(0)    收藏  举报

导航