• Home
  • Blog
  • Java – Use Swing components to create a simple GUI based program

Java – Use Swing components to create a simple GUI based program

0 comments

The
purpose of this program will be to provide a simple Binary Calculator.

The
user will enter operands for calculation with the provided “0” and “1” input
JButtons. The operation they want to perform will be selected with a JComboBox.
Once the second operand has been entered they may select the “Compute” JButton
and the results of the calculation will be displayed either in Binary or
Decimal depending on the state of the JRadioButtons.

They
can then perform another operation using the result as the first operand or
press the “Clear” JButton to start a new calculation.

A
total of two files are required.

BinaryCalculatorDriver.java
(provided)

BinaryCalculatorFrame.java

UML DIAGRAM FOR AND
DISCUSSION FOR BinaryCalculatorFrame

BinaryCalculatorFrame
extends JFrame

 
JTextField display

 
JRadioButton binary

 
JRadioButton decimal

 
ButtonGroup buttonGroup

 
JButton zeroButton

 
JButton oneButton;

 
JComboBox<String> operator

 
JButton compute

 
JButton clear

 
int currentOp

 
int displayedOperand

 
int operand1

 
int operand2

<<constructor>>  BinaryCalculatorFrame()

Notes
on BinaryCalclatorFrame

Additionally,
this class will declare two nested classes, one for each type of Event that may
occur. One will implement ActionListener (for the zeroButton, oneButton,
compute, and clear components) and the other will implement ItemListener (for
operator component). The display field does not need a Listener since it will
only be used for output. The binary and decimal radio buttons do not need a
listener since their states will only be used by compute when it changes the
value of the display text field.



Data Members:

currentOp
– represents the operator selected with the following possible values:

0
– no operator selected

1
– addition

2
– subtraction

3
– multiplication

4
– division

displayedOperand
– int value of the operand that is being displayed

operand1
– int value of the first operand in the calculation

operand2
– int value of the second operand in the calculation

Component descriptions:

display
– JTextField, starting text should be “0”, set its width to 20. There is a
JTextField constructor that accepts the initial String value and field width as
an int. Set it as uneditable and its alignment to right justified like this:

display.setHorizontalAlignment(JTextField.RIGHT);

binary,
decimal – JRadioButtons, initialize binary to true and decimal to false. Add
both to buttonGroup

zeroButton
– JButton, when pressed will add a 0 digit to the end of displayedOperand and
update display

oneButton
– JButton, when pressed will add a 1 digit to the end of displayedOperand and
update display

operator
– JComboBox, List of possible operators. List of Strings include, “OP” meaning
no operator is selected, and “+”, “-“, “x”, and “/” representing each of the
possible operations. When the selection of the box is changed (if it has not
been changed to 0) updates currentOp to the correct value depending on the
operator selected. The current value of the displayed operand should be stored
into operand1 and the displayed operand cleared out (set to 0) and updated in
the display.

compute
– JButton, when pressed the user wants to perform the selected operation (if
any). The currently displayed operand will be the second operand in the
calculation. Based upon the currently selected operation calculates the result
and sets this as the current value of displayedOperand. Depending on which
radio button is selected (either binary or decimal) the result will be
displayed in the corresponding base. The currently selected operator will be
reset to “OP”/0.

You
do not need to write methods to do calculations with binary values. Instead,
convert the operands into decimal and perform the calculations on the decimal
values.

The
following static Integer methods will be extremely helpful:

String
toString(int) – takes an int and returns a String representation

int
parseInt(String) – takes a String and converts it into an int

int
parseInt(String, int) – takes a String and a base (such as base 2 for binary).
Converts the String in the provided base and returns its decimal value as an
int

String
toBinaryString(int) – takes a decimal value and returns its binary
representation as a String

clear
– JButton, resets all values. displayedOperand, operand1, operand2, and
currentOp to 0, displayed text to “0”, and selected Operator to “OP”.

Other Notes:

Import all of the necessary
classes

Name the components as
show in the example

Please make sure your
program compiles with just the two source code files and command: “javac BinaryCalculatorDriver.java”.
An IDE may add extra stuff to your source code and causes it to need extra
configurations to compile.

Examples:

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}