fixed build process and added project configuration files from idea
Some checks failed
Build the Jar / build (push) Failing after 11s
Some checks failed
Build the Jar / build (push) Failing after 11s
This commit is contained in:
@@ -44,7 +44,7 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
protected double result;
|
||||
private String[] CalculatorModes = new String[] {"Standard", "Scientific", "Data calculation"};
|
||||
private JComboBox<String> modeselect;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
FlatMacDarkLaf.setup();
|
||||
try {
|
||||
@@ -76,22 +76,22 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
icons.add(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon512.png")));
|
||||
icons.add(Toolkit.getDefaultToolkit().getImage(Calculator.class.getResource("/images/appIcon1024.png")));
|
||||
setIconImages(icons);
|
||||
|
||||
|
||||
setTitle("angel's awesome calculator");
|
||||
setBounds(100, 100, 368, 556);
|
||||
setFocusable(true);
|
||||
addKeyListener(this);
|
||||
|
||||
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
setContentPane(contentPane);
|
||||
|
||||
|
||||
display = new JTextField();
|
||||
display.setEditable(false);
|
||||
display.setFont(new Font("Segoe UI", Font.PLAIN, 22));
|
||||
display.setColumns(10);
|
||||
JPanel buttonPanel = new JPanel();
|
||||
|
||||
|
||||
addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
@@ -99,7 +99,7 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
System.out.println("Window resized to " + size.width + "x" + size.height);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
JPanel panel = new JPanel();
|
||||
GroupLayout gl_contentPane = new GroupLayout(contentPane);
|
||||
gl_contentPane.setHorizontalGroup(
|
||||
@@ -119,10 +119,10 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
.addPreferredGap(ComponentPlacement.RELATED)
|
||||
.addComponent(display, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
|
||||
.addGap(27)
|
||||
.addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, 359, GroupLayout.PREFERRED_SIZE)
|
||||
.addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, 359, GroupLayout.PREFERRED_SIZE)
|
||||
.addContainerGap(199, Short.MAX_VALUE))
|
||||
);
|
||||
|
||||
|
||||
buttonPanel.setLayout(new GridLayout(0, 4, 0, 0));
|
||||
|
||||
modeselect = new JComboBox<>(CalculatorModes);
|
||||
@@ -134,22 +134,26 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
}
|
||||
});
|
||||
modeselect.setEditable(false);
|
||||
|
||||
JButton btnNewButton_1 = new JButton("New button");
|
||||
JLabel lblNewLabel = new JLabel("New label");
|
||||
modeselect.setFocusable(false);
|
||||
|
||||
JButton btnNewButton_1 = new JButton("New button");
|
||||
btnNewButton_1.setFocusable(false);
|
||||
JLabel lblNewLabel = new JLabel("New label");
|
||||
lblNewLabel.setFocusable(false);
|
||||
|
||||
|
||||
|
||||
panel.add(modeselect);
|
||||
panel.add(lblNewLabel);
|
||||
panel.add(btnNewButton_1);
|
||||
|
||||
JButton empty_4 = new JButton("");
|
||||
JButton empty_1 = new JButton("");
|
||||
JButton empty_2 = new JButton("");
|
||||
JButton empty = new JButton("");
|
||||
JButton empty_3 = new JButton("");
|
||||
|
||||
JButton btnErase = new JButton("DEL");
|
||||
JButton btnPercent = new JButton("%");
|
||||
JButton btnClearEntry = new JButton("CE");
|
||||
JButton btnReciprocal = new JButton("1/x");
|
||||
JButton btnSquare = new JButton("x²");
|
||||
JButton btnSquareRoot = new JButton("√x");
|
||||
|
||||
// Use FlatLaf Tree.icon.collapsed as backspace-like icon (or just use text)
|
||||
JButton btnBackspace = new JButton("⌫");
|
||||
JButton btnClear = new JButton("C");
|
||||
JButton btnPlusMin = new JButton("+/-");
|
||||
JButton btnDot = new JButton(".");
|
||||
@@ -162,7 +166,7 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
JButton btn4 = new JButton("4");
|
||||
JButton btn5 = new JButton("5");
|
||||
JButton btn6 = new JButton("6"); // why was 6 afraid of 7... because 7 8 9 :sob:
|
||||
JButton btn7 = new JButton("7"); // 67!
|
||||
JButton btn7 = new JButton("7"); // 67!
|
||||
JButton btn8 = new JButton("8");
|
||||
JButton btn9 = new JButton("9");
|
||||
|
||||
@@ -174,9 +178,9 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
btnDot.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String n = display.getText(); // get the text on the display
|
||||
if (n.contains(".")) return; // if it already contains a "." skip rest of this
|
||||
if (n.contains(".")) return; // if it already contains a "." skip rest of this
|
||||
processDigit(e.getActionCommand());
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
@@ -185,19 +189,25 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
math();
|
||||
}
|
||||
});
|
||||
|
||||
btnErase.addActionListener(new ActionListener() {
|
||||
|
||||
btnBackspace.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
clear();
|
||||
backspace();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
btnClearEntry.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
clearEntry();
|
||||
}
|
||||
});
|
||||
|
||||
btnClear.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
clear();
|
||||
clearAll();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
btnPlus.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setOperator(e.getActionCommand());
|
||||
@@ -208,8 +218,8 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setOperator(e.getActionCommand());
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
btnMul.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setOperator(e.getActionCommand());
|
||||
@@ -245,13 +255,13 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
processDigit(e.getActionCommand());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
btn4.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
processDigit(e.getActionCommand());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
btn5.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
processDigit(e.getActionCommand());
|
||||
@@ -268,37 +278,62 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
processDigit(e.getActionCommand());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
btn8.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
processDigit(e.getActionCommand());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
btn9.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
processDigit(e.getActionCommand());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
btnPlusMin.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
String tmp = display.getText();
|
||||
if (tmp == null || tmp.isEmpty()) return;
|
||||
char first = tmp.charAt(0);
|
||||
if (first == '-') {
|
||||
tmp = tmp.substring(1, tmp.length());
|
||||
tmp = tmp.substring(1, tmp.length());
|
||||
display.setText(tmp);
|
||||
} else {
|
||||
display.setText("-" + tmp);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
btnErase.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnPercent.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
setOperator("%");
|
||||
}
|
||||
});
|
||||
|
||||
btnReciprocal.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
reciprocal();
|
||||
}
|
||||
});
|
||||
|
||||
btnSquare.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
square();
|
||||
}
|
||||
});
|
||||
|
||||
btnSquareRoot.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
squareRoot();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
btnBackspace.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnClear.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnClearEntry.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnPlusMin.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnEq.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnDot.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
@@ -316,20 +351,44 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
btnMin.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnDiv.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnPlus.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnPercent.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnReciprocal.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnSquare.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
btnSquareRoot.setFont(new Font("Segoe UI", Font.PLAIN, 29));
|
||||
|
||||
empty.setEnabled(false);
|
||||
empty_1.setEnabled(false);
|
||||
empty_2.setEnabled(false);
|
||||
empty_3.setEnabled(false);
|
||||
empty_4.setEnabled(false);
|
||||
// Make all buttons non-focusable so keyboard input always goes to the frame
|
||||
btnBackspace.setFocusable(false);
|
||||
btnClear.setFocusable(false);
|
||||
btnClearEntry.setFocusable(false);
|
||||
btnPlusMin.setFocusable(false);
|
||||
btnEq.setFocusable(false);
|
||||
btnDot.setFocusable(false);
|
||||
btn0.setFocusable(false);
|
||||
btn1.setFocusable(false);
|
||||
btn2.setFocusable(false);
|
||||
btn3.setFocusable(false);
|
||||
btn4.setFocusable(false);
|
||||
btn5.setFocusable(false);
|
||||
btn6.setFocusable(false);
|
||||
btn7.setFocusable(false);
|
||||
btn8.setFocusable(false);
|
||||
btn9.setFocusable(false);
|
||||
btnMul.setFocusable(false);
|
||||
btnMin.setFocusable(false);
|
||||
btnDiv.setFocusable(false);
|
||||
btnPlus.setFocusable(false);
|
||||
btnPercent.setFocusable(false);
|
||||
btnReciprocal.setFocusable(false);
|
||||
btnSquare.setFocusable(false);
|
||||
btnSquareRoot.setFocusable(false);
|
||||
|
||||
buttonPanel.add(empty);
|
||||
buttonPanel.add(empty_3);
|
||||
buttonPanel.add(btnPercent);
|
||||
buttonPanel.add(btnClearEntry);
|
||||
buttonPanel.add(btnClear);
|
||||
buttonPanel.add(btnErase);
|
||||
buttonPanel.add(empty_4);
|
||||
buttonPanel.add(empty_1);
|
||||
buttonPanel.add(empty_2);
|
||||
buttonPanel.add(btnBackspace);
|
||||
buttonPanel.add(btnReciprocal);
|
||||
buttonPanel.add(btnSquare);
|
||||
buttonPanel.add(btnSquareRoot);
|
||||
buttonPanel.add(btnDiv);
|
||||
buttonPanel.add(btn7);
|
||||
buttonPanel.add(btn8);
|
||||
@@ -347,7 +406,7 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
buttonPanel.add(btn0);
|
||||
buttonPanel.add(btnDot);
|
||||
buttonPanel.add(btnEq);
|
||||
|
||||
|
||||
contentPane.setLayout(gl_contentPane);
|
||||
|
||||
}
|
||||
@@ -362,7 +421,7 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
}
|
||||
}
|
||||
|
||||
void clear() {
|
||||
void backspace() {
|
||||
String tmp = display.getText();
|
||||
if (tmp.length()>1) // if there is more than 1 character
|
||||
display.setText( tmp.substring(0, tmp.length()-1) );
|
||||
@@ -370,11 +429,56 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
display.setText("");
|
||||
}
|
||||
|
||||
void clearEntry() {
|
||||
// Clear only the current display (entry)
|
||||
display.setText("");
|
||||
}
|
||||
|
||||
void clearAll() {
|
||||
// Clear everything including operands and operator
|
||||
display.setText("");
|
||||
operand1 = "";
|
||||
operand2 = "";
|
||||
operator = "";
|
||||
resultVisible = false;
|
||||
}
|
||||
|
||||
void reciprocal() {
|
||||
String tmp = display.getText();
|
||||
if (tmp == null || tmp.isEmpty()) return;
|
||||
double value = Double.parseDouble(tmp);
|
||||
if (value != 0) {
|
||||
result = 1.0 / value;
|
||||
display.setText("" + result);
|
||||
resultVisible = true;
|
||||
}
|
||||
}
|
||||
|
||||
void square() {
|
||||
String tmp = display.getText();
|
||||
if (tmp == null || tmp.isEmpty()) return;
|
||||
double value = Double.parseDouble(tmp);
|
||||
result = value * value;
|
||||
display.setText("" + result);
|
||||
resultVisible = true;
|
||||
}
|
||||
|
||||
void squareRoot() {
|
||||
String tmp = display.getText();
|
||||
if (tmp == null || tmp.isEmpty()) return;
|
||||
double value = Double.parseDouble(tmp);
|
||||
if (value >= 0) {
|
||||
result = Math.sqrt(value);
|
||||
display.setText("" + result);
|
||||
resultVisible = true;
|
||||
}
|
||||
}
|
||||
|
||||
void processDigit(String actionCommand) {
|
||||
if (resultVisible == true) {
|
||||
display.setText("");
|
||||
resultVisible = false;
|
||||
}
|
||||
}
|
||||
// if (actionCommand == "+") {
|
||||
// btnDiv.setForeground(new Color(0, 0, 0));
|
||||
// btnMul.setForeground(new Color(0, 0, 0));
|
||||
@@ -397,19 +501,19 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
// btnMin.setForeground(new Color(30, 144, 255));
|
||||
// }
|
||||
display.setText(display.getText() + actionCommand);
|
||||
|
||||
|
||||
}
|
||||
void setOperator(String daop) {
|
||||
operand1 = display.getText();
|
||||
operator = daop;
|
||||
display.setText("");
|
||||
}
|
||||
|
||||
|
||||
void math() {
|
||||
operand2 = display.getText();
|
||||
double op1 = Double.parseDouble(operand1);
|
||||
double op2 = Double.parseDouble(operand2);
|
||||
|
||||
|
||||
if (operator == "+") {
|
||||
result = op1+op2;
|
||||
} else if (operator == "-") {
|
||||
@@ -418,25 +522,27 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
result = op1*op2;
|
||||
} else if (operator == "/") {
|
||||
result = op1/op2;
|
||||
} else if (operator == "%") {
|
||||
result = op1 % op2;
|
||||
} else {
|
||||
result = op2;
|
||||
System.out.println("Op: " + op1);
|
||||
System.out.println("Op2: " + op2);
|
||||
}
|
||||
|
||||
|
||||
operator = "";
|
||||
operand1 = "";
|
||||
operand2 = "";
|
||||
|
||||
|
||||
// btnDiv.setForeground(new Color(0, 0, 0));
|
||||
// btnMul.setForeground(new Color(0, 0, 0));
|
||||
// btnPlus.setForeground(new Color(0, 0, 0));
|
||||
// btnMin.setForeground(new Color(0, 0, 0));
|
||||
|
||||
|
||||
resultVisible = true;
|
||||
display.setText(""+result);
|
||||
}
|
||||
|
||||
|
||||
// Implement the keyPressed method
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
@@ -445,14 +551,14 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
if (KeyEvent.getKeyText(keyCode) == "Enter") {
|
||||
math();
|
||||
} else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
|
||||
clear();
|
||||
backspace();
|
||||
}
|
||||
}
|
||||
|
||||
// Implement the keyReleased method
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
// int keyCode = e.getKeyCode();
|
||||
// int keyCode = e.getKeyCode();
|
||||
// System.out.println("Key Released: " + KeyEvent.getKeyText(keyCode));
|
||||
}
|
||||
|
||||
@@ -477,6 +583,6 @@ public class Calculator extends JFrame implements KeyListener {
|
||||
if (c == '=') {
|
||||
math();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user