Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.TextComponent.*;
- import java.awt.event.TextListener.*;
- class LabelExample {
- public static void main(String args[]) {
- Frame f = new Frame("Calculator");
- f.setSize(400, 450);
- Button b1 = new Button("1");
- Button b2 = new Button("2");
- Button b3 = new Button("3");
- Button b4 = new Button("4");
- Button b5 = new Button("5");
- Button b6 = new Button("6");
- Button b7 = new Button("7");
- Button b8 = new Button("8");
- Button b9 = new Button("9");
- Button plus = new Button("+");
- //Button minus = new Button("-");
- plus.setBounds(70, 345, 70, 50);
- f.add(plus);
- // minus.setBounds(270, 345, 70, 50);
- // f.add(minus);
- b1.setBounds(70, 105, 70, 50);
- f.add(b1);
- b2.setBounds(170, 105, 70, 50);
- f.add(b2);
- b3.setBounds(270, 105, 70, 50);
- f.add(b3);
- b4.setBounds(70, 185, 70, 50);
- f.add(b4);
- b5.setBounds(170, 185, 70, 50);
- f.add(b5);
- b6.setBounds(270, 185, 70, 50);
- f.add(b6);
- b7.setBounds(70, 265, 70, 50);
- f.add(b7);
- b8.setBounds(170, 265, 70, 50);
- f.add(b8);
- b9.setBounds(270, 265, 70, 50);
- f.add(b9);
- Button ednakvo = new Button("=");
- ednakvo.setBounds(170, 345, 70, 50);
- f.add(ednakvo);
- TextField t1, t2;
- t2 = new TextField();
- t2.setText("0");
- t2.setVisible(false);
- t1 = new TextField();
- t1.setBounds(70, 50, 270, 25);
- f.add(t1);
- t2.setBounds(70, 150, 70, 25);
- f.add(t2);
- b1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String a = t1.getText();
- t1.setText(a + "1");
- }
- });
- b2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String a = t1.getText();
- t1.setText(a + "2");
- }
- });
- b3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String a = t1.getText();
- t1.setText(a + "3");
- }
- });
- b4.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String a = t1.getText();
- t1.setText(a + "4");
- }
- });
- b5.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String a = t1.getText();
- t1.setText(a + "5");
- }
- });
- b6.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String a = t1.getText();
- t1.setText(a + "7");
- }
- });
- b7.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String a = t1.getText();
- t1.setText(a + "7");
- }
- });
- b8.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String a = t1.getText();
- t1.setText(a + "8");
- }
- });
- b9.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String a = t1.getText();
- t1.setText(a + "9");
- }
- });
- plus.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String a = t1.getText();
- t1.setText("");
- int a1 = Integer.parseInt(a);
- String b = t2.getText();
- int b1 = Integer.parseInt(b);
- int c1 = a1 + b1;
- String c = Integer.toString(c1);
- t2.setText(c);
- }
- });
- ednakvo.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String a = t1.getText();
- int a1 = Integer.parseInt(a);
- String b = t2.getText();
- int b1 = Integer.parseInt(b);
- int c1 = a1 + b1;
- String c = Integer.toString(c1);
- t1.setText(c);
- t2.setText("0");
- }
- });
- f.setLayout(null);
- f.setVisible(true);
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- super.windowClosing(e);
- System.exit(0);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement