`
收藏列表
标题 标签 来源
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:aop="http://www.springframework.org/schema/aop"
	xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

</bean>
JTextField 设置背景图
package com.qing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class WatermarkTextField extends JTextField {
	private static final long serialVersionUID = 1L;
	public static final Image LOG = new ImageIcon(
			WatermarkTextField.class.getResource("/resource/images/LOG.png"))
			.getImage();
	private BufferedImage img;
	private TexturePaint texture;

	/**
	 * 构造函数通过传递一张图作为其背景图
	 * 
	 * @param file:图像文件
	 * @throws IOException
	 */
	public WatermarkTextField(File file) throws IOException {
		// 通过文件的方式获取BufferedImage
		img = ImageIO.read(file);
		// 根据指定图像的大小创建一个基准矩形,此矩形和图像大小相等(如果想矩形小一点,,也可绘制部分)
		Rectangle rect = new Rectangle(0, 0, img.getWidth(null),
				img.getHeight(null));
		/** TexturePaint 类提供一种用于被指定为 BufferedImage 的纹理填充 Shape 的方式 **/
		texture = new TexturePaint(img, rect);
		// 使此文本框透明
		setOpaque(false);
	}

	@Override
	public void paintComponent(Graphics g) {
		/** 在调用父类的绘图方法前绘制好背景 **/
		Graphics2D g2 = (Graphics2D) g;
		// 为 Graphics2D 设置 TexturePaint 属性
		g2.setPaint(texture);
		// 获取文本框的的大小,并将其全部填充
		g.fillRect(0, 0, getWidth(), getHeight());
		
		// 然后重用父类的绘图方法,比如绘制文本字符信息等
		super.paintComponent(g);
	}

         //到此已经完成文本框背景图的重绘,下面进行测试
	public static void main(String[] args) throws Exception {
		JFrame frame = new JFrame("Swing Hacks 005:添加背景的文本框");
		frame.setIconImage(LOG);

		JTextField textfield = new WatermarkTextField(new File(
				WatermarkTextField.class
						.getResource("/resource/images/red.png").toURI()));
		textfield.setText(" Thia ia a TextField ");
		textfield.setFont(textfield.getFont().deriveFont(50f));//设置文本字体
		frame.getContentPane().add(textfield, BorderLayout.NORTH);
		frame.pack();
		frame.setVisible(true);
	}
}
JComponent 子类setBorder
public class ImageBorder extends AbstractBorder {

	Image top_center, top_left, top_right;
	Image left_center, right_center;
	Image bottom_center, bottom_left, bottom_right;
	Insets insets;

	public ImageBorder(Image top_left, Image top_center, Image top_right,
			Image left_center, Image right_center, Image bottom_left,
			Image bottom_center, Image bottom_right) {

		this.top_left = top_left;
		this.top_center = top_center;
		this.top_right = top_right;
		this.left_center = left_center;
		this.right_center = right_center;
		this.bottom_left = bottom_left;
		this.bottom_center = bottom_center;
		this.bottom_right = bottom_right;
	}

	public void setInsets(Insets insets) {
		this.insets = insets;
	}

	@Override
	public Insets getBorderInsets(Component c) {
		if (insets != null) {
			return insets;
		} else {
			int top = top_center.getHeight(null);
			int left = left_center.getWidth(null);
			int bottom = left_center.getWidth(null);
			int right = right_center.getHeight(null);
			return new Insets(top, left, bottom, right);
		}
	}

	@SuppressWarnings("unused")
	@Override
	public void paintBorder(Component c, Graphics g, int x, int y, int width,
			int height) {
		//绘制背景色
		g.setColor(Color.white);
		g.fillRect(x, y, width, height);

		Graphics2D g2 = (Graphics2D) g;

		int tlw = top_left.getWidth(null);
		int tlh = top_left.getHeight(null);
		int tcw = top_center.getWidth(null);
		int tch = top_center.getHeight(null);
		int trw = top_right.getWidth(null);
		int trh = top_right.getHeight(null);

		int lcw = left_center.getWidth(null);
		int lch = left_center.getHeight(null);

		int rcw = right_center.getWidth(null);
		int rch = right_center.getHeight(null);

		int blw = bottom_left.getWidth(null);
		int blh = bottom_left.getHeight(null);
		int bcw = bottom_center.getWidth(null);
		int bch = bottom_center.getHeight(null);
		int brw = bottom_right.getWidth(null);
		int brh = bottom_right.getHeight(null);

		fillTexture(g2, top_left, x, y, tlw, tlh);
		fillTexture(g2, top_center, x + tlw, y, width - tlw - trw, tch);
		fillTexture(g2, top_right, x + width - trw, y, trw, trh);

		fillTexture(g2, left_center, x, y + tlh, lcw, height - tlh - blh);
		fillTexture(g2, right_center, x + width - rcw, y + trh, rcw, height
				- trh - brh);

		fillTexture(g2, bottom_left, x, y + height - blh, blw, blh);
		fillTexture(g2, bottom_center, x + blw, y + height - bch, width - blw
				- brw, bch);
		fillTexture(g2, bottom_right, x + width - brw, y + height - brh, brw,
				brh);
	}

	public void fillTexture(Graphics2D g2, Image img, int x, int y, int w, int h) {
		BufferedImage buff = createBufferedImage(img);
		// 根据指定图像的大小创建一个基准矩形
		Rectangle anchor = new Rectangle(x, y, img.getWidth(null),
				img.getHeight(null));
		/** TexturePaint 类提供一种用被指定为 BufferedImage 的纹理填充 Shape 的方式 **/
		// 将buff复制定位到anchor中
		TexturePaint paint = new TexturePaint(buff, anchor);
		// 为 Graphics2D 设置 TexturePaint 属性
		g2.setPaint(paint);
		//根据位置填充矩形
		g2.fillRect(x, y, w, h);
	}

	/** 将Image转化为BufferedImage **/
	public BufferedImage createBufferedImage(Image img) {
		// 创建一个与原图大小相同的缓冲空图像
		// 图像类型为8位ARGB颜色分量(24b全彩色,并带有一个alpha通道)
		BufferedImage buff = new BufferedImage(img.getWidth(null),
				img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		// 创建一个 Graphics2D,可以将它绘制到此 BufferedImage中
		Graphics gfx = buff.createGraphics();
		// 绘制指定图像中当前可用的图像
		gfx.drawImage(img, 0, 0, null);
		// 释放Graphics使用的所有系统资源,释放之后不能再使用 此对象
		gfx.dispose();
		// 返回将Image的缓冲对象
		return buff;
	}
}
/** 调用情况
ImageBorder image_border = new ImageBorder(upper_left, upper,
				upper_right, left_center, right_center, bottom_left,
				bottom_center, bottom_right);
panel.setBorder(image_border);(或者JButton)
*/
JLabel3D文字
public class RichLabel extends JLabel {
	private int tracking;
	private int left_x, left_y, right_x, right_y;
	private Color left_color, right_color;

	/**
	 * 
	 * @param text :需要显示的文本
	 * @param tracking :文本字符间隙大小
	 */
	public RichLabel(String text, int tracking) {
		super(text);
		this.tracking = tracking;
	}

	public void setLeftShadow(int x, int y, Color color) {
		this.left_x = x;
		this.left_y = y;
		this.left_color = color;
	}

	public void setRightShadow(int x, int y, Color color) {
		this.right_x = x;
		this.right_y = y;
		this.right_color = color;
	}

	@Override
	public Dimension getPreferredSize() {
		String text = getText();
		FontMetrics fm = getFontMetrics(getFont());

		int w = fm.stringWidth(text);
		w += (text.length() - 1) * tracking;
		w += left_x + right_x;

		int h = fm.getHeight();
		h += left_y + right_y;

		return new Dimension(w, h);
	}

	@Override
	protected void paintComponent(Graphics g) {
		// 将文本抗锯齿功能打开
		((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		char[] chars = getText().toCharArray();
		// FontMetrics 类含有很多获取当前文本字体高和宽度的方法
		FontMetrics fm = getFontMetrics(getFont());
		// 获取基线到字体顶部的距离
		int h = fm.getAscent();
		int x = 0;

		for (int i = 0; i < chars.length; i++) {
			char ch = chars[i];
			int w = fm.charWidth(ch) + tracking;

			g.setColor(left_color);
			g.drawString("" + chars[i], x - left_x, h - left_y);
			g.setColor(right_color);
			g.drawString("" + chars[i], x + right_x, h + right_y);
			g.setColor(getForeground());
			g.drawString("" + chars[i], x, h);
			x += w;
		}
		((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
	}
}
/*调用此标签
RichLabel label = new RichLabel("2012", 0);
label.setLeftShadow(5, 5, new Color(0xeebbcc));
label.setRightShadow(-3, -3, new Color(0xbacbdc));
label.setForeground(new Color(0X8888ff));
label.setFont(label.getFont().deriveFont(200f));
*/

JButton不同状态显示不同图标
		// 设置图标
		ImageIcon imageIcon = new ImageIcon(
				ImageButton.class.getResource("/resource/images/button.png"));
		Image image = imageIcon.getImage();
		setSize(image.getWidth(null), image.getHeight(null));

		// 设置按钮图标的五种状态
		setIcon(imageIcon);

		image = imageIcon.getImage();
		// 设置按钮的按下图标。
		setPressedIcon(new ImageIcon(
				ImageButton.class
						.getResource("/resource/images/button-down.png")));
		// 设置按钮的翻转图标。
		setRolloverIcon(new ImageIcon(
				ImageButton.class
						.getResource("/resource/images/button-sel.png")));
		// 设置按钮的选择图标。
		setSelectedIcon(new ImageIcon(
				ImageButton.class
						.getResource("/resource/images/button-sel.png")));
		// 设置按钮的翻转选择图标。
		setRolloverSelectedIcon(new ImageIcon(
				ImageButton.class
						.getResource("/resource/images/button-sel-over.png")));

/**调用方法
		// 设置按钮边框和标签之间的空白。边距为0,即内容填满按钮
		setMargin(new Insets(0, 0, 0, 0));
		setIconTextGap(0); // 设置图标与文字间的距离
		setText(null); // 设置按钮文字
		setToolTipText("This is a Button");
		setBorder(null);// 设置不绘制边框
		setBorderPainted(true);// 取消边框绘制
		setOpaque(true);// 绘制组件部分内容
		setContentAreaFilled(false);// 设置背景透明

*/
Global site tag (gtag.js) - Google Analytics