floatingbutton.java
字号:
package piy;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.AbstractButton;
/**
* A button that doesn't have a border until the mouse moves over it.
* @author David Vivash
* @version 1.0, 25/11/00
*/
public class FloatingButton extends AbstractButton implements MouseListener, MouseMotionListener
{
protected static boolean dragged = false;
private Image image;
private boolean down = false;
private boolean over = false;
private String commandString = "";
private int halfImageWidth=0, halfImageHeight=0;
private boolean selectable = false;
private boolean selected = false;
private ArrayList listeners = new ArrayList(1);
/**
* Creates a floating button that will highlight itself with a surrounding box
* when the mouse moves over it.
* @param image the image on the button
* @param highlight the highlight color of the surrounding box
* @param command the command string to send to the action listener of this button
*/
public FloatingButton(Image image, String command, boolean selectable) {
this.image = image;
this.commandString = command;
halfImageWidth = image.getWidth(this)/2;
halfImageHeight = image.getHeight(this)/2;
addMouseListener(this);
addMouseMotionListener(this);
this.selectable = selectable;
setSelected(false);
}
public void paint(Graphics g) {
g.setColor(getBackground());
if (down && over) {
g.draw3DRect(0,0,getSize().width-1,getSize().height-1, false);
g.drawImage(image, getWidth()/2 - halfImageWidth + 1, getHeight()/2 - halfImageHeight + 1, this);
} else if ((over && !down) && !isSelected()) {
g.draw3DRect(0,0,getSize().width-1,getSize().height-1, true);
g.drawImage(image, getWidth()/2 - halfImageWidth, getHeight()/2 - halfImageHeight, this);
} else if (!isSelected()) {
g.drawImage(image, getWidth()/2 - halfImageWidth, getHeight()/2 - halfImageHeight, this);
} else {
g.draw3DRect(0,0,getSize().width-1,getSize().height-1, false);
g.drawImage(image, getWidth()/2 - halfImageWidth, getHeight()/2 - halfImageHeight, this);
}
}
public boolean isSelected() { return selected; }
public void setSelected(boolean val) {
if (selected != val) {
selected = val;
repaint();
}
}
/**
* Adds a new action listener for the button.
* @param actionListener the new ActionListener
*/
public void addActionListener(ActionListener actionListener)
{
listeners.add(actionListener);
}
/**
* Sets the command string for this button. When the button is clicked, the
* action listener is informed and is told this command string.
* @param commandString the command string to notify to the action listener of
* when this button is clicked
*/
public void setActionCommand(String commandString) {
this.commandString = commandString;
}
//This is an extra identifier number when more than the command string is needed
private int identifier = 0;
public void setIdentifier(int id) { identifier = id; }
public int getIdentifier() { return identifier; }
//--- Mouse listener methods ---
public void mouseClicked(MouseEvent e) { /*this is caught in mousePressed/mouseReleased pair*/}
public void mousePressed(MouseEvent e) {
down = true; repaint();
}
public void mouseReleased(MouseEvent e) {
down = false; FloatingButton.dragged = false; repaint();
if (over) {
if (selectable) setSelected(!isSelected());
notifyListeners();
}
}
public void mouseEntered(MouseEvent e) {
if (!FloatingButton.dragged) {
over = true; repaint();
}
}
public void mouseExited(MouseEvent e) {
over = false; repaint();
}
//--- Mouse motion listener methods ---
public void mouseDragged(MouseEvent e) {
FloatingButton.dragged = true;
int x = (int)e.getX();
int y = (int)e.getY();
boolean newOver = (x>=0) && (x<=getSize().width) && (y>=0) && (y<=getSize().height);
if (over != newOver) repaint();
over = newOver;
}
public void mouseMoved(MouseEvent e) { }
/**
* Notifies all action listeners that this buton has been clicked
*/
private void notifyListeners()
{
for (int i=0; i<listeners.size(); i++)
((ActionListener)listeners.get(i)).actionPerformed(new ActionEvent(this, 0, commandString));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -