📄 raplaarrowbutton.java
字号:
/*--------------------------------------------------------------------------*
| Copyright (C) 2006 Christopher Kohlhaas |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by the |
| Free Software Foundation. A copy of the license has been included with |
| these distribution in the COPYING file, if not go to www.fsf.org |
| |
| As a special exception, you are granted the permissions to link this |
| program with every library, which license fulfills the Open Source |
| Definition as published by the Open Source Initiative (OSI). |
*--------------------------------------------------------------------------*/
package org.rapla.components.calendar;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
public class RaplaArrowButton extends JButton {
private static final long serialVersionUID = 1L;
ButtonStateChecker m_checker = new ButtonStateChecker();
int m_delay = 0;
boolean m_buttonDown = false;
ArrowPolygon poly;
char c;
public RaplaArrowButton(char c) {
this(c,18);
}
public RaplaArrowButton(char c,int size) {
super();
this.c = c;
setMargin(new Insets(0,0,0,0));
setSize(size,size);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
if (!isEnabled())
return;
m_buttonDown = true;
// repaint();
m_checker.start();
}
/** Implementation-specific. Should be private.*/
public void mouseReleased(MouseEvent me) {
m_buttonDown = false;
// repaint();
}
/** Implementation-specific. Should be private.*/
public void mouseClicked(MouseEvent me) {
m_buttonDown = false;
//repaint();
}
});
}
/** Here you can set if the button should fire repeated clicks. If
set to 0 the button will fire only once when pressed.
*/
public void setClickRepeatDelay(int millis) {
m_delay = millis;
}
public int getClickRepeatDelay() {
return m_delay;
}
/** Set the size of the drop-down button. A drop-down button is square.
The maximum of width and height will be used as new size.
*/
public void setSize(int width,int height) {
int size = Math.max(width,height);
int imageSize = size - 8;
if (imageSize > 0) {
poly = new ArrowPolygon(c,imageSize);
BufferedImage image = new BufferedImage(imageSize,imageSize,BufferedImage.TYPE_INT_ARGB);
Graphics g = image.createGraphics();
g.setColor(new Color(0, 0, 0, 0));
g.fillRect( 0, 0, imageSize, imageSize );
g.setColor( Color.darkGray );
g.fillPolygon( poly );
g.setColor( Color.black );
g.drawPolygon( poly );
setIcon(new ImageIcon(image));
} else {
setIcon(null);
}
super.setSize(size,size);
Dimension dim = new Dimension(size ,size );
setPreferredSize(dim);
setMaximumSize(dim);
setMinimumSize(dim);
}
class ButtonStateChecker implements Runnable{
long startMillis;
long startDelay;
public void start() {
startDelay = m_delay * 10;
startMillis = System.currentTimeMillis();
if (m_delay > 0)
SwingUtilities.invokeLater(this);
}
private void fireAndReset() {
fireActionPerformed(new ActionEvent(this
,ActionEvent.ACTION_PERFORMED
,""));
startMillis = System.currentTimeMillis();
}
public void run() {
if (!m_buttonDown)
return;
if ((Math.abs(System.currentTimeMillis() - startMillis) > startDelay)) {
if (startDelay > m_delay)
startDelay = startDelay/2;
fireAndReset();
}
try {
Thread.sleep(10);
} catch (Exception ex) {
return;
}
SwingUtilities.invokeLater(this);
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -