Commit 97498a94 authored by 9531703's avatar 9531703

version 1

parents
Pipeline #48 failed with stages
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class CalculatorGUI implements ActionListener, KeyListener {
JFrame calcFrame;
public JTextArea display;
protected int a, b, cs;
public CalculatorGUI(){
calcFrame = new JFrame();
calcFrame.setTitle("AUT Calculator");
calcFrame.setSize(350, 370);
calcFrame.setLocation(100, 200);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem exitItem = new JMenuItem("Exit"); // create exit item
exitItem.setMnemonic('x');
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
System.exit(0);
}
});
fileMenu.add(exitItem);
menuBar.add(fileMenu);
//calcFrame.add(menuBar);
JPanel keyboardPanel = new JPanel();
keyboardPanel.setLayout(new GridLayout(5,3));
display = new JTextArea(3 , 10);
display.setEditable(false);
display.setToolTipText("for displaying");
display.setFont(new Font("Arial", 14, 14));
display.setBackground(Color.GREEN);
for(int i = 9 ; i > 0 ; i--)
{
JButton btn = new JButton();
btn.setText(""+i);
btn.setToolTipText("number " + i);
btn.setFocusable(true);
btn.addActionListener(this);
btn.addKeyListener(this);
keyboardPanel.add(btn);
}
JButton sumBtn = new JButton();
sumBtn.setText("+");
sumBtn.setToolTipText("btn +");
sumBtn.setFocusable(true);
sumBtn.addKeyListener(this);
sumBtn.addActionListener(this);
keyboardPanel.add(sumBtn);
JButton zeroBtn = new JButton();
zeroBtn.setText("0");
zeroBtn.setToolTipText("number 0");
zeroBtn.setFocusable(true);
zeroBtn.addKeyListener(this);
zeroBtn.addActionListener(this);
keyboardPanel.add(zeroBtn);
JButton doBtn = new JButton();
doBtn.setText("=");
doBtn.setToolTipText("btn =");
doBtn.setFocusable(true);
doBtn.addKeyListener(this);
doBtn.addActionListener(this);
keyboardPanel.add(doBtn);
JButton mulBtn = new JButton();
mulBtn.setText("*");
mulBtn.setToolTipText("btn *");
mulBtn.setFocusable(true);
mulBtn.addKeyListener(this);
mulBtn.addActionListener(this);
keyboardPanel.add(mulBtn);
JButton divideBtn = new JButton();
divideBtn.setText("/");
divideBtn.setToolTipText("btn /");
divideBtn.setFocusable(true);
divideBtn.addKeyListener(this);
divideBtn.addActionListener(this);
keyboardPanel.add(divideBtn);
JButton subBtn = new JButton();
subBtn.setText("-");
subBtn.setToolTipText("btn -");
subBtn.addActionListener(this);
keyboardPanel.add(subBtn);
JPanel totalOne = new JPanel();
totalOne.setSize(350, 370);
totalOne.setLayout(new GridLayout(2,1));
totalOne.add(display);
totalOne.add(keyboardPanel);
JTabbedPane tp = new JTabbedPane();
totalOne.addKeyListener(this);
tp.add("manual", totalOne);
tp.addKeyListener(this);
//--------------------------------------------------------------
JPanel keyboardPanel2 = new JPanel();
keyboardPanel2.setLayout(new GridLayout(6,3));
for(int i = 9 ; i > 0 ; i--)
{
JButton btn = new JButton();
btn.setText(""+i);
keyboardPanel2.add(btn);
}
JButton sumBtn2 = new JButton();
sumBtn2.setText("+");
keyboardPanel2.add(sumBtn2);
JButton zeroBtn2 = new JButton();
zeroBtn2.setText("0");
keyboardPanel2.add(zeroBtn2);
JButton doBtn2 = new JButton();
doBtn2.setText("=");
keyboardPanel2.add(doBtn2);
JButton mulBtn2 = new JButton();
mulBtn2.setText("*");
keyboardPanel2.add(mulBtn2);
JButton divideBtn2 = new JButton();
divideBtn2.setText("=");
keyboardPanel2.add(divideBtn2);
JButton subBtn2 = new JButton();
subBtn2.setText("-");
keyboardPanel2.add(subBtn2);
JButton sinBtn = new JButton();
sinBtn.setText("sin / cos");
keyboardPanel2.add(sinBtn);
JButton tanBtn = new JButton();
tanBtn.setText("tan / cot");
keyboardPanel2.add(tanBtn);
JButton logBtn = new JButton();
logBtn.setText("log / exp");
keyboardPanel2.add(logBtn);
JTextArea display2 = new JTextArea(3 , 10);
display2.setEditable(false);
display2.setFont(new Font("Arial", 14, 14));
display2.setBackground(Color.GREEN);
JButton shBtn = new JButton("Shift");
shBtn.setBackground(Color.RED);
JPanel totalTwo = new JPanel();
totalTwo.setLayout(new GridLayout(3,1));
totalTwo.add(display2);
totalTwo.add(keyboardPanel2);
totalTwo.add(shBtn);
tp.add("engenering", totalTwo);
calcFrame.add(menuBar, BorderLayout.NORTH);
calcFrame.add(tp, BorderLayout.CENTER);
//calcFrame.setLayout(null);
calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calcFrame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
String s = btn.getText();
if(s != "+" && s != "-" && s != "/" && s != "=" && s != "*")
{
display.setText(display.getText() + s);
}
switch(s){
case "+":
a = Integer.parseInt(display.getText());
display.setText("");
cs = 1;
break;
case "-":
a = Integer.parseInt(display.getText());
display.setText("");
cs = 2;
break;
case "/":
a = Integer.parseInt(display.getText());
display.setText("");
cs = 3;
break;
case "*":
a = Integer.parseInt(display.getText());
display.setText("");
cs = 4;
break;
case "=":
b = Integer.parseInt(display.getText());
if(cs == 1)
display.setText("" + (a + b));
else if(cs == 2)
display.setText("" + (a - b));
else if (cs == 3)
display.setText("" + (a / b));
else if (cs == 4)
display.setText("" + (a * b));
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
int kRealeased = e.getKeyCode();
switch (kRealeased){
case KeyEvent.VK_NUMPAD1:
display.setText(display.getText()+"1");
break;
case KeyEvent.VK_NUMPAD2:
display.setText(display.getText()+"2");
break;
case KeyEvent.VK_NUMPAD3:
display.setText(display.getText()+"3");
break;
case KeyEvent.VK_NUMPAD4:
display.setText(display.getText()+"4");
break;
case KeyEvent.VK_NUMPAD5:
display.setText(display.getText()+"5");
break;
case KeyEvent.VK_NUMPAD6:
display.setText(display.getText()+"6");
break;
case KeyEvent.VK_NUMPAD7:
display.setText(display.getText()+"7");
break;
case KeyEvent.VK_NUMPAD8:
display.setText(display.getText()+"8");
break;
case KeyEvent.VK_NUMPAD9:
display.setText(display.getText()+"9");
break;
case KeyEvent.VK_NUMPAD0:
display.setText(display.getText()+"0");
break;
case KeyEvent.VK_ADD:
a = Integer.parseInt(display.getText());
display.setText("");
cs = 1;
break;
case KeyEvent.VK_SUBTRACT:
a = Integer.parseInt(display.getText());
display.setText("");
cs = 2;
break;
case KeyEvent.VK_DIVIDE:
a = Integer.parseInt(display.getText());
display.setText("");
cs = 3;
break;
case KeyEvent.VK_MULTIPLY:
a = Integer.parseInt(display.getText());
display.setText("");
cs = 4;
break;
case KeyEvent.VK_EQUALS:
b = Integer.parseInt(display.getText());
if(cs == 1)
display.setText("" + (a + b));
else if(cs == 2)
display.setText("" + (a - b));
else if (cs == 3)
display.setText("" + (a / b));
else if (cs == 4)
display.setText("" + (a * b));
break;
}
}
}
package com.company;
public class Main {
public static void main(String[] args) {
CalculatorGUI calculator = new CalculatorGUI();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment