Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JTextField;
- import java.awt.FlowLayout;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.JTextPane;
- public class CalFrame extends JFrame {
- private JPanel contentPane;
- private JTextField calText;
- String dis = "";
- Boolean addbol = false;
- Boolean subbol = false;
- Boolean eqlbol = false;
- private int TEMP;
- private int TEMP2;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- CalFrame frame = new CalFrame();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public CalFrame() {
- setTitle("Calculator");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 400, 527);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- calText = new JTextField();
- calText.setBounds(10, 11, 364, 80);
- contentPane.add(calText);
- calText.setColumns(10);
- JButton button1 = new JButton("1");
- button1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- }
- });
- button1.setBounds(10, 140, 100, 50);
- contentPane.add(button1);
- JButton button2 = new JButton("2");
- button2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- }
- });
- button2.setBounds(140, 140, 100, 50);
- contentPane.add(button2);
- JButton button3 = new JButton("3");
- button3.setBounds(274, 140, 100, 50);
- contentPane.add(button3);
- JButton button4 = new JButton("4");
- button4.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- }
- });
- button4.setBounds(10, 210, 100, 50);
- contentPane.add(button4);
- JButton button5 = new JButton("5");
- button5.setBounds(140, 210, 100, 50);
- contentPane.add(button5);
- JButton button6 = new JButton("6");
- button6.setBounds(274, 210, 100, 50);
- contentPane.add(button6);
- JButton button7 = new JButton("7");
- button7.setBounds(10, 280, 100, 50);
- contentPane.add(button7);
- JButton button8 = new JButton("8");
- button8.setBounds(140, 280, 100, 50);
- contentPane.add(button8);
- JButton button9 = new JButton("9");
- button9.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- }
- });
- button9.setBounds(274, 280, 100, 50);
- contentPane.add(button9);
- JButton buttonPls = new JButton("+");
- buttonPls.setBounds(10, 350, 100, 50);
- contentPane.add(buttonPls);
- JButton button0 = new JButton("0");
- button0.setBounds(140, 350, 100, 50);
- contentPane.add(button0);
- JButton buttonMin = new JButton("-");
- buttonMin.setBounds(274, 350, 100, 50);
- contentPane.add(buttonMin);
- JButton buttonEql = new JButton("=");
- buttonEql.setBounds(10, 420, 364, 50);
- contentPane.add(buttonEql);
- button1.addActionListener(new Listen1());
- button2.addActionListener(new Listen2());
- button3.addActionListener(new Listen3());
- button4.addActionListener(new Listen4());
- button5.addActionListener(new Listen5());
- button6.addActionListener(new Listen6());
- button7.addActionListener(new Listen7());
- button8.addActionListener(new Listen8());
- button9.addActionListener(new Listen9());
- button0.addActionListener(new Listen0());
- buttonPls.addActionListener(new ListenPls());
- buttonMin.addActionListener(new ListenMin());
- buttonEql.addActionListener(new ListenEql());
- }
- class Clearr implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- calText.setText("");
- addbol = false;
- subbol = false;
- TEMP = 0;
- TEMP2 = 0;
- }
- }
- class Listen1 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- dis = calText.getText();
- calText.setText(dis + "1");
- }}
- class Listen2 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- dis = calText.getText();
- calText.setText(dis + "2");
- }}
- class Listen3 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- dis = calText.getText();
- calText.setText(dis + "3");
- }}
- class Listen4 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- dis = calText.getText();
- calText.setText(dis + "4");
- }}
- class Listen5 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- dis = calText.getText();
- calText.setText(dis + "5");
- }}
- class Listen6 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- dis = calText.getText();
- calText.setText(dis + "6");
- }}
- class Listen7 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- dis = calText.getText();
- calText.setText(dis + "7");
- }}
- class Listen8 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- dis = calText.getText();
- calText.setText(dis + "8");
- }}
- class Listen9 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- dis = calText.getText();
- calText.setText(dis + "9");
- }}
- class Listen0 implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- dis = calText.getText();
- calText.setText(dis + "0");
- }}
- class ListenPls implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- TEMP = Integer.parseInt(calText.getText());
- dis = calText.getText();
- calText.setText(dis + "+");
- addbol = true;
- }}
- class ListenMin implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- TEMP = Integer.parseInt(calText.getText());
- dis = calText.getText();
- calText.setText(dis + "-");
- subbol = true;
- }}
- class ListenEql implements ActionListener{
- @Override
- public void actionPerformed(ActionEvent e) {
- // TODO Auto-generated method stub
- TEMP2 = Integer.parseInt(calText.getText());
- if (subbol == true)
- {
- if(calText.getText().contains("-"))
- {
- String[] splt = calText.getText().split("-");
- String prt1 = splt[0];
- String prt2 = splt[1];
- TEMP2=Integer.parseInt(prt1)-Integer.parseInt(prt2);
- TEMP2=TEMP2+TEMP;
- calText.setText(dis+Integer.toString(TEMP2));
- }
- }
- else if ( addbol == true)
- {
- if(calText.getText().contains("+"))
- {
- String[] splt = calText.getText().split("+");
- String prt1 = splt[0];
- String prt2 = splt[1];
- TEMP2=Integer.parseInt(prt1)+Integer.parseInt(prt2);
- TEMP2=TEMP2+TEMP;
- calText.setText(dis+Integer.toString(TEMP2));
- }
- }
- /*TEMP2= Integer.parseInt(calText.getText());
- if (addbol == true)
- {
- TEMP2 = TEMP2 + TEMP;
- }
- else if ( subbol == true)
- {
- TEMP2 = TEMP2 - TEMP;
- }
- calText.setText(Integer.toString(TEMP2));
- addbol = false;
- subbol = false;
- */
- subbol = false;
- addbol = false;
- }}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement