gpt4 book ai didi

java - 如何给多个按钮提供不同的命令

转载 作者:行者123 更新时间:2023-11-30 03:09:00 26 4
gpt4 key购买 nike

我一直在尝试用Javax swing制作一个计算器。我知道如何让一个按钮有一个命令,但我不知道如何在Java中为多个按钮提供更多命令?我想为按钮提供 a0-clear 操作。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Calculator extends JFrame {

private JButton a0;
private JButton a1;
private JButton a2;
private JButton a3;
private JButton a4;
private JButton a5;
private JButton a6;
private JButton a7;
private JButton a8;
private JButton a9;
private JButton clear;
private JButton plusminus;
private JButton plus;
private JButton minus;
private JButton multiply;
private JButton divide;
private JButton equal;
private JButton decimal;

private JLabel resultLabel;

public Calculator() {
setLayout(new FlowLayout());

a0 = new JButton("0");
add(a0);

a1 = new JButton("1");
add(a1);

a2 = new JButton("2");
add(a2);

a3 = new JButton("3");
add(a3);

a4 = new JButton("4");
add(a4);

a5 = new JButton("5");
add(a5);

a6 = new JButton("6");
add(a6);

a7 = new JButton("7");
add(a7);

a8 = new JButton("8");
add(a8);

a9 = new JButton("9");
add(a9);

decimal = new JButton(".");
add(decimal);

clear = new JButton("C");
add(clear);

plusminus = new JButton("+/-");
add(plusminus);

plus = new JButton("+");
add(plus);

minus = new JButton("-");
add(minus);

multiply = new JButton("X");
add(multiply);

divide = new JButton("/");
add(divide);

equal = new JButton("=");
add(equal);

resultLabel = new JLabel("");
add(resultLabel);

}

public class event implements ActionListener {

public void actionPerformed(ActionEvent event) {

a0.addActionListener(this);

a1.addActionListener(this);

a2.addActionListener(this);

a3.addActionListener(this);

a4.addActionListener(this);

a5.addActionListener(this);

a6.addActionListener(this);

a7.addActionListener(this);

a8.addActionListener(this);

a9.addActionListener(this);

clear.addActionListener(this);

decimal.addActionListener(this);

plusminus.addActionListener(this);

plus.addActionListener(this);

minus.addActionListener(this);

multiply.addActionListener(this);

divide.addActionListener(this);

equal.addActionListener(this);

if (event.getSource() == a1) {
resultLabel.setText("0");
}

}
}

public static void main(String[] args) {
Calculator gui = new Calculator();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(300, 800);
gui.setVisible(true);
gui.setTitle("Caclculator");

}
}

最佳答案

我将向您展示 1 个按钮,对所有其他按钮执行相同的操作,

public class Calculator extends JFrame  implements ActionListener {

JButton a0 = new JButton("0");
a0.addActionListener(this);
add(a0);

public void actionPerformed(ActionEvent event) {

String command = e.getActionCommand();

switch(command){

case "0":
//do whatever want while button '0' press
break;
case "1":
//do whatever want while button '1' press
break;
case "2":
//do whatever want while button '2' press
break;


}

}
}

关于java - 如何给多个按钮提供不同的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34029723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com