如何让多个JButton工作?

时间:2014-02-17 19:05:08

标签: 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岁。

2 个答案:

答案 0 :(得分:1)

您需要为按钮编写侦听器。

http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

编辑:

这也可能有所帮助。 http://www.youtube.com/watch?v=mH1TltI61yU

答案 1 :(得分:1)

你的代码有点不对劲。首先,我建议您查看我附加的代码,这样您就可以看到一个简单的示例,说明如何使用按钮创建表单,以及如何处理按钮上的单击事件。我已经演示了如何使用已定义的动作侦听器和匿名动作侦听器。

请注意,我在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!");
    }

}

}