Java GUI Calculator In this homework I am working on Java GU
Java GUI Calculator
In this homework, I am working on Java GUI Components and interfaces. The objectives of this program are to learn the use of Java swing components, GroupLayout, ActionListener, and MouseListener interfaces. The calculator must come with a menu (which its requirements are shown in the image below) and it must be capable of accepting hex, dec, oct, and bin inputs. It must be graphically similar (while not necessarily 100% so) to the image shown below, and be capable of using all of its buttons.
All of the test cases that will come with this calculator are...
Disable Dword, Word, Byte, but keep the buttons there. Also, no interaction of the mouse on the binary bits area is required. Just display 64 bits for allthe items with appropriate bits filled in based on the value in the number field. View Edit Help Menu items View Hide and Show the calculator Edit Copy the Text Help Perform the same as the actual Windows calculator 0000 0000 0000 0000 0000 0000 0000 0000 0000 31 Quot Mod A OHex Dec C 7 8 9 OBin Qword ODword E 1 2 3 Word OByteSolution
//aboutFrame
import javax.swing.*;
public class aboutFrame extends JFrame {
public JTextArea about;
aboutFrame(){
about = new JTextArea();
about.setEditable(false);
add(about);
about.setText(\"Hello! This is my Calculator!\");
setVisible(true);
setSize(370,200);
setTitle(\"About this calculator...\");
}
}
====================================================================
//ConversionPanel.java
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import java.awt.ComponentOrientation;
public class ConversionPanel extends JPanel {
public JTextArea conversionBox;
public JScrollPane scrollPane;
public RadioButtonPanel radioPad;
boolean h, d, o, b;
public ConversionPanel(){
radioPad = new RadioButtonPanel();
conversionBox = new JTextArea(2,44);
conversionBox.setFont(new Font(\"Arial\", Font.PLAIN, 15)); // setting the font
conversionBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); // setting the text to start from right side
scrollPane = new JScrollPane( conversionBox );
add(scrollPane);
conversionBox.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
e.consume(); // ignore event
}
});
}
public void setTheResult(String s, boolean qw, boolean dw, boolean wo){
char result[] = binaryString(s).toCharArray();
if(qw == true){
conversionBox.setText(result[31] + \" \" + result[30] + \" \" + result[29] + \" \" + result[28] + \" \" + result[27] + \" \" + result[26] + \" \"
+ result[25] + \" \" + result[24] + \" \" + result[23] + \" \" + result[22] + \" \" + result[21] + \" \" + result[20] + \" \" +
result[19] + \" \" + result[18] + \" \" + result[17] + \" \" + result[16] + \" \" + result[15] + \" \" + result[14] + \" \" + result[13] + \" \"
+ result[12] + \" \" + result[11] + \" \" + result[10] + \" \" + result[9] + \" \" + result[8] + \" \" + result[7] + \" \" + result[6] + \" \"
+ result[5] + \" \" + result[4] + \" \" + result[3] + \" \" + result[2] + \" \" + result[1] + \" \" + result[0] + \" \ \" + result[63] + \" \" + result[62] + \" \" + result[61] +
\" \" + result[60] + \" \" + result[59] + \" \" + result[58] + \" \"
+ result[57] + \" \" + result[56] + \" \" + result[55] + \" \" + result[54] + \" \" + result[53] + \" \" + result[52] + \" \" +
result[51] + \" \" + result[50] + \" \" + result[49] + \" \" + result[48] + \" \" + result[47] + \" \" + result[46] + \" \" + result[45] + \" \"
+ result[44] + \" \" + result[43] + \" \" + result[42] + \" \" + result[41] + \" \" + result[40] + \" \" + result[39] + \" \" + result[38] + \" \"
+ result[37] + \" \" + result[36] + \" \" + result[35] + \" \" + result[34] + \" \" + result[33] + \" \" + result[32]);
}
else if(dw == true){
conversionBox.setText(\"\ \" + result[63] + \" \" + result[62] + \" \" + result[61] +
\" \" + result[60] + \" \" + result[59] + \" \" + result[58] + \" \"
+ result[57] + \" \" + result[56] + \" \" + result[55] + \" \" + result[54] + \" \" + result[53] + \" \" + result[52] + \" \" +
result[51] + \" \" + result[50] + \" \" + result[49] + \" \" + result[48] + \" \" + result[47] + \" \" + result[46] + \" \" + result[45] + \" \"
+ result[44] + \" \" + result[43] + \" \" + result[42] + \" \" + result[41] + \" \" + result[40] + \" \" + result[39] + \" \" + result[38] + \" \"
+ result[37] + \" \" + result[36] + \" \" + result[35] + \" \" + result[34] + \" \" + result[33] + \" \" + result[32]);
}
else if(wo == true){
conversionBox.setText(\"\ \" + result[63] + \" \" + result[62] + \" \" + result[61] +
\" \" + result[60] + \" \" + result[59] + \" \" + result[58] + \" \"
+ result[57] + \" \" + result[56] + \" \" + result[55] + \" \" + result[54] + \" \" + result[53] + \" \" + result[52] + \" \" +
result[51] + \" \" + result[50] + \" \" + result[49] + \" \" + result[48]);
}
else{
conversionBox.setText(\"\ \" + result[63] + \" \" + result[62] + \" \" + result[61] +
\" \" + result[60] + \" \" + result[59] + \" \" + result[58] + \" \"
+ result[57] + \" \" + result[56]);
}
}
public String binaryString(String binaryConversion){
int num = Integer.parseInt(binaryConversion);
String result = String.format(\"%64s\", Integer.toBinaryString(num)).replace(\' \', \'0\');
return result;
}
}
========================================================================
//NumPanel.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NumPanel extends JPanel implements ActionListener {
public JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnC, btnCE, btnBS, aBtn, bBtn, cBtn, dBtn, eBtn, fBtn, btnblnk1, btnblnk2, btnblnk3, btnblnk4;
public RadioButtonPanel hexTruth;
public ResultPanel result;
public NumPanel(){
btnblnk1 = new JButton(\" \");
btnblnk2 = new JButton(\" \");
btnblnk3 = new JButton(\" \");
btnblnk4 = new JButton(\" \");
btn0 = new JButton(\"0\");
btn1 = new JButton(\"1\");
btn2 = new JButton(\"2\");
btn3 = new JButton(\"3\");
btn4 = new JButton(\"4\");
btn5 = new JButton(\"5\");
btn6 = new JButton(\"6\");
btn7 = new JButton(\"7\");
btn8 = new JButton(\"8\");
btn9 = new JButton(\"9\");
btnC = new JButton(\"C\");
btnCE = new JButton(\"CE\");
btnBS = new JButton(\"\\u2190\");
aBtn = new JButton(\"A\");
bBtn = new JButton(\"B\");
cBtn = new JButton(\"C\");
dBtn = new JButton(\"D\");
eBtn = new JButton(\"E\");
fBtn = new JButton(\"F\");
aBtn.setEnabled(false);
bBtn.setEnabled(false);
cBtn.setEnabled(false);
dBtn.setEnabled(false);
eBtn.setEnabled(false);
fBtn.setEnabled(false);
btnblnk1.setEnabled(false);
btnblnk2.setEnabled(false);
btnblnk3.setEnabled(false);
btnblnk4.setEnabled(false);
}
public void actionPerformed(ActionEvent e){
}
public String backSpace(String s){
String bs = null;
if(s.length() > 0){
StringBuilder newStr = new StringBuilder(s);
newStr.deleteCharAt(s.length() - 1);
bs = newStr.toString();
}
return bs;
}
}
===============================================================================
//OperationsPanel.java
import javax.swing.*;
import java.awt.event.*;
public class OperationsPanel extends JPanel implements ActionListener{
public JButton addButton, subButton, divButton, multButton, rootButton, oneoverxButton, pmButton, modButton, eButton;
public OperationsPanel(){
addButton = new JButton(\"+\");
subButton = new JButton(\"-\");
divButton = new JButton(\"/\");
multButton = new JButton(\"*\");
rootButton = new JButton(\"\\u221A\");
oneoverxButton = new JButton(\"x\");
pmButton = new JButton(\"\\u00B1\");
modButton = new JButton(\"%\");
eButton = new JButton(\"=\");
rootButton.setEnabled(false);
oneoverxButton.setEnabled(false);
modButton.setEnabled(false);
}
public void actionPerformed(ActionEvent e){
}
}
==================================================================================
//RadioButtonPanel.java
import javax.swing.*;
import java.awt.GridLayout;
public class RadioButtonPanel extends JPanel {
public JRadioButton hexBtn, decBtn, octBtn, binBtn, qWordBtn, dWordBtn, wordBtn, byteBtn;
public RadioButtonPanel(){
hexBtn = new JRadioButton(\" HEX\");
decBtn = new JRadioButton(\" DEC\");
octBtn = new JRadioButton(\" OCT\");
binBtn = new JRadioButton(\" BIN\");
qWordBtn = new JRadioButton(\" qWORD \");
dWordBtn = new JRadioButton(\" dWORD\");
wordBtn = new JRadioButton(\" WORD\");
byteBtn = new JRadioButton(\" BYTE\");
ButtonGroup group1 = new ButtonGroup();
group1.add(hexBtn);
group1.add(decBtn);
group1.add(octBtn);
group1.add(binBtn);
decBtn.setSelected(true);
ButtonGroup group2 = new ButtonGroup();
group2.add(qWordBtn);
group2.add(dWordBtn);
group2.add(wordBtn);
group2.add(byteBtn);
qWordBtn.setSelected(true);
setLayout(new GridLayout(8,1));
add(hexBtn);
add(decBtn);
add(octBtn);
add(binBtn);
add(qWordBtn);
add(dWordBtn);
add(wordBtn);
add(byteBtn);
}
}
============================================================================
//TestMyCalculator.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.Toolkit;
public class TestMyCalculator {
public static void main(String[] args) {
// This section calls on the Actual calculator Frame
final MyCalculatorFrame Calc = new MyCalculatorFrame();
Calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Calc.setSize(600,450);
Calc.setTitle(\"Calculator\");
Calc.setVisible(true);
// Creating a Menu Bar for the Calculator
JMenuBar menuBar = new JMenuBar();
JMenu view = new JMenu(\"View\");
JMenu edit = new JMenu(\"Edit\");
JMenu help = new JMenu(\"Help\");
// Giving the Menu Items
JMenuItem hide = new JMenuItem(\"Hide\");
JMenuItem show = new JMenuItem(\"Show\");
JMenuItem copy = new JMenuItem(\"Copy\");
JMenuItem viewHelp = new JMenuItem(\"View Help\");
JMenuItem about = new JMenuItem(\"About\");
Calc.setJMenuBar(menuBar);
menuBar.add(view);
menuBar.add(edit);
menuBar.add(help);
view.add(hide);
view.add(show);
edit.add(copy);
help.add(viewHelp);
help.add(about);
// If Hide is clicked, it\'ll hide the calculator
hide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Calc.resultTextBox.setVisible(false);
Calc.numPad.setVisible(false);
Calc.opPad.setVisible(false);
Calc.conTextBox.setVisible(false);
Calc.radioPad.setVisible(false);
}
});
// If Show is clicked, it\'ll show the calculator
show.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Calc.resultTextBox.setVisible(true);
Calc.numPad.setVisible(true);
Calc.opPad.setVisible(true);
Calc.conTextBox.setVisible(true);
Calc.radioPad.setVisible(true);
}
});
// If Copy is clicked, the result from the result box will be copied to the clip board
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String myString = Calc.resultTextBox.getTheNumber();
StringSelection stringSelection = new StringSelection(myString);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);
}
});
// If About is clicked, the About BOX will pop up
about.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
aboutFrame aboutBox = new aboutFrame();
}
});
// If View Help is clicked, the Help BOX will pop up
viewHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ViewHelp helpBox = new ViewHelp();
}
});
}
}
=========================================================================
// ViewHelp.java
import javax.swing.*;
public class ViewHelp extends JFrame {
public JTextArea help;
ViewHelp(){
help = new JTextArea();
help.setEditable(false);
add(help);
help.setText(\"Need help on using this calculator?\ \"
+ \"To use one of the functions (dword or qword), you must \ \"
+ \"update the calculator each usesage by pressing the = button \ \"
+ \"That way the calculator knows when to update it\'s info!\"
+ \"\ \ \" + \"The rest of the calculator is pretty easy to use!\");
setVisible(true);
setSize(370,200);
setTitle(\"Help\");
}
}
==============================================================================






