Калькулятор ax2+bx+c - Java
Формулировка задачи:
У меня есть код, не могу понять почему не работает кнопка 1(b1), не считает, хотя кнопка2(b2) работает, прошу о помощи,
не пойму, что не так?
Листинг программы
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class Main extends JFrame {
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- Main window = new Main("Calculator");
- window.f1.setVisible(true);
- }
- });
- }
- JButton b1, b2;
- JTextField t1, t2, t3;
- JLabel l1;
- JFrame f1;
- double a, b, c, d, x1, x2;
- String s1, s2, s3;
- eHandler handler = new eHandler();
- public Main(String s) {
- super(s);
- f1 = new JFrame();
- f1.setBounds(100, 100, 300, 200);
- f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f1.getContentPane().setLayout(null);
- b1 = new JButton("Solve");
- b1.setBounds(0, 61, 284, 50);
- f1.getContentPane().add(b1);
- t1 = new JTextField();
- t1.setColumns(10);
- t1.setBounds(0, 39, 94, 20);
- f1.getContentPane().add(t1);
- b2 = new JButton("Clear");
- b2.setBounds(0, 112, 284, 50);
- f1.getContentPane().add(b2);
- t2 = new JTextField();
- t2.setColumns(10);
- t2.setBounds(96, 39, 94, 20);
- f1.getContentPane().add(t2);
- t3 = new JTextField();
- t3.setColumns(10);
- t3.setBounds(192, 39, 94, 20);
- f1.getContentPane().add(t3);
- JLabel l1 = new JLabel();
- l1.setBounds(3, 0, 281, 39);
- f1.getContentPane().add(l1);
- b1.addActionListener(handler);
- b2.addActionListener(handler);
- }
- public class eHandler implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == b1) {
- a = Double.parseDouble(t1.getText());
- b = Double.parseDouble(t2.getText());
- c = Double.parseDouble(t3.getText());
- d = (b*b) - (4*a*c);
- x1 = ((-b + Math.sqrt(d))/(2*a));
- x2 = ((-b - Math.sqrt(d))/(2*a));
- s1 = "x1 = " + x1;
- s2 = "x2 = " + x2;
- s3 = s1 + " " + s2;
- l1.setText(s3);
- }
- if (e.getSource() == b2) {
- t1.setText(null);
- t2.setText(null);
- t3.setText(null);
- l1.setText(null);
- }
- }
- }
- }
Решение задачи: «Калькулятор ax2+bx+c»
textual
Листинг программы
- l1 = new JLabel();
- l1.setBounds(3, 0, 281, 39);
- f1.getContentPane().add(l1);
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д