Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

«Проект калькулятор в Java»



Реферат на тему:

«Проект калькулятор в Java»

 

 

Давыдов Иван 11ТО

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class MyCalc extends JFrame {

private JButton display;

private double result;

private String lastCom;

private boolean start;

 

private void addButton(String name, ActionListener listener, int x, int y,

int width, int height) {

JButton button = new JButton(name);

button.setBounds(x, y, width, height);

button.addActionListener(listener);

getContentPane().add(button);

}

 

public MyCalc() {

getContentPane().setLayout(null);

setTitle("Мой калькулятор");

setBounds(200, 200, 203, 242);

getContentPane().setBackground(new Color(100, 149, 237));

 

ActionListener output = new OutputAction();

ActionListener command = new CommandAction();

 

result = 0;

lastCom = "=";

start = true;

 

display = new JButton("0");

display.setEnabled(false);

 

display.setBounds(5, 10, 177, 30);

 

addButton("7", output, 5, 50, 41, 20);

addButton("8", output, 50, 50, 41, 20);

addButton("9", output, 95, 50, 41, 20);

addButton("/", command, 140, 50, 41, 20);

 

addButton("4", output, 5, 75, 41, 20);

addButton("5", output, 50, 75, 41, 20);

addButton("6", output, 95, 75, 41, 20);

addButton("*", command, 140, 75, 41, 20);

 

addButton("1", output, 5, 100, 41, 20);

addButton("2", output, 50, 100, 41, 20);

addButton("3", output, 95, 100, 41, 20);

addButton("-", command, 140, 100, 41, 20);

 

addButton("0", output, 5, 125, 41, 20);

addButton(".", output, 50, 125, 41, 20);

addButton("=", command, 95, 125, 41, 20);

addButton("+", command, 140, 125, 41, 20);

 

addButton("с", command, 5, 150, 85, 20);

addButton("<", command, 95, 150, 85, 20);

 

addButton("sqrt", command, 5, 175, 85, 20);

addButton("pow(x,2)", command, 95, 175, 85, 20);

 

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);

TextEvent t = new TextEvent(getContentPane(), 1);

}

 

private class OutputAction implements ActionListener {

public void actionPerformed(ActionEvent event) {

String input = event.getActionCommand();

if (start) {

display.setText("");

start = false;

}

display.setText(display.getText() + input);

}

}

 

private class CommandAction implements ActionListener {

public void actionPerformed(ActionEvent event) {

String command = event.getActionCommand();

if (start) {

if (command.equals("-")) {

display.setText(command);

start = false;

} else {

lastCom = command;

}

}else if (command.equals("sqrt")) {

display.setText("" + Math.sqrt(Double.parseDouble(display.getText())));

start = true;

} else if (command.equals("pow(x,2)")) {

display.setText("" + Math.pow(Double.parseDouble(display.getText()), 2));

start = true;

}else if (command.equals("с")) {

display.setText("");

start = true;

}else if (command.equals("<")) {

//???

} else {

calculate(Double.parseDouble(display.getText()));

lastCom = command;

start = true;

}

}

}

 

public void calculate(double x) {

if (lastCom.equals("+")) {

result += x;

display.setText("" + result);

} else if (lastCom.equals("-")) {

result -= x;

display.setText("" + result);

} else if (lastCom.equals("*")) {

result *= x;

display.setText("" + result);

} else if (lastCom.equals("/")) {

if (x == 0) {

display.setText("Деление на ноль");

} else {

result /= x;

display.setText("" + result);

}

} else if (lastCom.equals("=")) {

result = x;

display.setText("" + result);

}

}

public static void main(String[] args) {



new MyCalc();

}

}

 

Результат:


Дата добавления: 2015-09-28; просмотров: 34 | Нарушение авторских прав




<== предыдущая лекция | следующая лекция ==>
 | 

mybiblioteka.su - 2015-2024 год. (0.008 сек.)