java按钮数组添加事件_java 如何将下列按钮用按钮数组和for循环表现出来,并且添加按纽监听器!...

b0=newButton("Backspace");b0.addActionListener(this);b1=newButton("CE");b1.addActionListener(this);b2=newButton("C");b2.addActionListener(this);b3=newButton("MC");b3.addA...

b0 = new Button("Backspace");

b0.addActionListener(this);

b1 = new Button("CE");

b1.addActionListener(this);

b2 = new Button("C");

b2.addActionListener(this);

b3 = new Button("MC");

b3.addActionListener(this);

b4 = new Button("MR");

b4.addActionListener(this);

b5 = new Button("MS");

b5.addActionListener(this);

b6 = new Button("M+");

b6.addActionListener(this);

b7 = new Button("7");

b7.addActionListener(this);

b8 = new Button("8");

b8.addActionListener(this);

b9 = new Button("9");

b9.addActionListener(this);

b10 = new Button("/");

b10.addActionListener(this);

b11 = new Button("平方根");

b11.addActionListener(this);

b12 = new Button("4");

b12.addActionListener(this);

b13 = new Button("5");

b13.addActionListener(this);

b14 = new Button("6");

b14.addActionListener(this);

b15 = new Button("*");

b15.addActionListener(this);

b16 = new Button("平方");

b16.addActionListener(this);

b17 = new Button("1");

b17.addActionListener(this);

b18 = new Button("2");

b18.addActionListener(this);

b19 = new Button("3");

b19.addActionListener(this);

b20 = new Button("-");

b20.addActionListener(this);

b21 = new Button("立方");

b21.addActionListener(this);

b22 = new Button("0");

b22.addActionListener(this);

b23 = new Button("+/-");

b23.addActionListener(this);

b24 = new Button(".");

b24.addActionListener(this);

b25 = new Button("+");

b25.addActionListener(this);

b26 = new Button("=");

b26.addActionListener(this);

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是Java代码实现: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener { private JLabel label1, label2, resultLabel; private JTextField text1, text2; private JButton addButton, minusButton; public Calculator() { setTitle("简单计算器"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); label1 = new JLabel("输入第一个整数:"); label2 = new JLabel("输入第二个整数:"); resultLabel = new JLabel("运算结果将显示在这里"); text1 = new JTextField(10); text2 = new JTextField(10); addButton = new JButton("+"); addButton.addActionListener(this); minusButton = new JButton("-"); minusButton.addActionListener(this); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 2)); panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); panel.add(addButton); panel.add(minusButton); Container contentPane = getContentPane(); contentPane.add(panel, BorderLayout.CENTER); contentPane.add(resultLabel, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent event) { int num1 = Integer.parseInt(text1.getText()); int num2 = Integer.parseInt(text2.getText()); int result = 0; if (event.getSource() == addButton) { result = num1 + num2; resultLabel.setText(num1 + " + " + num2 + " = " + result); } else if (event.getSource() == minusButton) { result = num1 - num2; resultLabel.setText(num1 + " - " + num2 + " = " + result); } } public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.setVisible(true); } } ``` 这里我们使用了Java图形用户界面(GUI)工具包Swing来创建窗体、标签、文本框和按钮。在计算器构造方法中,我们定义了各个组件的属性,并将它们添加到面板中。我们使用GridLayout来排列面板中的部件,这样它们就可以被均匀地放置。 我们还实现了ActionListener接口来处理按钮的点击事件。在actionPerformed()方法中,我们首先从文本框中获取两个整数,然后根据按钮的来源执行加法或减法运算。最后,我们将运算式和结果显示在标签中。 在main()方法中,我们创建了一个计算器对象并将其设置为可见。这样,我们就完成了一个简单的计算器应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值