java - 如何让多个 JButton 工作?

标签 java swing actionlistener calculator

我正在尝试使用 Java 制作一个简单的加法计算器。我尝试让按钮正常工作,但不行。这是识别按下哪些按钮以及何时进行计算的代码。它很小,因为我只是尝试小部分来看看它是否有效:

static void something(ActionEvent e) {
    int num1 = 0;
    int num2 = 0;
    int answer = 0;
    //equals, plus, and one are all the names of JButtons
    while(e.getSource()!=equals&&e.getSource()!=plus) {
        if(e.getSource==one) {
            //The math is making it so as you type in more numbers, more numbers appear
            num1 = num1*10+1;
            //sayNum means say a number
            sayNum(num1);
        }
        if(e.getSource()==2) {
            num1 = num1*10+2;
            say(num1);
        }
    }
}

它似乎永远不会进入我的主要空白。这是我的整个代码,但我去掉了上面的一小段代码:

package Calculator;
import javax.swing.*;
import static java.lang.System.out;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileNotFoundException;
public abstract class CalculatorClass implements ActionListener {

    static JButton one = new JButton("1");
    static JButton two = new JButton("2");
    static JButton three = new JButton("3");
    static JButton four = new JButton("4");
    static JButton five = new JButton("5");
    static JButton six = new JButton("6");
    static JButton seven = new JButton("7");
    static JButton eight = new JButton("8");
    static JButton nine = new JButton("9");
    static JButton zero = new JButton("0");
    static JButton plus = new JButton("+");
    static JButton equals = new JButton("=");
    static JFrame frame = new JFrame();
    static JTextArea field = new JTextArea();
    public static void main(String args[]) {
        equals.setSize(225, 75);
        plus.setSize(75, 225);

        field.setSize(250, 75);
        seven.setBounds(0, 75, 50, 50);
        eight.setBounds(75, 75, 50, 50);
        nine.setBounds(150, 75, 50, 50);
        four.setBounds(0, 150, 50, 50);
        five.setBounds(75, 150, 50, 50);
        six.setBounds(150, 150, 50, 50);
        one.setBounds(0, 225, 50, 50);
        two.setBounds(75, 225, 50, 50);
        three.setBounds(150, 225, 50, 50);
        zero.setBounds(0, 300, 50, 50);
        plus.setBounds(225, 75, 75, 225);
        equals.setBounds(75, 300, 225, 75);
        equals.setVisible(true);

        buttonSetup(one);
        buttonSetup(two);
        buttonSetup(three);
        buttonSetup(four);
        buttonSetup(five);
        buttonSetup(six);
        buttonSetup(seven);
        buttonSetup(eight);
        buttonSetup(nine);
        buttonSetup(zero);

        frame.setSize(306, 403);
        frame.setResizable(false);
        frame.setAlwaysOnTop(true);
        frame.setTitle("Calculator");
        frame.add(one);
        frame.add(two);
        frame.add(three);
        frame.add(four);
        frame.add(five);
        frame.add(six);
        frame.add(seven);
        frame.add(eight);
        frame.add(nine);
        frame.add(zero);
        frame.add(plus);
        frame.add(equals);
        frame.add(field);

        one.setVisible(true);
        two.setVisible(true);
        field.setVisible(true);
        frame.setVisible(true);
    say("Ready!");
    sayNum(56);
}
public void setup() {
    one.addActionListener(this);
    two.addActionListener(this);
    three.addActionListener(this);
    four.addActionListener(this);
    five.addActionListener(this);
    six.addActionListener(this);
    seven.addActionListener(this);
    eight.addActionListener(this);
    nine.addActionListener(this);
    zero.addActionListener(this);
    equals.addActionListener(this);
    plus.addActionListener(this);
    say("Setup is done.");
}
static void say(String s) {
    String msg = s;
    field.append(msg + "\n");
}
static void sayNum(int i) {
    int msg = i;
    field.append(""+ msg + "\n");
}
static void buttonSetup(JButton b) {
    b.setSize(75, 75);
    b.setVisible(true);
}
}

我会放 .addActionListener(this);进入 main(String args[]) 但它说,“不能在静态上下文中使用它。”然后我取出 void main 中的静态,错误消失了,但是当我运行它时,它说没有主类型。然后我把静电放回去,但错误又出现了。如果您需要我以更清晰的形式提出我的问题,请尽管提出。请记住,我还不到 15 岁。

最佳答案

你的代码有一点错误。首先,我建议您查看我附加的代码,以便您可以看到一个简单的示例,说明如何制作带有按钮的表单,以及如何处理按钮上的单击事件。我已经演示了如何使用定义的操作监听器和匿名操作监听器来完成此操作。

请注意,我在 main 方法中创建了一个表单对象,然后在其构造函数中设置了该表单。这是大多数问题的根源,因为您尝试在主方法内部完成所有这些工作,因此是在静态上下文中完成的。

我编辑了这篇文章,添加了两个在表单上以不同方式处理的按钮。

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class MainF extends JFrame {

public static void main(String[] args) {
    MainF f = new MainF();
    f.setVisible(true);

}


public MainF() {
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setBounds(100, 100, 600, 300);
    this.setLayout(new FlowLayout());

    JButton someButton = new JButton("Some Text");
    JButton someOtherButton = new JButton("Some Other Text");
    //Add an anonymous one
    someButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "I was clicked and handled anonymously!");
        } });
    //add a defined one to our other button
    someOtherButton.addActionListener(new buttonListener());
    this.add(someButton);
    this.add(someOtherButton);
}


private class buttonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, "I was clicked and handled by buttonListener!");
    }

}

}

关于java - 如何让多个 JButton 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21837249/

相关文章:

java - 如何正确转换为日期对象这个 "strange"单元格值检索使用 Apache POI 解析 Excel?

java - 为什么抽象类中需要构造函数和私有(private)成员?

java - 在 Java Swing 中,如何管理允许选择多个面板的面板列表?

java - 使用 OO 观察者模式而不更新从中发起更改的对象

java - JTable + JPopupMenu + ActionListener = 噩梦

java - 如何赋予 JButton 不同的功能?

java - 蓝牙打印在不同设备上从 Socket 抛出 IOException

java - CountDownLatch 导致 JFrame(JButtons 等)中的内容消失,并且仅在鼠标悬停时才返回

java - 从 ListSelectionListeners 和 ActionListeners 传递变量

JavaFX HTMLEditor - 焦点 webView (TextArea)