📄 spinner.java
字号:
package edu.odu.cs.zeil.AlgAE.gui;import java.awt.Button;import java.awt.Checkbox;import java.awt.Color;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.Label;import java.awt.Panel;import java.awt.TextField;import java.awt.event.*;import java.util.Vector;import java.lang.String;import edu.odu.cs.zeil.AlgAE.gui.OutlinedPanel;public class Spinner extends OutlinedPanel{ private TextField valueText; private int value; private int oldValue; private int minValue; private int maxValue; private Vector listeners; private void sanityCheck() { value = Math.min(maxValue, Math.max(minValue, value)); } public Spinner (int initialValue, int minvalue, int maxvalue) { listeners = new Vector(); minValue = minvalue; maxValue = maxvalue; oldValue = initialValue; value = initialValue; sanityCheck(); Button increaseValueButton = new Button("+"); Button decreaseValueButton = new Button("-"); valueText = new TextField(String.valueOf(value), 2); setLayout (new FlowLayout()); add (increaseValueButton); add (valueText); add (decreaseValueButton); increaseValueButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ++value; sanityCheck(); valueText.setText (String.valueOf(value)); }}); decreaseValueButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { --value; sanityCheck(); valueText.setText (String.valueOf(value)); }}); valueText.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = valueText.getText(); try { value = Integer.valueOf(text).intValue(); } catch (NumberFormatException except) { value = oldValue; } sanityCheck(); }}); addMouseListener(new MouseAdapter() { public void mouseExited(MouseEvent e) { if (value != oldValue) { oldValue = value; for (int i = 0; i < listeners.size(); ++i) { ActionListener a = (ActionListener)listeners.elementAt(i); a.actionPerformed (new ActionEvent(Spinner.this, ActionEvent.ACTION_PERFORMED, "spinner value changed")); } } } }); } /** * Get the value of value. * @return Value of value. */ public int getValue() {return value;} /** * Set the value of value. * @param v Value to assign to value. */ public void setValue(int v) {value = v;} public void addActionListener (ActionListener l) { listeners.addElement (l); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -