📄 rotatebutton.java
字号:
/*** $Id: RotateButton.java,v 1.2 2001/05/07 12:34:45 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998-2000 by Timothy Gerard Endres** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE.*/package org.icemail.util;import java.awt.Image;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.lang.IllegalArgumentException;import java.util.Vector;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JButton;/** * RotateButton adds additional functionality to the normal JButton by * allowing an application to associate a series of icons and commands to * the button. * <p> * As the button is activated, the icon (shown) and pressed icon (underlying) * are rotated, as well as the associated command. This allows the button to * be "rotated" through a series of icons and associated commands, providing * a multiple state button for applications. */public class RotateButton extends JButton{ private Vector entries_ = new Vector( 2 ); private int current_ = 0; /** * Contruct a button. * Additional icons must be added to make the button useful, see addIcon(). * <p> * Of course, the JButton methods are also usable. */ public RotateButton( ) { super(); } /** * Return the current index of the button. */ public int current() { return current_; } /** * Add an icon using the given path and associated settings to the button. * The path and command are required parameters, whereas the text and tip * are optional. * <p> * Adding icons inconsistantly may producing undesired results, so always * provide text and tip when using these optional parameters. * <p> * The command should reflect the MOVEMENT to the next state, not the current state * because the button is going to be pushed, firing an event with the associated * command. * * @param path a path to an icon image * @param command associated command which reflects the MOVEMENT to the next state * @param text associated text or null * @param tip associated tool tip message or null */ public void addIcon( String path, String command, String text, String tip ) { if ( path == null || command == null ) { throw new IllegalArgumentException( "Invalid argument to RotateButton.addIcon()" ); } // add a new entry Image ximage = Toolkit.getDefaultToolkit().getImage( path ); Entry xentry = new Entry( new ImageIcon( ximage ), command, text, tip ); entries_.addElement( xentry ); switch ( entries_.size() ) { case 1: case 2: // rotate special cases to first position rotate( 0 ); break; } } /** * A convenience method for applications. * * @param path a path of an icon image to add to the rotatation list * @param command associated command which reflects the MOVEMENT to the next state */ public void addIcon( String path, String command ) { addIcon( path, command, null, null ); } /** * Add an icon and associated settings to the button. * The icon and command are required parameters, whereas the text and tip * are optional. * <p> * Adding icons inconsistantly may producing undesired results, so always * provide text and tip when using these optional parameters. * <p> * The command should reflect the MOVEMENT to the next state, not the current state * because the button is going to be pushed, firing an event with the associated * command. * * @param icon an icon to add to the rotatation list * @param command associated command which reflects the MOVEMENT to the next state * @param text optional associated text * @param tip optional associated tool tip message */ public void addIcon( Icon icon, String command, String text, String tip ) { if ( icon == null || command == null ) { throw new IllegalArgumentException( "Invalid argument to RotateButton.addIcon()" ); } // add a new entry Entry xentry = new Entry( icon, command, text, tip ); entries_.addElement( xentry ); switch ( entries_.size() ) { case 1: case 2: // rotate special cases to first position rotate( 0 ); break; } } /** * A convenience method for applications. * * @param icon an icon to add to the rotatation list * @param command associated command which reflects the MOVEMENT to the next state */ public void addIcon( Icon icon, String command ) { addIcon( icon, command, null, null ); } /** * Rotate the button to the given position. * The routine can be called manually to rotate the button to an initial starting * position, however it should be called after adding all icons. * * @param next index of next rotation */ public void rotate( int next ) { // check for error conditions int xsize = entries_.size(); if ( xsize == 0 ) return; // set the next position current_ = next; if ( current_ < 0 || current_ >= xsize ) current_ = 0; // set current icon Entry xentry = (Entry)entries_.elementAt( current_ ); setIcon( xentry.icon_ ); // set current text if ( xentry.text_ != null ) { setText( xentry.text_ ); } // set current tip if ( xentry.tip_ != null ) { setToolTipText( xentry.tip_ ); } // set next icon if ( ( current_ + 1 ) == xsize ) { setPressedIcon( ((Entry)entries_.firstElement()).icon_ ); } else { setPressedIcon( ((Entry)entries_.elementAt( current_+1 )).icon_ ); } // set the command super.setActionCommand( ((Entry)entries_.elementAt( current_ )).command_ ); } /** * This routine is called whenever the button is pushed. * Overloaded from javax.swing.AbstractButton. * The event is intercepted, the button is rotated automatically, and * the event is passed on to listener objects. * * @param event description * @see javax.swing.AbstractButton */ protected void fireActionPerformed( ActionEvent event ) { rotate( current_ + 1 ); super.fireActionPerformed( event ); } /** * Internal storage class for icon and command pairs. */ private class Entry { Icon icon_; String command_; String text_; String tip_; Entry( Icon icon, String command, String text, String tip ) { icon_ = icon; command_ = command; text_ = text; tip_ = tip; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -