ActionListener - Java (250426)

Узнай цену своей работы

Формулировка задачи:

Добрый день. Недавно занялся самостоятельным изучением Java. Прошу строго не судить, что создал новую тему. Не поможете с ActionListener. Пишу калькулятор и у меня не считывает ActionListener нажатия на JButton. Прошу помощи.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
 
public class C1 extends JFrame {
    JButton b[] = new JButton[10];
    JButton b0 = new JButton("0");
    JButton b1 = new JButton("+");
    JTextField t = new JTextField();
    JButton bb;
 
    public C1(String name) {
        super("Калькулятор");
        setLayout(null);
        setSize(300, 400);
        t.setBounds(10, 10, 260, 50);
        t.setEditable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        b0.setBounds(60, 220, 50, 50);
        add(b0);
        add(t);
 
        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
                b[x * 3 + y + 1] = new JButton((x * 3 + y + 1) + "");
                b[x * 3 + y + 1].setBounds(y * 50 + 10, x * 50 + 70, 50, 50);
                add(b[x * 3 + y + 1]);
 
            }
        }
        ActionListener l = new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton bb = (JButton) e.getSource();
                t.setText(t.getText() + t.getBounds());
 
            }
        };
        for (JButton bb : b) {
            bb.addActionListener(l);
 
        }
 
    }
}

Решение задачи: «ActionListener»

textual
Листинг программы
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
public class C1 extends JFrame {
    private JButton b[] = new JButton[10];
    private JButton b0 = new JButton("0");
    JButton b1 = new JButton("+");
    private JTextField t = new JTextField();
    JButton bb;
 
    public C1(String name) {
        super("Калькулятор");
        setLayout(null);
        setSize(300, 400);
        t.setBounds(10, 10, 260, 50);
        t.setEditable(false);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        b0.setBounds(60, 220, 50, 50);
        add(b0);
        add(t);
 
        ActionListener l = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton bb = (JButton) e.getSource();
                t.setText(t.getText() + t.getBounds());
            }
        };
        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
                b[x * 3 + y + 1] = new JButton((x * 3 + y + 1) + "");
                b[x * 3 + y + 1].setBounds(y * 50 + 10, x * 50 + 70, 50, 50);
                b[x * 3 + y + 1].addActionListener(l);
                add(b[x * 3 + y + 1]);
 
            }
        }
    }
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 3.8 из 5