`
IT阿狸
  • 浏览: 65588 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java-AWT

 
阅读更多

一、GUI和AWT

GUI:

1. Graphical User Interfaces,即图形用户界面

2.与命令行界面相比,GUI表现手段丰富,操作简单,大大提高了用户体验

 

AWT:

1.Abstract Window Toolkit,即抽象窗口工具包

2.Sun在JDK1.0中提供的GUI类库

3.AWT没有提供真正的实现,而是调用本地操作系统的图形界面组件,在不同平台上显示效果是不同的

4.为了迎合所有主流OS的界面设计,AWT组件只能使用这些OS图形组件的交集,功能受到限制

 

 

二、AWT组建构成

AWT包含以下内容:

1. 界面组件

Button、Window、Checkbox

2. 布局管理器

FlowLayout、BorderLayout、GridLayout、CardLayout

3. 事件处理模型

a.事件源、事件、事件监听器、事件适配器、事件处理器

b.窗口事件、键盘事件、鼠标事件、Action事件

4.图形和图像工具

Graphics、Image、ImageIO

5.和本地剪贴板操作一起使用的工具

Clipboard、Tranferable、StringSelection

组件: 

1.图形用户界面最小单位,里面不再包含其他成分 

2.一般说来,组件的作用是完成与用户的一次交互 

3.例如按钮、文本框、密码框等

 

容器:

1.容器是用来盛装普通组件

2.容器本身也是一种组件

3.如Window、Frame等

 

 

三、AWT中类层次结构

 

 

四、AWT容器

1.Frame类

a.Window容器类的子类,可以独立存在

b.最常用的容器类之一

 

 

2. Panel类

a.Panel是最常用的容器类之一

b.只能嵌入其他的容器,而不能独立存在

c.可以将其他组件放在Panel提供的空间内

 

 

五、AWT布局管理器

1.布局管理器

a.使用布局管理器负责管理组件在容器中的布局,并非直接设置组件的位置和大小

b.可以保证图形用户界面具有良好的平台无关性

 

2.常用布局管理器

a.FlowLayout

b.BorderLayout

c.GridLayout

d.CardLayout

 

3.FlowLayout

a.自左向右、自上而下

b.自动换行显示组件

c.Panel的默认布局管理器

构造方法 说明

FlowLayout()

构造一个FlowLayout,居中对齐,默认的水平和垂直间隙是 5 个单位

FlowLayout(int align) 构造一个FlowLayout,具有指定的对齐方式,默认水平和垂直间隙5 个单位

FlowLayout(int align, int hgap, int vgap)

创建一个流布局管理器,具有指定的对齐方式以及指定的水平和垂直间隙

 

4. BorderLayout

a.将容器区域划分为上、下、左、右、中 5个区域

b.Window、Frame的默认布局管理器

c.加入组件时需指明所放置的位置,默认为center 

构造方法 说明

BorderLayout()

构造一个组件之间没有间距的新边框布局

BorderLayout(int hgap, int vgap)

构造一个具有指定组件间距的边框布局

 

 5.GridLayout

a.容器被分成m*n大小相等的矩形

b.一个矩形中放置一个组件

c.默认从左到右、从上到下依次添加组件

构造方法 说明

GridLayout()

创建具有默认值的网格布局,即每个组件占据一行一列

GridLayout(int rows, int cols)

创建具有指定行数和列数的网格布局

GridLayout(int rows, int cols, int

hgap, int vgap)

在指定行数、列数、水平间距和垂直间距的情况下,创建网格布局

 

6.CardLayout

a.容器中的每个组件看作一张卡片

b.一次只能看到一张卡片,容器则充当卡片的堆栈

 

 

六、事件处理机制

1.所有事件由事件监听器处理

a.为组件注册(绑定)事件监听器

b.用户操作组件

c.激活一个事件,封装为Event对象

d.触发绑定的事件监听器

e.监听器根据操作调用对应事件处理器进行响应

 

2.关键操作

a.开发事件监听器

 

b.为组件注册(绑定)事件监听器



 

 3.事件源

事件发生的场所,如点击的按钮、菜单项

 

4.事件

a.封装了组件上发生的特定事件,对应Event对象

b.如单击事件、键盘释放事件

 

5.事件监听器

负责监听事件源发生的事件,并对事件进行响应处理

 

6.事件处理器

a.事件监听器的一个方法,由监听器委派来处理某事件

b.事件处理器的参数就是Event对象

 

 

七、事件和监听器对应关系

Event类 监听器接口
ActionEvent ActionListener
AdjustmentEvent AdjustmentListener
ComponentEvent ComponentListener
FocusEvent FocusListener
ItemEvent ItemListener
WindowEvent WindowListener
TextEvent TextListener
MouseEvent MouseListener,MouseMotionListener
KeyEvent KeyListener

 

 

八、事件处理

1.demo

 

public class TestEvent {
	Frame f = new Frame("测试");
	Button btn = new Button("点击我");
	TextField txt = new TextField(30);
	// 定义事件监听器类
	class btnListener implements ActionListener {
		// 开发事件处理器
		public void actionPerformed(ActionEvent e) {
			txt.setText("你好,世界!");
		}
	}

	public void init() {
		// 注册事件监听器
		btn.addActionListener(new btnListener());
		f.add(txt);
		f.add(btn, BorderLayout.SOUTH);
		f.setSize(200, 100);
		f.setVisible(true);
	}

	public static void main(String[] args) {
		TestEvent h = new TestEvent();
		h.init();
	}
}
 

 

 

 2.事件适配器

a.事件适配器是监听器接口的空实现

b.继承事件适配器,直接覆盖指定的方法即可,可以减少开发代码

c.如果某个监听器只有一个方法,则该监听器接口无需提供适配器

d.含多个方法的监听器接口都有一个对应的适配器

 

// 使用事件适配器关闭窗口
public class WindowAdapterDemo extends Frame {
	public WindowAdapterDemo() {
		this.addWindowListener(new WindowDemoListener());
		setSize(200, 150);
		setVisible(true);
		setTitle("测试窗口事件");
	}

	public static void main(String[] args) {
		new WindowAdapterDemo();
	}

	class WindowDemoListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			System.out.println("窗口正在关闭");
			Window w = e.getWindow();
			w.dispose();
		}
	}
}
 

 

3.键盘事件

事件标志 KeyListener方法 触发条件
KEY_PRESSED

keyPressed()

按下按键
KEY_RELEASE keyReleased()  释放按键
KEY_TYPED keyTyped() 键入字符

 

 

九、绘制图形常用类

1.Graphics类

a.一个抽象的画笔对象,支持两种类型的绘图功能

b.绘制图形

c.显示图像

方法 说明
drawLine() 绘制直线

drawString()

绘制字符串

drawRect()

绘制矩形

drawRoundRect()

绘制圆角矩形
drawOval() 绘制椭圆
setColor() 设置画笔的颜色
setFont() 设置画笔的字体

 

2.Component类提供了三个和绘图有关的方法

a.paint() :用来绘制组件的外观

b.update() :调用paint方法,刷新组件外观

c.repaint() :调用update,刷新组件外观

 

3.三个方法的调用关系

a.repaint方法调用update方法,update方法再调用paint方法

b.程序中不应该主动调用paint和update方法,而是由系统负责调用

 

 

十、字体

Font类 

字体类,包含字体名称、风格和点数量

 

public class FontDemo extends Frame {
	public FontDemo() {
		setTitle("测试字体");
		setSize(200, 100);
		setVisible(true);
	}

	public void paint(Graphics g) {
		Font f = new Font("宋体", Font.ITALIC, 16);
		g.setFont(f);
		g.drawString("我叫张三!", 20, 50);
	}

	public static void main(String[] args) {
		new FontDemo();
	}
}

 

 

 

十一、颜色

Color类

a.颜色类

b.提供标准颜色的常量,如Color.RED

c.通过构造方法来生成自定义的颜色 

d.通过setColor()设置颜色

 

public class ColorDemo extends Frame {
	public ColorDemo() {
		setTitle("测试字体");
		setSize(200, 100);
		setVisible(true);
	}

	public void paint(Graphics g) {
		Font f = new Font("宋体", Font.ITALIC, 16);
		g.setColor(Color.RED);
		g.setFont(f);
		g.drawString("我叫张三!", 20, 50);
		g.setColor(new Color(0, 0, 255));
		g.drawString("我叫李四!", 20, 80);
	}

	public static void main(String[] args) {
		new ColorDemo();
	}
}

 

 

 

十二、绘制图形

 

public class DrawsDemo extends Frame {
	public DrawsDemo() {
		setTitle("绘图综合示例");
		setSize(300, 200);
		setVisible(true);
	}

	public void paint(Graphics g) {
		g.drawLine(20, 20, 20, 100);// 画线
		g.drawRect(50, 50, 60, 30); // 绘制矩形
		g.drawRoundRect(50, 100, 80, 80, 20, 20);// 绘制圆角矩形
		g.setColor(Color.red);// 设置颜色
		g.fillRect(120, 50, 60, 30);// 绘制填充矩形
		g.fillRoundRect(180, 100, 80, 80, 20, 20);// 绘制填充圆角矩形
		g.fillOval(10, 100, 80, 30); // 绘制填充椭圆
	}

	public static void main(String[] args) {
		new DrawsDemo();
	}
}

 

 

 

十三、显示图像

1.Toolkit类

a.Toolkit getDefaultToolkit()

b.获取Toolkit对象

c.Image getImage(String filename)

d.返回一个将从文件中读取其像素数据的图像

 

2.Graphices类

a.drawImage(img, x,  y, null)

b.在指定位置(x,y)显示图像img

c.drawImage(img, x, y, width, height, null)d.在指定位置(x,y)显示图像img,图像大小为

(width,height)

 

 

十四、边框布局的demo

package org.e276.awt;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;

/**
 * 边框布局
 * 
 * @author miao
 * 
 */
public class TestBorderLayout {

	public static void main(String[] args) {
		Frame frame = new Frame("测试边框布局");
		// 设置窗体布局
		frame.setLayout(new BorderLayout(10, 5));
		// 在5个区域添加按钮
		frame.add(new Button("North"), BorderLayout.NORTH);
		frame.add(new Button("South"), BorderLayout.SOUTH);
		frame.add(new Button("East"), BorderLayout.EAST);
		frame.add(new Button("West"), BorderLayout.WEST);
		frame.add(new Button("Center"));
		frame.setSize(200, 200);
		frame.setVisible(true);
	}
}

 

 

十五、流式布局的demo

package org.e276.awt;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;

/**
 * 流式布局
 * 
 * @author miao
 * 
 */
public class TestFlowLayout {

	public static void main(String[] args) {
		Frame frame = new Frame("测试流式布局");
		// 设置窗体布局
		frame.setLayout(new FlowLayout(FlowLayout.LEFT));
		// 添加10个按钮
		for (int i = 0; i < 10; i++) {
			frame.add(new Button("按钮" + i));
		}
		frame.setSize(200, 200);
		frame.setVisible(true);
	}
}

 

 

十六、网格布局的demo

package org.e276.awt;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;

/**
 * 网格布局
 * 
 * @author miao
 * 
 */
public class TestGridLayout {

	public static void main(String[] args) {
		Frame frame = new Frame("计算器");
		Panel panel = new Panel();
		String[] btnStr = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/",
				"=" };
		// 创建一个文本,放置在frame的上部
		frame.add(new TextField(50), BorderLayout.NORTH);
		// 3行5列
		panel.setLayout(new GridLayout(3, 5));
		for (int i = 0; i < btnStr.length; i++) {
			panel.add(new Button(btnStr[i]));
		}
		// 加到Center
		frame.add(panel);
		frame.setSize(200, 150);
		frame.setVisible(true);
	}
}

 

 

十七、按键适配器的demo

package org.e276.awt;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 按键适配器 
 * 单击窗口关闭按钮,关闭窗口根据键盘的键入和释放,来动态改变窗体背景色 当键入字符x时,窗体自动退出程序
 * 
 * @author miao
 * 
 */
public class KeyEventDemo {

	public static void main(String[] args) {
		MyFrame myFrame = new MyFrame();
		myFrame.setVisible(true);
	}

}

/**
 * 带按钮面板的窗体
 * 
 * @author miao
 * 
 */
class MyFrame extends Frame {
	public MyFrame() {
		setTitle("测试键盘事件");
		setSize(300, 200);
		MyPanel panel = new MyPanel();
		add(panel);
		addWindowListener(new MyWindowAdapter());
	}

	private class MyWindowAdapter extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			dispose();
			System.exit(0);
		}
	}
}

/**
 * 带一个按钮的面板
 * 
 * @author miao
 * 
 */
class MyPanel extends Panel {
	public MyPanel() {
		// 创建文本域
		TextField textField = new TextField(20);
		// 将按钮添加至面板
		add(textField);
		MyKeyAdapter keyAction = new MyKeyAdapter();
		// 将监听器对象添加至文本域
		textField.addKeyListener(keyAction);
	}

	/**
	 * 覆盖KeyAdapter类的keyPressed、keyReleased、keyTyped方法
	 */
	private class MyKeyAdapter extends KeyAdapter {

		public void keyTyped(KeyEvent e) {
			// 判断字符用 ==
			if (e.getKeyChar() == 'x') {
				System.exit(0);
			}
		}

		public void keyPressed(KeyEvent e) {
			setBackground(Color.red);
			repaint();
		}

		public void keyReleased(KeyEvent e) {
			setBackground(Color.blue);
			repaint();
		}

	}
}

 

 

十八、一个使用AWT制作的简单例子,实现对数据库的增删改查。

这里使用了Spring-JDBC作为数据库持久层

 

1.entity类

package org.nobel.entity;

/**
 * 实体类
 * 
 * @author miao
 * 
 */
public class Nobel {

	private String id;
	private String time;
	private String subject;
	private String author;
	private String works;
	private String country;

	public Nobel() {
		super();
	}

	public Nobel(String id, String time, String subject, String author, String works, String country) {
		super();
		this.id = id;
		this.time = time;
		this.subject = subject;
		this.author = author;
		this.works = works;
		this.country = country;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getWorks() {
		return works;
	}

	public void setWorks(String works) {
		this.works = works;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}
}

 

2.mapper类(实现RowMapper接口)

package org.nobel.mapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.nobel.entity.Nobel;
import org.springframework.jdbc.core.RowMapper;

/**
 * 实现RowMapper接口
 * 
 * @author miao
 * 
 */
public class NobelMapper implements RowMapper<Nobel> {

	@Override
	public Nobel mapRow(ResultSet rs, int rowNum) throws SQLException {
		Nobel nobel = new Nobel();
		// 数据库的列名
		nobel.setId(rs.getString("id"));
		nobel.setTime(rs.getString("ttime"));
		nobel.setSubject(rs.getString("subject"));
		nobel.setAuthor(rs.getString("author"));
		nobel.setWorks(rs.getString("works"));
		nobel.setCountry(rs.getString("country"));
		return nobel;
	}

}

 

3.dao

package org.nobel.dao;

import org.nobel.entity.Nobel;

/**
 * 诺贝尔奖的DAO类
 * 
 * @author miao
 * 
 */
public interface NobelDao {

	/**
	 * 添加诺贝尔奖
	 * 
	 * @param no
	 * @return
	 */
	public int add(Nobel no);

	/**
	 * 通过ID删除
	 * 
	 * @param id
	 * @return
	 */
	public int del(String id);

	/**
	 * 修改
	 * 
	 * @param no
	 * @return
	 */
	public int modify(Nobel no);

	/**
	 * 通过ID删除
	 * 
	 * @param no
	 * @return
	 */
	public Nobel query(String id);
}

 

4.daoImpl

package org.nobel.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.nobel.dao.NobelDao;
import org.nobel.entity.Nobel;
import org.nobel.mapper.NobelMapper;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;

/**
 * 诺贝尔奖的DAO实现类
 * 
 * @author miao
 * 
 */
public class NobelDaoImpl extends NamedParameterJdbcDaoSupport implements NobelDao {

	@Override
	public int add(final Nobel no) {
		String sql = "insert into nobel(id, author, subject, works, ttime, country) values(?, ?, ?, ?, ?, ?)";
		return super.getJdbcTemplate().update(sql, new PreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				Nobel nobel = no;
				ps.setString(1, nobel.getId());
				ps.setString(2, nobel.getAuthor());
				ps.setString(3, nobel.getSubject());
				ps.setString(4, nobel.getWorks());
				ps.setString(5, nobel.getTime());
				ps.setString(6, nobel.getCountry());
			}
		});
	}

	@Override
	public int del(String id) {
		return super.getJdbcTemplate().update("delete nobel where id = ?", id);
	}

	@Override
	public int modify(Nobel no) {
		return super
				.getNamedParameterJdbcTemplate()
				.update("update nobel set author = :author,subject = :subject,works = :works,ttime = :time,country = :country where id = :id",
						new BeanPropertySqlParameterSource(no));
	}

	@Override
	public Nobel query(String id) {
		return super.getJdbcTemplate().queryForObject(
				"select id, author, subject, works, ttime, country from nobel where id=?",
				new NobelMapper(), id);
	}

}

 

5.ui(表示层)

package org.nobel.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import org.nobel.dao.NobelDao;
import org.nobel.dao.impl.NobelDaoImpl;
import org.nobel.entity.Nobel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 表示层
 * 
 * @author miao
 * 
 */
public class NobelGui implements ActionListener {

	private JFrame jFrame;

	private JPanel jContentPane;

	private JLabel lblId = null;

	private JTextField tfId = null;

	private JLabel lblAuthor = null;

	private JTextField tfAuthor = null;

	private JLabel lblsubject = null;

	private JComboBox cbsubject = null;

	private JLabel lblworks = null;

	private JTextField tfworks = null;

	private JLabel lbltime = null;

	private JTextField tftime = null;

	private JLabel lblcountry = null;

	private JTextField tfcountry = null;

	private JButton btnadd = null;

	private JButton btndel = null;

	private JButton btnmodify = null;

	private JButton btnquery = null;

	private static NobelDao nobelDao;

	// set注入
	@SuppressWarnings("static-access")
	public void setNobelDao(NobelDao nobelDao) {
		this.nobelDao = nobelDao;
	}

	/*
	 * 得到编号的对象,并设置大小
	 */
	private JTextField getTfId() {
		if (tfId == null) {
			tfId = new JTextField();
			tfId.setBounds(new Rectangle(115, 36, 197, 25));
		}
		return tfId;
	}

	/*
	 * 得到作者对象
	 */
	private JTextField getTfAuthor() {
		if (tfAuthor == null) {
			tfAuthor = new JTextField();
			tfAuthor.setBounds(new Rectangle(115, 75, 197, 25));
		}
		return tfAuthor;
	}

	/*
	 * 得到下拉列表
	 */
	private JComboBox getCbsubject() {
		if (cbsubject == null) {
			String[] str = { "和平奖", "物理奖", "文学奖", "化学奖", "生物奖", "经济奖" };
			cbsubject = new JComboBox(str);
			cbsubject.setBounds(new Rectangle(115, 115, 197, 25));
		}
		return cbsubject;
	}

	/*
	 * 得到获奖作品
	 */
	private JTextField getTfworks() {
		if (tfworks == null) {
			tfworks = new JTextField();
			tfworks.setBounds(new Rectangle(115, 151, 197, 25));
			tfworks.setText("");
		}
		return tfworks;
	}

	/*
	 * 得到获奖时间
	 */
	private JTextField getTftime() {
		if (tftime == null) {
			tftime = new JTextField();
			tftime.setBounds(new Rectangle(115, 190, 197, 25));
		}
		return tftime;
	}

	/*
	 * 得到国家
	 */
	private JTextField getTfcountry() {
		if (tfcountry == null) {
			tfcountry = new JTextField();
			tfcountry.setBounds(new Rectangle(115, 227, 197, 25));
		}
		return tfcountry;
	}

	// 添加按钮
	private JButton getBtnadd() {
		if (btnadd == null) {
			btnadd = new JButton();
			btnadd.setBounds(new Rectangle(36, 282, 64, 28));
			// 实现了相应的接口
			btnadd.addActionListener(this);
			btnadd.setText("添加");
		}
		return btnadd;
	}

	// 删除按钮
	private JButton getBtndel() {
		if (btndel == null) {
			btndel = new JButton();
			btndel.setBounds(new Rectangle(108, 282, 64, 28));
			// 实现了相应的接口
			btndel.addActionListener(this);
			btndel.setText("删除");
		}
		return btndel;
	}

	// 修改按钮
	private JButton getBtnmodify() {
		if (btnmodify == null) {
			btnmodify = new JButton();
			btnmodify.setBounds(new Rectangle(180, 282, 64, 28));
			// 实现了相应的接口
			btnmodify.addActionListener(this);
			btnmodify.setText("修改");
		}
		return btnmodify;
	}

	// 查找按钮
	private JButton getBtnquery() {
		if (btnquery == null) {
			btnquery = new JButton();
			btnquery.setBounds(new Rectangle(254, 282, 64, 28));
			btnquery.addActionListener(this);
			btnquery.setText("查找");
		}
		return btnquery;
	}

	/**
	 * 设置窗体的属性
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			// 设置关闭
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			// 不能改变大小
			jFrame.setResizable(false);
			// 窗体大小
			jFrame.setSize(353, 375);
			// 调用自己的方法窗体剧中,要在setSize之后
			setCenter(jFrame);
			// 设置内容面板
			jFrame.setContentPane(getJContentPane());
			// 设置标题
			jFrame.setTitle("诺贝尔信息管理");
		}
		return jFrame;
	}

	// 得到内容面板对象
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			lblcountry = new JLabel();
			lblcountry.setBounds(new Rectangle(37, 228, 72, 25));
			lblcountry.setText("获奖人国家");
			lbltime = new JLabel();
			lbltime.setBounds(new Rectangle(37, 193, 61, 25));
			lbltime.setText("获奖时间");
			lblworks = new JLabel();
			lblworks.setBounds(new Rectangle(37, 152, 61, 25));
			lblworks.setText("获奖作品");
			lblsubject = new JLabel();
			lblsubject.setBounds(new Rectangle(37, 114, 61, 25));
			lblsubject.setText("获奖科目");
			lblAuthor = new JLabel();
			lblAuthor.setText("获奖人");
			lblAuthor.setBounds(new Rectangle(37, 75, 61, 25));
			lblId = new JLabel();
			lblId.setBounds(new Rectangle(37, 36, 61, 25));
			lblId.setPreferredSize(new Dimension(52, 20));
			lblId.setText("证书编号");
			// 得到面板对象,并向面板对象中加入上面的对象
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(Color.lightGray);
			jContentPane.add(lblId, null);
			jContentPane.add(getTfId(), null);
			jContentPane.add(lblAuthor, null);
			jContentPane.add(getTfAuthor(), null);
			jContentPane.add(lblsubject, null);
			jContentPane.add(getCbsubject(), null);
			jContentPane.add(lblworks, null);
			jContentPane.add(getTfworks(), null);
			jContentPane.add(lbltime, null);
			jContentPane.add(getTftime(), null);
			jContentPane.add(lblcountry, null);
			jContentPane.add(getTfcountry(), null);
			jContentPane.add(getBtnadd(), null);
			jContentPane.add(getBtndel(), null);
			jContentPane.add(getBtnmodify(), null);
			jContentPane.add(getBtnquery(), null);
		}
		return jContentPane;
	}

	/*
	 * 按钮点击事件
	 */
	public void actionPerformed(ActionEvent e) {
		// 添加按钮
		if (e.getActionCommand().equals("添加")) {
			if (tfId.getText().trim().equals("") || tftime.getText().trim().equals("")
					|| tfAuthor.getText().trim().equals("") || tfworks.getText().trim().equals("")
					|| tfcountry.getText().trim().equals("")) {
				// 显示信息
				JOptionPane.showMessageDialog(jFrame, "请添写相应的信息");
			} else {
				Nobel no = new Nobel();
				no.setId(tfId.getText());
				no.setAuthor(tfAuthor.getText());
				no.setSubject((String) cbsubject.getSelectedItem());
				no.setWorks(tfworks.getText());
				no.setCountry(tfcountry.getText());
				no.setTime(tftime.getText());
				int count = nobelDao.add(no);
				if (count > 0) {
					JOptionPane.showMessageDialog(jFrame, "添加成功");
				} else {
					JOptionPane.showMessageDialog(jFrame, "添加失败");
				}
			}
			// 删除按钮
		} else if (e.getActionCommand().equals("删除")) {
			if (tfId.getText().trim().equals("")) {
				JOptionPane.showMessageDialog(jFrame, "请选择一条记录");
			} else {
				String id = tfId.getText();
				int count = nobelDao.del(id);
				if (count > 0) {
					JOptionPane.showMessageDialog(jFrame, "删除成功");
					tfId.setText("");
					tfAuthor.setText("");
					tfcountry.setText("");
					tfworks.setText("");
				} else {
					JOptionPane.showMessageDialog(jFrame, "删除失败");
				}
			}
			// 修改按钮
		} else if (e.getActionCommand().equals("修改")) {
			if (tfId.getText().trim().equals("") || tftime.getText().trim().equals("")
					|| tfAuthor.getText().trim().equals("") || tfworks.getText().trim().equals("")
					|| tfcountry.getText().trim().equals("")) {
				JOptionPane.showMessageDialog(jFrame, "请添写相应的信息");
			} else {
				Nobel no = new Nobel();
				no.setId(tfId.getText());
				no.setAuthor(tfAuthor.getText());
				no.setSubject((String) cbsubject.getSelectedItem());
				no.setWorks(tfworks.getText());
				no.setCountry(tfcountry.getText());
				no.setTime(tftime.getText());
				int count = nobelDao.modify(no);
				if (count > 0) {
					JOptionPane.showMessageDialog(jFrame, "修改成功");
				} else {
					JOptionPane.showMessageDialog(jFrame, "修改失败");
				}
			}
			// 查找按钮
		} else if (e.getActionCommand().equals("查找")) {
			if (tfId.getText().trim().equals("")) {
				JOptionPane.showMessageDialog(jFrame, "请选择一条记录");
			} else {
				String id = tfId.getText();
				Nobel no = nobelDao.query(id);
				tfAuthor.setText(no.getAuthor());
				tftime.setText(no.getTime());
				tfworks.setText(no.getWorks());
				tfcountry.setText(no.getCountry());
				cbsubject.setSelectedItem(no.getSubject());
			}
		}

	}

	/**
	 * 默认将窗体放在屏幕中心
	 */
	private void setCenter(JFrame frame) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		frame.setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
	}

	/**
	 * 程序入口
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		nobelDao = context.getBean(NobelDaoImpl.class);
		// Swing的工具类,后台异步运行这个类
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				NobelGui application = new NobelGui();
				application.getJFrame().setVisible(true);
			}
		});
	}

}

 

 6.Spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	
	<!-- 数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
		</property>
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl">
		</property>
		<property name="username" value="y2" />
		<property name="password" value="bdqn" />
	</bean>
	
	<!-- jdbcTemplate -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
		<!-- 以下属性可选 -->
		<property name="queryTimeout" value="10" />
		<property name="maxRows" value="100" />
	</bean>
	
	<!-- dao -->
	<bean id="nobelDao" class="org.nobel.dao.impl.NobelDaoImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean>
	
	<!-- gui -->
	<bean id="application" class="org.nobel.ui.NobelGui">
		<property name="nobelDao" ref="nobelDao" />
	</bean>
	
</beans>

 

 

十九、demos

E276-AWT.zip

AWT-Nobel.zip

  • 大小: 30.5 KB
  • 大小: 49 KB
  • 大小: 8.8 KB
  • 大小: 10.1 KB
  • 大小: 8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics