m0_56650725 2021-07-11 17:24 采纳率: 100%
浏览 25
已结题

怎么用swing完成一个简单的加法计算!

如图:
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/3

img
要求: 用基本的Swing窗口程序,实现类似求和计算 这样简单的 JFrame窗口程序,会用Action事件;

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2021-07-11 17:29
    关注

    我有写过这种swing窗体。把文体替换就好了,如果不会,我可以帮你替换。可以看一下:
    如有帮助,请点击我回答右上角【采纳】按钮。
    img

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;
    public class SummationForm extends JFrame implements ActionListener {
        private static final long serialVersionUID = -3059616600875760053L;
        JLabel labMathematics, labEnglish, labTotal;
        JTextField textMathematics, textEnglish, textTotal;
        static JButton butSummation,butReturn;
        public SummationForm() {
            labMathematics = new JLabel("第一个数");
            labEnglish = new JLabel("第二个数");
            labTotal = new JLabel("结果");
            textMathematics = new JTextField("", 10);
            textEnglish = new JTextField("", 10);
            textTotal = new JTextField("", 10);
            butSummation = new JButton("求和");
            butReturn = new JButton("退出");
            labMathematics.setBounds(30, 30, 120, 30);
            textMathematics.setBounds(180, 30, 120, 30);
            labEnglish.setBounds(30, 80, 120, 30);
            textEnglish.setBounds(180, 80, 120, 30);
            labTotal.setBounds(30, 130, 120, 30);
            textTotal.setBounds(180, 130, 120, 30);
            butSummation.setBounds(80, 180, 80, 30);
            butReturn.setBounds(200, 180, 80, 30);
            add(labMathematics);
            add(textMathematics);
            add(labEnglish);
            add(textEnglish);
            add(labTotal);
            add(textTotal);
            add(butSummation);
            add(butReturn);
            setLayout(null);
            setBounds(200, 100, 400, 300);
            // 窗口在屏幕中间显示
            setLocationRelativeTo(null);
            setTitle("求和");
            setResizable(false);
            setVisible(true);
        }
        public static void main(String[] args) {
            SummationForm summationForm = new SummationForm();
            butSummation.addActionListener(summationForm);
            butReturn.addActionListener(summationForm);
            summationForm.add(butSummation);
            summationForm.add(butReturn);
        }
        public void actionPerformed(ActionEvent e) {
            JButton jb = (JButton) e.getSource();
            if (jb == butSummation) {
                String Mathematics = textMathematics.getText();
                String English = textEnglish.getText();
                try {
                    int a = Integer.parseInt(Mathematics);
                    int b = Integer.parseInt(English);
                    int t = a + b;
                    textTotal.setText(String.valueOf(t));
                } catch (Exception e2) {
                    JOptionPane.showMessageDialog(this, "输入的格式错误!", "提示对话框", JOptionPane.DEFAULT_OPTION);
                }
            }else if(jb == butReturn){
                System.exit(0);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月14日
  • 已采纳回答 7月11日
  • 创建了问题 7月11日

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作