Написал калькулятор, но он не работает - Java
Формулировка задачи:
Нужно сделать калькулятор на Java, делаю-делаю - не делается :с
Пишу в Ecpipse
Пишу неделю наверно, не совсем ещё разобрался.
Помогите пожалуйста, заранее спасибо
Вот весь код
1 скрин - ошибка
2 скрин вкладки Design
Листинг программы
- package plus;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class plusFrame extends JFrame {
- private JTextField textField;
- private JTextField display;
- public plusFrame() {
- getContentPane().setLayout(null);
- display = new JTextField();
- display.setBounds(0, 11, 258, 20);
- getContentPane().add(display);
- display.setColumns(10);
- JPanel buttonpanel = new JPanel();
- buttonpanel.setBounds(0, 48, 258, 246);
- getContentPane().add(buttonpanel);
- buttonpanel.setLayout(null);
- JButton button0 = new JButton("0");
- button0.setBounds(10, 212, 89, 23);
- buttonpanel.add(button0);
- JButton button1 = new JButton("1");
- button1.setBounds(10, 178, 39, 23);
- buttonpanel.add(button1);
- JButton button2 = new JButton("2");
- button2.setBounds(58, 178, 39, 23);
- buttonpanel.add(button2);
- JButton button3 = new JButton("3");
- button3.setBounds(107, 178, 39, 23);
- buttonpanel.add(button3);
- JButton button4 = new JButton("4");
- button4.setBounds(10, 144, 39, 23);
- buttonpanel.add(button4);
- JButton button5 = new JButton("5");
- button5.setBounds(58, 144, 39, 23);
- buttonpanel.add(button5);
- JButton button6 = new JButton("6");
- button6.setBounds(107, 144, 39, 23);
- buttonpanel.add(button6);
- JButton button7 = new JButton("7");
- button7.setBounds(10, 110, 39, 23);
- buttonpanel.add(button7);
- JButton button8 = new JButton("8");
- button8.setBounds(58, 110, 39, 23);
- buttonpanel.add(button8);
- JButton button9 = new JButton("9");
- button9.setBounds(107, 110, 39, 23);
- buttonpanel.add(button9);
- JButton buttonSum = new JButton("+");
- buttonSum.setBounds(149, 212, 41, 23);
- buttonpanel.add(buttonSum);
- JButton buttonBack = new JButton("C");
- buttonBack.setBounds(107, 77, 39, 23);
- buttonpanel.add(buttonBack);
- JButton buttonDivide = new JButton("/");
- buttonDivide.setBounds(149, 110, 41, 23);
- buttonpanel.add(buttonDivide);
- JButton buttonSub = new JButton("-");
- buttonSub.setBounds(149, 178, 41, 23);
- buttonpanel.add(buttonSub);
- JButton buttonMul = new JButton("*");
- buttonMul.setBounds(149, 144, 41, 23);
- buttonpanel.add(buttonMul);
- JButton buttonStart = new JButton("=");
- buttonStart.setBounds(193, 178, 55, 57);
- buttonpanel.add(buttonStart);
- //
- int firstValue = 0;
- String operation = "+";
- button0.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText()+"0");
- }
- });
- button1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText()+"1");
- }
- });
- button2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText()+"2");
- }
- });
- button3.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText()+"3");
- }
- });
- button4.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText()+"4");
- }
- });
- button5.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText()+"5");
- }
- });
- button6.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText()+"6");
- }
- });
- button7.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText()+"7");
- }
- });
- button8.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText()+"8");
- }
- });
- button9.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- display.setText(display.getText()+"9");
- }
- });
- buttonBack.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String temp = display.getText();
- display.setText(temp.substring(0,temp.length()-1));
- }
- });
- buttonSum.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- firstValue = Integer.valueOf(display.getText());
- display.setText("");
- operation = "+";
- }
- });
- buttonMul.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- firstValue = Integer.valueOf(display.getText());
- display.setText("");
- operation = "*";
- }
- });
- buttonDivide.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- firstValue = Integer.valueOf(display.getText());
- display.setText("");
- operation = "/";
- }
- });
- buttonSub.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- firstValue = Integer.valueOf(display.getText());
- display.setText("");
- operation = "-";
- }
- });
- buttonStart.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int secondValue = Integer.valueOf(display.getText());
- if("+".equals(operation)){
- display.setText((firstValue+secondValue)+"");
- }
- if("-".equals(operation)){
- display.setText((firstValue-secondValue)+"");
- }
- if("*".equals(operation)){
- display.setText((firstValue*secondValue)+"");
- }
- if("/".equals(operation)){
- display.setText((firstValue/secondValue)+"");
- }
- firstValue = 0;
- operation = "+";
- }
- });
- //
- }
- public static void main(String[] args) {
- new plusFrame();
- }
- }
Решение задачи: «Написал калькулятор, но он не работает»
textual
Листинг программы
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- class CalcFrame extends JFrame {
- CalcController controller;
- private TextField display;
- public CalcFrame() throws HeadlessException {
- setContentPane(constructContent());
- pack();
- }
- private Container constructContent() {
- this.setTitle("Calc");
- this.display = new TextField(20);
- JPanel root = new JPanel();
- JPanel buttonsPanel = new JPanel();
- JPanel digitPanel = new JPanel(new GridLayout(4, 3));
- JPanel operationPanel = new JPanel(new GridLayout(4, 1));
- String[] buttonNames = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "C", "="};
- for (String s : buttonNames) {
- digitPanel.add(createButton(s));
- }
- String[] operations = {"+", "-", "*", "/"};
- for (String s : operations) {
- operationPanel.add(createButton(s));
- }
- buttonsPanel.setLayout((new BoxLayout(buttonsPanel, BoxLayout.X_AXIS)));
- buttonsPanel.add(digitPanel);
- buttonsPanel.add(operationPanel);
- root.setLayout((new BoxLayout(root, BoxLayout.Y_AXIS)));
- root.add(this.display);
- root.add(buttonsPanel);
- return root;
- }
- private Component createButton(final String s) {
- Button button = new Button(s);
- button.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- controller.pushButton(s);
- }
- });
- return button;
- }
- public void setController(CalcController controller) {
- this.controller = controller;
- }
- public void update(String data) {
- display.setText(data);
- }
- public void start() {
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- setVisible(true);
- }
- }
- class CalcController {
- CalcData data;
- public void setData(CalcData data) {
- this.data = data;
- }
- public void pushButton(String buttonValue) {
- if (hasError(data.getData(), buttonValue)) {
- return;
- }
- if (buttonValue.equals("=")) {
- String result = calculateExpression(data.getData());
- data.setData(result);
- return;
- }
- if (buttonValue.equals("C")) {
- data.setData("");
- return;
- }
- StringBuilder sb = new StringBuilder();
- sb.append(data.getData());
- sb.append(buttonValue);
- data.setData(sb.toString());
- }
- private boolean hasError(String data, String buttonValue) {
- //тут добавить проверку что бы нельзя было ввести 2 символа операций подряд
- //например 2+-2
- return false;
- }
- private String calculateExpression(String data) {
- //Тут (или вынести в отдельный класс) логика вычисления выражения
- //Гуглить - "Обратная польская нотация"
- return "метод не реализован";
- }
- }
- class CalcData {
- CalcFrame view;
- String data = new String();
- public void setView(CalcFrame view) {
- this.view = view;
- }
- public String getData() {
- return data;
- }
- public void setData(String data) {
- this.data = data;
- notifyView();
- }
- private void notifyView() {
- view.update(getData());
- }
- }
- public class Calculator {
- public static void main(String[] args) {
- CalcFrame frame = new CalcFrame();
- CalcController controller = new CalcController();
- CalcData data = new CalcData();
- frame.setController(controller);
- controller.setData(data);
- data.setView(frame);
- frame.start();
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д