⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplebutton.java

📁 打印管理程序,测试完全通过.windows开发环境.
💻 JAVA
字号:
/*
    $Author: $
    $Date: $
    $Revision: $
    $NoKeywords: $
*/
package jp.co.ntl.swing.ext;

import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.AWTEventMulticaster;
import java.awt.AWTEvent;
import java.awt.Image;
import java.awt.Font;
import java.awt.FontMetrics;

import javax.swing.JComponent;

import jp.co.ntl.Util;
import jp.co.ntl.awt.AwtUtilities;

public class SimpleButton extends JComponent {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Font font = new Font("Dialog", Font.PLAIN, 12);
	
	private transient ActionListener actionListener;
	private boolean pressed = false;
	private boolean selected = false;
	private String actionCommand;
	private String text;
	private Image image;
	private Image imagePressed;
	private Image imageDisabled;
	
	public SimpleButton() {
		this("");
	}
	
	public SimpleButton(String text) {
		this.text = text;
		enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);	
		setFont(font);
	}
	
	public SimpleButton(Image image, Image imagePressed, Image imageDisabled) {
		this.image = image;
		this.imagePressed = imagePressed;
		this.imageDisabled = imageDisabled;
		enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);	
		setFont(font);
	}
	
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		repaint();
	}
	
	public Dimension getPreferredSize() {
		if (image != null) {
			return new Dimension(image.getWidth(this), image.getHeight(this));
		} else if (text != null){
			Graphics	g = getGraphics();
			FontMetrics	fm = g.getFontMetrics();
			int			totalWidth = 0;
			for (int i = 0; i < text.length(); i++) {
				totalWidth += fm.charWidth(text.charAt(i));
			}
			int			h = fm.getAscent() + fm.getDescent();
			
			return new Dimension(totalWidth, h);
		}
		
		return super.getPreferredSize();
	}
	
	public Dimension getMinimumSize() {
		return getPreferredSize();
	}
	
	public void addActionListener(ActionListener al) {
		if (al == null) {
			return;
		}
		actionListener = AWTEventMulticaster.add(actionListener, al);
	}
	
	public void removeActionListener(ActionListener al) {
		if (al == null) {
			return;
		}
		actionListener = AWTEventMulticaster.remove(actionListener, al);
	}
		
    public void setActionCommand(String actionCommand) {
        this.actionCommand = actionCommand;
    }

    public String getActionCommand() {
		if (actionCommand == null) {
			actionCommand = text;
		}
        return actionCommand;
    }
	
	protected void processEvent(AWTEvent e) {
		if (e instanceof ActionEvent) {
			processActionEvent((ActionEvent)e);
			return;
		}
		
		super.processEvent(e);
	}
	
	protected void processActionEvent(ActionEvent ae) {
		if (actionListener != null) {
			actionListener.actionPerformed(ae);
		}
	}
	
	protected void processMouseEvent(MouseEvent me) {
		if (isEnabled()) {
			switch (me.getID()) {
			case  MouseEvent.MOUSE_PRESSED:
				if (AwtUtilities.isLeftMouseButton(me) && contains(me.getPoint())) {
					pressed = true;
					selected = true;
					repaint();
				}
				break;
			case MouseEvent.MOUSE_RELEASED:
				if (pressed) {
					pressed = false;		
					repaint();
					if (contains(me.getPoint())) {
						processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getActionCommand()));
					}
				}
        		selected = false;
				break;
			}
		}

		super.processMouseEvent(me);
	}

	protected void processMouseMotionEvent(MouseEvent me) {
		if (isEnabled()) {
			if (me.getID() == MouseEvent.MOUSE_DRAGGED) {
				if (contains(me.getPoint())) {
					if (selected && !pressed) {
						pressed = true;
						repaint();
					}
				} else {
					if (selected && pressed) {
						pressed = false;
						repaint();
					}
				}
			}
		}
		
		super.processMouseMotionEvent(me);
	}

	public void update(Graphics g) {
		paint(g);
	}

    private Image backImage = null;
///    private Graphics gImage = null;

	public void paintComponent(Graphics g) {
		Dimension d = getSize();
		if (d.width == 0 || d.height == 0) {
			return;
		}

		if (backImage == null) {
			backImage = createImage(d.width, d.height);
///			gImage = backImage.getGraphics();
		}
		
		if (image != null) {
			paintImage(g);
		} else {
			paintText(g);
		}
	//	g.drawImage(backImage, 0, 0, this);
	}

	private void paintImage(Graphics g) {
///		Dimension d = getSize();
		
		if (pressed) {
			g.drawImage(imagePressed, 0, 0, this);
		} else {
			if (isEnabled()) {
				g.drawImage(image, 0, 0, this);
			} else {
				g.drawImage(imageDisabled, 0, 0, this);
			}
		}
	}
	
	private void paintText(Graphics g) {
		if (font != null) {
			g.setFont(font);
		}
		Dimension d = getSize();
		g.setColor(getBackground());
		if (pressed) {
			g.fillRect(0, 0, d.width, d.height);
			
			g.setColor(Color.white);
			g.drawLine(0, d.height - 1, d.width - 1, d.height - 1);
			g.drawLine(d.width - 1, 0, d.width - 1, d.height - 1);
			
			g.setColor(Color.darkGray);
			g.drawLine(0, 0, 0, d.height - 1);
			g.drawLine(0, 0, d.width - 1, 0);

			g.setColor(Color.gray);
			g.drawLine(1, 1, 1, d.height - 1);
			g.drawLine(1, 1, d.width - 1, 1);

			int x = Util.basePointX(g.getFontMetrics(), text, d.width);
			int y = Util.basePointY(g.getFontMetrics(), text, d.height);
			g.setColor(getForeground());
			g.drawString(text, x + 1, y + 1);

		} else {
			g.fillRect(0, 0, d.width, d.height);
			
			g.setColor(Color.white);
			g.drawLine(0, 0, 0, d.height - 1);
			g.drawLine(0, 0, d.width - 1, 0);
			
			g.setColor(Color.darkGray);
			g.drawLine(1, d.height - 1, d.width - 1, d.height - 1);
			g.drawLine(d.width - 1, 1, d.width - 1, d.height - 1);

			g.setColor(Color.gray);
			g.drawLine(1, d.height - 2, d.width - 2, d.height - 2);
			g.drawLine(d.width - 2, 1, d.width - 2, d.height - 2);

			int x = Util.basePointX(g.getFontMetrics(), text, d.width);
			int y = Util.basePointY(g.getFontMetrics(), text, d.height);
			
			if (isEnabled()) {
				g.setColor(getForeground());
				g.drawString(text, x, y);
			} else {
				g.setColor(Color.white);
				g.drawString(text, x + 1, y + 1);
				g.setColor(getBackground().darker());
				g.drawString(text, x, y);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -