【问题标题】:Java Calculator buttonsJava 计算器按钮
【发布时间】:2022-11-22 17:01:05
【问题描述】:

我在 Java 中有一个计算器界面,帮助我将按钮绑定到操作并使其工作! 我很不擅长这门编程语言,很多东西也不懂,所以经常要上网看看。我在那里没有找到解决问题的方法,但我自己没有成功

代码:

package com.myfile;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Objects;

public class Panel extends JPanel {

    private JButton numbers[] = new JButton[10];
    private Font font = new Font("SanSerif", Font.BOLD, 16);
    private JTextField output = new JTextField();
    private JButton clear = new JButton("AC");
    private JButton plus = new JButton("+");
    private JButton minus = new JButton("-");
    private JButton multiply = new JButton("*");
    private JButton devide = new JButton("/");

    private JButton equall = new JButton("=");
    public Panel(){
        setLayout(null);
        setFocusable(true);
        grabFocus();

        numbers[0] = new JButton("0");
        numbers[0].setBounds(75, 70 , 50, 50);
        numbers[0].setFont(font);
        add(numbers[0]);

        for(int x = 0; x < 3; x++){
            for(int y = 0; y < 3; y++){
                numbers[y * 3 + x + 1] = new JButton((y * 3 + x + 1) + "");
                numbers[y * 3 + x + 1].setBounds(x * (50 + 10) + 15, y * (50 + 10) + 130, 50, 50);
                numbers[y * 3 + x + 1].setFont(font);
                add(numbers[y * 3 + x + 1]);
            }
        }

        output.setBounds(10, 10, 250, 50);
        output.setEditable(false);
        output.setFont(font);
        add(output);

        ActionListener l = (ActionEvent event) -> {
            JButton c = (JButton)event.getSource();

            output.setText(output.getText() + c.getText());
        };

        for(JButton c : numbers){
            c.addActionListener(l);
        }

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                char symbol = e.getKeyChar();

                if(!Character.isDigit(symbol))
                    return;
                output.setText(output.getText() + symbol);
            }
        });

        clear.setBounds(15, 70, 50, 50);
        clear.setActionCommand("");
        add(clear);


        equall.setBounds(135, 70, 50, 50);
        equall.setActionCommand("");
        equall.setFont(font);
        add(equall);

        plus.setBounds(195, 70, 60, 50);
        plus.setActionCommand("+");
        plus.setFont(font);
        add(plus);

        minus.setBounds(195, 130, 60, 50);
        minus.setActionCommand("-");
        minus.setFont(font);
        add(minus);

        multiply.setBounds(195, 190, 60, 50);
        multiply.setActionCommand("*");
        multiply.setFont(font);
        add(multiply);

        devide.setBounds(195, 250, 60, 50);
        devide.setActionCommand("*");
        devide.setFont(font);
        add(devide);

        class KeyDispatcher implements KeyEventDispatcher {
            public boolean dispatchKeyEvent(KeyEvent e) {

                return true;
            }
        };
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clear.addActionListener(this);
                equall.addActionListener(this);
                plus.addActionListener(this);
                minus.addActionListener(this);
                multiply.addActionListener(this);
                devide.addActionListener(this);
                output.setText(e.getActionCommand());

            }

        };
    };
}

求助,很急!

【问题讨论】:

    标签: java calculator


    【解决方案1】:

    要将按钮绑定到“操作”,您可以使用 setOnAction -> 只要单击按钮,该功能/事件就会激活。

    Button.setOnAction(event -> {       
           do this when button is clicked.
    });
    

    这是 javadoc,您可以在其中查找其中的大部分内容。 https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ButtonBase.html#setOnAction-javafx.event.EventHandler-

    【讨论】:

      猜你喜欢
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多