Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/simplejavacalculator/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.Box;
import javax.swing.BoxLayout;
Expand All @@ -48,7 +48,7 @@ public class UI implements ActionListener {
private final JPanel panelSub7;
private final JPanel panelSub8;

private final JTextArea text;
private final JTextField text;

private final JButton but[], butAdd, butMinus, butMultiply, butDivide,
butEqual, butCancel, butSquareRoot, butSquare, butOneDividedBy,
Expand Down Expand Up @@ -83,7 +83,10 @@ public UI() throws IOException {

font = new Font("Consolas",Font.PLAIN, 18);

text = new JTextArea(1, 30);
text = new JTextField(30);
text.setBackground(java.awt.Color.WHITE);
text.setForeground(java.awt.Color.BLACK);
text.setCaretColor(java.awt.Color.BLACK);

textFont = new Font("Consolas",Font.BOLD, 24);

Expand Down Expand Up @@ -231,7 +234,8 @@ public void actionPerformed(ActionEvent e) {

for (int i = 0; i < 10; i++) {
if (source == but[i]) {
text.replaceSelection(buttonValue[i]);
String currentText = text.getText();
text.setText(currentText + buttonValue[i]);
return;
}
}
Expand All @@ -246,22 +250,22 @@ public void actionPerformed(ActionEvent e) {
if (checkNum != null || source == butCancel) {
if (source == butAdd) {
writer(calc.calculateBi(Calculator.BiOperatorModes.add, reader()));
text.replaceSelection(butAdd.getText());
text.setText(text.getText() + butAdd.getText());
}

if (source == butMinus) {
writer(calc.calculateBi(Calculator.BiOperatorModes.minus, reader()));
text.replaceSelection(butMinus.getText());
text.setText(text.getText() + butMinus.getText());
}

if (source == butMultiply) {
writer(calc.calculateBi(Calculator.BiOperatorModes.multiply, reader()));
text.replaceSelection(butMultiply.getText());
text.setText(text.getText() + butMultiply.getText());
}

if (source == butDivide) {
writer(calc.calculateBi(Calculator.BiOperatorModes.divide, reader()));
text.replaceSelection(butDivide.getText());
text.setText(text.getText() + butDivide.getText());
}

if (source == butxpowerofy) {
Expand Down Expand Up @@ -309,7 +313,8 @@ public void actionPerformed(ActionEvent e) {
parsetoBinary();
}

text.selectAll();
// Don't select all text after each action
text.setCaretPosition(text.getText().length());
}

private void parsetoBinary() {
Expand All @@ -334,6 +339,7 @@ public void writer(final Double num) {
text.setText("");
} else {
text.setText(Double.toString(num));
text.repaint(); // Force a repaint of the text area
}
}
}