Advertisement
Kulas_Code20

JAVA GUI CAL

Oct 6th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package com.swing;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.GridLayout;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.BorderFactory;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12. import javax.swing.JTextField;
  13. import javax.swing.SwingConstants;
  14.  
  15. @SuppressWarnings("serial")
  16. public class InfixToPostfixCalculator extends JFrame implements ActionListener {
  17.  
  18.     JFrame frame;
  19.     JTextField textField = new JTextField("0 ");
  20.     Font font = new Font("Orbitron", Font.BOLD, 35);//OrbitronRoboto
  21.     Font btnfont = new Font("Arial", Font.BOLD, 35);
  22.     String[] btnText = new String[] { "1", "2", "3", "C", "4", "5", "6", "+", "7", "8", "9", "-", "*", "0", "/", "=", };
  23.     JButton[] calBtn = new JButton[16];
  24.     JPanel panel = new JPanel();
  25.  
  26.     // Contructor
  27.     public InfixToPostfixCalculator() {
  28.         frame = new JFrame("Calculator");
  29.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.         frame.setSize(380, 470);
  31.         frame.setLocation(450, 130);
  32.         frame.setLayout(null);
  33.         frame.getContentPane().setBackground(Color.BLACK);
  34.         frame.setResizable(false);
  35.  
  36.         textField.setFont(font);
  37.         textField.setBounds(5, 10, 353, 50);
  38.         textField.setEditable(false);
  39.         textField.setBackground(Color.WHITE);
  40.         textField.setHorizontalAlignment(JTextField.RIGHT);
  41.         textField.setFocusable(false);
  42.         textField.setForeground(Color.BLACK);
  43.         textField.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1, true));
  44.         frame.add(textField);
  45.  
  46.         for (int i = 0; i < btnText.length; i++) {
  47.             calBtn[i] = new JButton();
  48.             calBtn[i].setText(btnText[i]);
  49.             calBtn[i].setHorizontalTextPosition(SwingConstants.CENTER);
  50.             calBtn[i].setFont(btnfont);
  51.             calBtn[i].setSize(50, 50);
  52.             calBtn[i].setForeground(Color.BLACK);
  53.             calBtn[i].setFocusable(false);
  54.             calBtn[i].addActionListener(this);
  55.             panel.add(calBtn[i]);
  56.         }
  57.         panel.setBackground(Color.DARK_GRAY);
  58.         panel.setBounds(5, 70, 353, 355);
  59.         panel.setLayout(new GridLayout(4, 3, 10, 12));
  60.         frame.add(panel);
  61.         frame.setVisible(true);
  62.     }
  63.  
  64.     public static void main(String[] args) {
  65.         new InfixToPostfixCalculator();
  66.  
  67.     }
  68.  
  69.     @Override
  70.     public void actionPerformed(ActionEvent event) {
  71.         String input = event.getActionCommand();
  72.         String result = textField.getText();
  73.         switch(input) {
  74.             case "1":case "2":case "3":case "4":case "5":
  75.             case "6":case "7":case "8":case "9":
  76.                 result = (result.equals("0 "))? input:result+input;
  77.                 textField.setText(result);
  78.                 break;
  79.             case"0":
  80.                 result = (!result.equals("0 "))? result+input:input;
  81.                 textField.setText(result);
  82.                 break;
  83.             case "C": textField.setText("0 ");
  84.                 break;
  85.             case"*":case"/":case"+":case"-":
  86.                 result = result+input;
  87.                 textField.setText(result);
  88.                 break;
  89.             case"=":
  90.                 MyInfixToPostfix itf = new MyInfixToPostfix(result);
  91.                 textField.setText(itf.compute()+"");
  92.                 break;
  93.         }
  94.     }
  95.  
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement