Java Swing 简单计算器实现

本教程将带您逐步实现一个简单的 Java Swing 计算器应用程序。该计算器将使用 FlowLayout 布局管理器,并通过按钮事件处理用户输入和计算结果。

1. 界面设计

首先,我们需要创建一个窗口,并在其中添加以下组件:

  • 标题栏: “计算”* 按钮: “加”、“减”、“乘”、“除”* 文本框: 两个用于输入数字,一个用于显示结果

**代码示例:**javapublic class SetCalculator extends Frame implements ActionListener { static SetCalculator frm = new SetCalculator(); static Button btn1, btn2, btn3, btn4; static TextField txt1, txt2, txt3;

public static void main(String[] args) {        frm.setTitle('计算器');        frm.setBounds(200, 200, 300, 200);        frm.setLayout(new FlowLayout());        frm.setVisible(true);

    btn1 = new Button('加');        btn2 = new Button('减');        btn3 = new Button('乘');        btn4 = new Button('除');        btn1.addActionListener(frm);        btn2.addActionListener(frm);        btn3.addActionListener(frm);        btn4.addActionListener(frm);

    txt1 = new TextField(10);        txt2 = new TextField(10);        txt3 = new TextField(10);        txt3.setEditable(false);

    frm.add(txt1);        frm.add(btn1);        frm.add(btn2);        frm.add(btn3);        frm.add(btn4);        frm.add(txt2);        frm.add(new Label('='));        frm.add(txt3);    }

public void actionPerformed(ActionEvent e) {        if (e.getSource() == btn1) {            int sum;            sum = Integer.parseInt(txt1.getText()) + Integer.parseInt(txt2.getText());            String Sum = String.valueOf(sum);            txt3.setText(Sum);        }        if (e.getSource() == btn2) {            int diff;            diff = Integer.parseInt(txt1.getText()) - Integer.parseInt(txt2.getText());            String Diff = String.valueOf(diff);            txt3.setText(Diff);        }        if (e.getSource() == btn3) {            int product;            product = Integer.parseInt(txt1.getText()) * Integer.parseInt(txt2.getText());            String Product = String.valueOf(product);            txt3.setText(Product);        }        if (e.getSource() == btn4) {            double quotient;            quotient = Double.parseDouble(txt1.getText()) / Double.parseDouble(txt2.getText());            String Quotient = String.valueOf(quotient);            txt3.setText(Quotient);        }    }}

2. 事件处理

当用户点击按钮时,我们需要获取两个文本框中的数字,进行相应的计算,并将结果显示在第三个文本框中。

**代码示例:**javapublic void actionPerformed(ActionEvent e) { if (e.getSource() == btn1) { // 加法 // ... } else if (e.getSource() == btn2) { // 减法 // ... } else if (e.getSource() == btn3) { // 乘法 // ... } else if (e.getSource() == btn4) { // 除法 // ... }}

3. 完整代码javaimport java.awt.;import java.awt.event.;

public class SetCalculator extends Frame implements ActionListener { static SetCalculator frm = new SetCalculator(); static Button btn1, btn2, btn3, btn4; static TextField txt1, txt2, txt3;

public static void main(String[] args) {        frm.setTitle('计算器');        frm.setBounds(200, 200, 300, 200);        frm.setLayout(new FlowLayout());        frm.setVisible(true);

    btn1 = new Button('加');        btn2 = new Button('减');        btn3 = new Button('乘');        btn4 = new Button('除');        btn1.addActionListener(frm);        btn2.addActionListener(frm);        btn3.addActionListener(frm);        btn4.addActionListener(frm);

    txt1 = new TextField(10);        txt2 = new TextField(10);        txt3 = new TextField(10);        txt3.setEditable(false);

    frm.add(txt1);        frm.add(btn1);        frm.add(btn2);        frm.add(btn3);        frm.add(btn4);        frm.add(txt2);        frm.add(new Label('='));        frm.add(txt3);    }

public void actionPerformed(ActionEvent e) {        if (e.getSource() == btn1) {            int sum;            sum = Integer.parseInt(txt1.getText()) + Integer.parseInt(txt2.getText());            String Sum = String.valueOf(sum);            txt3.setText(Sum);        }        if (e.getSource() == btn2) {            int diff;            diff = Integer.parseInt(txt1.getText()) - Integer.parseInt(txt2.getText());            String Diff = String.valueOf(diff);            txt3.setText(Diff);        }        if (e.getSource() == btn3) {            int product;            product = Integer.parseInt(txt1.getText()) * Integer.parseInt(txt2.getText());            String Product = String.valueOf(product);            txt3.setText(Product);        }        if (e.getSource() == btn4) {            double quotient;            quotient = Double.parseDouble(txt1.getText()) / Double.parseDouble(txt2.getText());            String Quotient = String.valueOf(quotient);            txt3.setText(Quotient);        }
Java Swing 计算器实现:使用 FlowLayout 布局和按钮事件

原文地址: https://www.cveoy.top/t/topic/oB20 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录