Java 如何让多个jbutton工作?
我正在尝试使用Java制作一个简单的加法计算器。我试着让我的按钮正常工作,但不起作用。这是识别按下哪些按钮以及何时计算的代码。它非常小,因为我只是尝试小部件,看看它是否有效:Java 如何让多个jbutton工作?,java,swing,actionlistener,calculator,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
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(这个);进入main(stringargs[]),但它说,“不能在静态上下文中使用它。”然后我在void main中取出static,错误消失了,但当我运行它时,它说没有main类型。然后我把静电放回原处,但是错误再次出现。如果你需要我把我的问题写得更清楚,那就问吧。请记住,我还不到15岁。您需要为按钮编写侦听器 编辑: 这可能也有帮助。
您的代码有相当多的错误。首先,我建议您查看我附带的代码,这样您就可以看到一个简单的示例,说明如何使用按钮创建表单,以及如何处理按钮上的单击事件。我演示了如何使用已定义的动作侦听器和匿名侦听器 请注意,我在主方法内创建了一个表单对象,然后在其构造函数内设置了该表单。这就是大多数问题的来源,因为您试图在主方法中完成所有这些,因此是从静态上下文中完成的 我编辑了这篇文章,在表单上添加了两个不同处理的按钮
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!");
}
}
}
他的主要形式是action listener,我认为方法something()是actionPerformed实现。除了我输入setup()时;在main(String args[])void中,出现了一个错误,说我必须将setup()更改为静态void,我更改了,但它不起作用。