One by one. Who is not too lazy to read the code . I think the idea is pretty clear (a simple calculator).

import java.awt.FlowLayout; import javax.swing.*; public class calc_97_lvl { public static JLabel jlab; calc_97_lvl() { ListenCalc LC = new ListenCalc(); JFrame jfrm = new JFrame("Калькулятор Кузнецова"); jfrm.setSize(185, 157); jfrm.setLayout(new FlowLayout()); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jfrm.setVisible(true); JLabel jlab = new JLabel(); jlab.setText("egeherjrj!"); jlab.setSize(150, 150); JButton but1 = new JButton("+"); JButton but2 = new JButton("-"); JButton but3 = new JButton("*"); JButton but4 = new JButton("/"); JButton but5 = new JButton("."); JButton but6 = new JButton("e"); JButton but7 = new JButton("="); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); JButton b5 = new JButton("5"); JButton b6 = new JButton("6"); JButton b7 = new JButton("7"); JButton b8 = new JButton("8"); JButton b9 = new JButton("9"); JButton b0 = new JButton("0"); but1.addActionListener(LC); but2.addActionListener(LC); but3.addActionListener(LC); but4.addActionListener(LC); but5.addActionListener(LC); but6.addActionListener(LC); but7.addActionListener(LC); b1.addActionListener(LC); b2.addActionListener(LC); b3.addActionListener(LC); b4.addActionListener(LC); b5.addActionListener(LC); b6.addActionListener(LC); b7.addActionListener(LC); b8.addActionListener(LC); b9.addActionListener(LC); b0.addActionListener(LC); jfrm.add(jlab); jfrm.add(b1); jfrm.add(b2); jfrm.add(b3); jfrm.add(but1); jfrm.add(b4); jfrm.add(b5); jfrm.add(b6); jfrm.add(but2); jfrm.add(b7); jfrm.add(b8); jfrm.add(b9); jfrm.add(but3); jfrm.add(but5); jfrm.add(b0); jfrm.add(but6); jfrm.add(but7); } public void set_jlab(String newText) { jlab.setText(newText); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new calc_97_lvl(); } }); } } import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.StringTokenizer; public class ListenCalc implements ActionListener { double numberResult; public void actionPerformed(ActionEvent ae) { resultGiver(ae.getActionCommand()); } public double resultGiver(String textResult) { StringTokenizer ST = new StringTokenizer(textResult, "+-*/e", true); String s1 = ST.nextToken(); String s2 = ST.nextToken(); String s3 = ST.nextToken(); double op1 = Double.parseDouble(s1); double op2 = Double.parseDouble(s3); if (s2.equals("+")) { numberResult = op1 + op2; } else if (s2.equals("-")) { numberResult = op1 - op2; } else if (s2.equals("*")) { numberResult = op1 * op2; } else if (s2.equals("/")) { numberResult = op1 / op2; } else {} return 0; } } 
  • 2
    But the essence of the problem is what? if those errors are below the code. then everything is simple. all that is below line 96 must be placed in a separate file - jmu
  • one
    laziness, really laziness - Barmaley
  • Why is there laziness? Everything in Eclipse is correct and does not work. How, after processing the buttons, are they again transferred to the user? - BubbleGum


1 answer 1

Che something you have screwed up already scary to look at the code.

  1. On a fig all stuffed into the designer of your class?
  2. On the fig, the constructor call was also pushed into the asynchronous call of the window system message handler.

It is necessary to do everything easier and more wooden - we thought too much and did not read much of the documentation.

Take this example as a basis, and if you don’t have enough wisdom here

  • Thank you .. - BubbleGum