?? spinnumber.java
字號:
/*
$Author: $
$Date: $
$Revision: $
$NoKeywords: $
*/
package jp.co.ntl.awt;
import java.awt.Panel;//sContainer;
import java.awt.Point;
import java.awt.event.*;
import jp.co.ntl.awt.event.ChangeEvent;
import jp.co.ntl.awt.event.ChangeListener;
import jp.co.ntl.awt.event.ChangeSupport;
public class SpinNumber extends Panel//Container
implements MouseListener, MouseMotionListener, KeyListener, TextListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int BUTTON_WIDTH = 13;
NumberField numField;
ArrowButton btnUp;
ArrowButton btnDown;
UpDownThread thread = null;
private float step = 1; // 1 or 0.1
private ChangeSupport support = new ChangeSupport();
public SpinNumber(int figure, int type, float minimum, float maximum) {
setLayout(null);
numField = new NumberField(figure, type, minimum, maximum);
add(numField);
btnUp = new ArrowButton(ArrowButton.TOP);
add(btnUp);
btnDown = new ArrowButton(ArrowButton.BOTTOM);
add(btnDown);
numField.addKeyListener(this);
numField.addTextListener(this);
btnUp.addMouseListener(this);
btnUp.addMouseMotionListener(this);
btnDown.addMouseListener(this);
btnDown.addMouseMotionListener(this);
}
public void setStep(float step) {
this.step = step;
}
public void setBounds(int x, int y, int width, int height) {
super.setBounds(x, y, width, height);
numField.setBounds(0, 0, width - BUTTON_WIDTH, height);
int temp = height / 2;
btnUp.setBounds(width - BUTTON_WIDTH, 0, BUTTON_WIDTH, temp);
btnDown.setBounds(width - BUTTON_WIDTH, temp, BUTTON_WIDTH, height - temp);
}
public void setEnabled(boolean b) {
super.setEnabled(b);
numField.setEnabled(b);
btnUp.setEnabled(b);
btnDown.setEnabled(b);
}
public void setValue(float value) {
numField.setValue(value);
}
public void setValue(int value) {
numField.setValue(value);
}
public int getIntValue() {
return numField.getIntValue();
}
public float getFloatValue() {
return numField.getFloatValue();
}
public void setText(String text) {
numField.setText(text);
}
public void addChangeListener(ChangeListener cl) {
support.addChangeListener(cl);
}
public void removeChangeListener(ChangeListener cl) {
support.removeChangeListener(cl);
}
public void keyTyped(KeyEvent ke) { /*support.fireChangeEvent(new ChangeEvent(this));*/ }
public void keyPressed(KeyEvent ke) { }
public void keyReleased(KeyEvent ke) {}
public void textValueChanged(TextEvent te) { support.fireChangeEvent(new ChangeEvent(this)); }
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mousePressed(MouseEvent me) {
Object source = me.getSource();
if (source == btnUp) {
startThread(true);
}
else if (source == btnDown) {
startThread(false);
}
}
public void mouseReleased(MouseEvent me) {
stopThread();
}
public void mouseDragged(MouseEvent me) {
Point pt = me.getPoint();
if (thread != null) {
if ((thread.isUp() && !btnUp.contains(pt)) || (!thread.isUp() && !btnDown.contains(pt))) {
stopThread();
}
}
}
public void mouseMoved(MouseEvent me) {
}
void startThread(boolean up) {
if (thread == null) {
thread = new UpDownThread(up);
thread.start();
}
}
void stopThread() {
if (thread != null) {
thread.setThreadStop();
thread = null;
}
}
class UpDownThread extends Thread {
private boolean threadStop = false;
private boolean up;
private int interval = 300;
UpDownThread(boolean up) {
this.up = up;
}
boolean isUp() {
return up;
}
void setThreadStop() {
threadStop = true;
}
float value;
public void run() {
while (!threadStop) {
try {
value = numField.getFloatValue();
}
catch (NumberFormatException e) {
break;
}
if (interval > 60) {
interval -= 20;
}
float min = numField.getMinimum();
float max = numField.getMaximum();;
if (value != min && value != max) {
if (value < min) {
value = min;
} else if (value > max) {
value = max;
} else {
if (up) {
if (value != max) {
value += step;
if (step != 1) {
value = (int)((value + 0.05f) * 10) / 10.0f;
}
if (value > max) {
value = max;
}
}
} else {
if (value != min) {
value -= step;
if (step != 1) {
value = (int)((value + 0.05f) * 10) / 10.0f;
}
if (value < min) {
value = min;
}
}
}
}
numField.setValue(value);
support.fireChangeEvent(new ChangeEvent(SpinNumber.this));
} else if (value == min) {
if (up) {
value += step;
if (step != 1) {
value = (int)((value + 0.05f) * 10) / 10.0f;
}
if (value > max) {
value = max;
}
numField.setValue(value);
support.fireChangeEvent(new ChangeEvent(SpinNumber.this));
}
} else if (value == max) {
if (!up) {
value -= step;
if (step != 1) {
value = (int)((value + 0.05f) * 10) / 10.0f;
}
if (value < min) {
value = min;
}
numField.setValue(value);
support.fireChangeEvent(new ChangeEvent(SpinNumber.this));
}
}
try {
sleep(interval);
}
catch (Exception e) {
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -