Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.swing;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.SwingConstants;
- @SuppressWarnings("serial")
- public class InfixToPostfixCalculator extends JFrame implements ActionListener {
- JFrame frame;
- JTextField textField = new JTextField("0 ");
- Font font = new Font("Orbitron", Font.BOLD, 35);//OrbitronRoboto
- Font btnfont = new Font("Arial", Font.BOLD, 35);
- String[] btnText = new String[] { "1", "2", "3", "C", "4", "5", "6", "+", "7", "8", "9", "-", "*", "0", "/", "=", };
- JButton[] calBtn = new JButton[16];
- JPanel panel = new JPanel();
- // Contructor
- public InfixToPostfixCalculator() {
- frame = new JFrame("Calculator");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(380, 470);
- frame.setLocation(450, 130);
- frame.setLayout(null);
- frame.getContentPane().setBackground(Color.BLACK);
- frame.setResizable(false);
- textField.setFont(font);
- textField.setBounds(5, 10, 353, 50);
- textField.setEditable(false);
- textField.setBackground(Color.WHITE);
- textField.setHorizontalAlignment(JTextField.RIGHT);
- textField.setFocusable(false);
- textField.setForeground(Color.BLACK);
- textField.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1, true));
- frame.add(textField);
- for (int i = 0; i < btnText.length; i++) {
- calBtn[i] = new JButton();
- calBtn[i].setText(btnText[i]);
- calBtn[i].setHorizontalTextPosition(SwingConstants.CENTER);
- calBtn[i].setFont(btnfont);
- calBtn[i].setSize(50, 50);
- calBtn[i].setForeground(Color.BLACK);
- calBtn[i].setFocusable(false);
- calBtn[i].addActionListener(this);
- panel.add(calBtn[i]);
- }
- panel.setBackground(Color.DARK_GRAY);
- panel.setBounds(5, 70, 353, 355);
- panel.setLayout(new GridLayout(4, 3, 10, 12));
- frame.add(panel);
- frame.setVisible(true);
- }
- public static void main(String[] args) {
- new InfixToPostfixCalculator();
- }
- @Override
- public void actionPerformed(ActionEvent event) {
- String input = event.getActionCommand();
- String result = textField.getText();
- switch(input) {
- case "1":case "2":case "3":case "4":case "5":
- case "6":case "7":case "8":case "9":
- result = (result.equals("0 "))? input:result+input;
- textField.setText(result);
- break;
- case"0":
- result = (!result.equals("0 "))? result+input:input;
- textField.setText(result);
- break;
- case "C": textField.setText("0 ");
- break;
- case"*":case"/":case"+":case"-":
- result = result+input;
- textField.setText(result);
- break;
- case"=":
- MyInfixToPostfix itf = new MyInfixToPostfix(result);
- textField.setText(itf.compute()+"");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement