|
JIDE 3.5.15 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.JPanel com.jidesoft.swing.Calculator
public class Calculator
Calculator is a component that can do simple arithmetic calculation. Since it extends JPanel, you can use it at any place in your application.
To make it more flexible, the Calculator has no text field to display the result. You can create your own JTextField or JLabel to display the result. Here is a simple example to create a text field and associate it with Calculator.
final JTextField textField = new JTextField();
textField.setColumns(20);
textField.setHorizontalAlignment(JTextField.TRAILING);
Calculator calculator = new Calculator();
calculator.registerKeyboardActions(textField, JComponent.WHEN_FOCUSED);
calculator.addPropertyChangeListener(Calculator.PROPERTY_DISPLAY_TEXT, new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
textField.setText("" + evt.getNewValue());
}
});
calculator.clear();
With the code above, user can type in directly into text field and do the calculation. If you just want to display
the result and don't mind if the text field accepts keyboard input, you don't need to call registerKeyboardActions
method.
All numeric and operator keys work as expected. Here are a few special keys that worth mentioning
Calculator calculator = new Calculator();
calculator.input('1');
calculator.input('0');
calculator.input('*');
calculator.input('2');
calculator.input('4');
calculator.input('=');
System.out.println("10 * 24 = " + calculator.getDisplayText());
The print out will be "10 * 24 = 240".
There are several methods you can use to get internal state of the Calculator. getDisplayText()
:
to get the result that should be displayed. Please note, this method return a string. getResult()
: to
get the last calculated result. This method returns a double value. getOperator()
: to get the current
operator isOverflow()
: to check if there is an overflow. Usually if you try to divide by zero, you will
get an overflow.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class javax.swing.JPanel |
---|
JPanel.AccessibleJPanel |
Nested classes/interfaces inherited from class javax.swing.JComponent |
---|
JComponent.AccessibleJComponent |
Nested classes/interfaces inherited from class java.awt.Container |
---|
Container.AccessibleAWTContainer |
Nested classes/interfaces inherited from class java.awt.Component |
---|
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy |
Field Summary | |
---|---|
static char |
CHAR_0
|
static char |
CHAR_1
|
static char |
CHAR_2
|
static char |
CHAR_3
|
static char |
CHAR_4
|
static char |
CHAR_5
|
static char |
CHAR_6
|
static char |
CHAR_7
|
static char |
CHAR_8
|
static char |
CHAR_9
|
static char |
CHAR_ADD
|
static char |
CHAR_BACKSPACE
|
static char |
CHAR_CLEAR
|
static char |
CHAR_DIVIDE
|
static char |
CHAR_EQUAL
|
static char |
CHAR_MINUS
|
static char |
CHAR_MULTIPLY
|
static char |
CHAR_NEGATIVE
|
static char |
CHAR_POINT
|
static int |
OPERATOR_ADD
|
static int |
OPERATOR_DIVIDE
|
static int |
OPERATOR_MINUS
|
static int |
OPERATOR_MULTIPLY
|
static int |
OPERATOR_NONE
|
static String |
PROPERTY_DISPLAY_TEXT
|
static String |
PROPERTY_OPERATOR
|
Fields inherited from class javax.swing.JComponent |
---|
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
Fields inherited from class java.awt.Component |
---|
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
Fields inherited from interface java.awt.image.ImageObserver |
---|
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
Constructor Summary | |
---|---|
Calculator()
Creates a Calculator . |
Method Summary | |
---|---|
void |
actionPerformed(ActionEvent e)
|
protected void |
beep()
|
void |
clear()
Clears the internal state and reset the calculator. |
void |
commit()
Calculates the pending calculation. |
protected void |
configureNumberFormat()
Configures the number format for displaying purpose. |
protected AbstractButton |
createButton(String text)
Creates the button that is used in the Calculator. |
protected AbstractButton |
createButton(String text,
Icon icon)
Creates the button that is used in the Calculator. |
protected void |
fakePressButton(AbstractButton button)
Press the button. |
int |
getButtonGap()
Gets the gap between buttons. |
int |
getButtonHeight()
Gets the button height. |
int |
getButtonWidth()
Gets the button width. |
NumberFormat |
getDisplayFormat()
Gets the display format for the number. |
String |
getDisplayText()
Gets the display text. |
int |
getOperator()
Gets the current operator. |
double |
getResult()
Gets the last calculated result. |
protected void |
initComponents()
|
void |
input(char c)
Inputs a char to the calculator. |
protected boolean |
isCellEditor()
If this method return true, ENTER and ESCAPE key will be registered. |
static boolean |
isEnterOrEqual(KeyEvent keyEvent)
Checks if the key event a key event for enter. |
static boolean |
isOperator(KeyEvent keyEvent)
Checks if the key event a key event for operators. |
boolean |
isOverflow()
Checks if the calculator is in overflow state. |
boolean |
isResultCalculated()
Get the flag indicating if the result was calculated at least once. |
static boolean |
isValidKeyEvent(KeyEvent keyEvent)
Checks if the key event a valid key event that can be accepted by the Calculator. |
static void |
main(String[] args)
|
void |
registerKeyboardActions(JComponent component,
int condition)
Registers necessary keyboard actions onto the component. |
void |
setButtonGap(int buttonGap)
|
void |
setButtonHeight(int buttonHeight)
Sets the button height. |
void |
setButtonWidth(int buttonWidth)
Sets the button width. |
void |
setDisplayFormat(NumberFormat displayFormat)
Sets the display format for the number. |
void |
setDisplayText(String displayText)
Sets the display text and fire property change event on property named PROPERTY_DISPLAY_TEXT . |
void |
setInitialValue(String value)
|
void |
setLocale(Locale l)
|
void |
setOperator(int operator)
Sets the operator and fire property change event on property named PROPERTY_OPERATOR . |
void |
setOverflow(boolean overflow)
Sets the overflow flag. |
void |
setResultCalculated(boolean resultCalculated)
Set the flag indicating if the result was calculated at least once. |
void |
unregisterKeyboardActions(JComponent component)
Unregisters the keyboard actions you registered using registerKeyboardActions(javax.swing.JComponent,
int) . |
void |
updateResult()
Update the result as if the equal was pressed. |
Methods inherited from class javax.swing.JPanel |
---|
getAccessibleContext, getUI, getUIClassID, paramString, setUI, updateUI |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int OPERATOR_NONE
public static final int OPERATOR_ADD
public static final int OPERATOR_MINUS
public static final int OPERATOR_MULTIPLY
public static final int OPERATOR_DIVIDE
public static final char CHAR_CLEAR
public static final char CHAR_POINT
public static final char CHAR_ADD
public static final char CHAR_MINUS
public static final char CHAR_MULTIPLY
public static final char CHAR_DIVIDE
public static final char CHAR_EQUAL
public static final char CHAR_NEGATIVE
public static final char CHAR_BACKSPACE
public static final char CHAR_0
public static final char CHAR_1
public static final char CHAR_2
public static final char CHAR_3
public static final char CHAR_4
public static final char CHAR_5
public static final char CHAR_6
public static final char CHAR_7
public static final char CHAR_8
public static final char CHAR_9
public static final String PROPERTY_DISPLAY_TEXT
public static final String PROPERTY_OPERATOR
Constructor Detail |
---|
public Calculator()
Calculator
.
Method Detail |
---|
public void setLocale(Locale l)
setLocale
in class Component
protected void configureNumberFormat()
public static boolean isValidKeyEvent(KeyEvent keyEvent)
keyEvent
- the key event.
public static boolean isOperator(KeyEvent keyEvent)
CHAR_ADD
, CHAR_MINUS
, CHAR_MULTIPLY
or CHAR_DIVIDE
, this method will return true.
keyEvent
- the key event.
public static boolean isEnterOrEqual(KeyEvent keyEvent)
KeyEvent.VK_ENTER
or KeyEvent.VK_EQUALS
, this method will return true.
keyEvent
- the key event.
public void registerKeyboardActions(JComponent component, int condition)
JTextField
.
component
- the component where the key input will be taken and passed to the Calculator
.condition
- the condition as defined in JComponent.registerKeyboardAction(java.awt.event.ActionListener,
javax.swing.KeyStroke, int)
.public void unregisterKeyboardActions(JComponent component)
registerKeyboardActions(javax.swing.JComponent,
int)
.
component
- the component.protected void initComponents()
public boolean isResultCalculated()
public void setResultCalculated(boolean resultCalculated)
resultCalculated
- the flagprotected AbstractButton createButton(String text)
createButton(String,
javax.swing.Icon)
method.
text
- the text on the button.
protected AbstractButton createButton(String text, Icon icon)
AbstractButton button = new JideButton(text, icon);
button.setOpaque(true);
button.setContentAreaFilled(true);
button.setRequestFocusEnabled(false);
button.setFocusable(false);
button.addActionListener(this);
return button;
text
- the text on the button.icon
- the icon on the button.
public boolean isOverflow()
public void setOverflow(boolean overflow)
overflow
- the overflow flag.public void input(char c)
Calculator
class as CHAR_XXX constants.
c
- the char input char.protected void beep()
public void updateResult()
public void clear()
public double getResult()
public String getDisplayText()
public void setDisplayText(String displayText)
PROPERTY_DISPLAY_TEXT
.
displayText
- the displayed text.public int getOperator()
public void setOperator(int operator)
PROPERTY_OPERATOR
.
operator
- the operator.public void actionPerformed(ActionEvent e)
actionPerformed
in interface ActionListener
protected void fakePressButton(AbstractButton button)
button
- the buttonpublic NumberFormat getDisplayFormat()
public void setDisplayFormat(NumberFormat displayFormat)
displayFormat
- the display format.public void commit()
public int getButtonWidth()
public void setButtonWidth(int buttonWidth)
buttonWidth
- the new button width.public int getButtonHeight()
public void setButtonHeight(int buttonHeight)
buttonHeight
- the new button height.public int getButtonGap()
public void setButtonGap(int buttonGap)
protected boolean isCellEditor()
public void setInitialValue(String value)
public static void main(String[] args)
|
JIDE 3.5.15 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |