m0_56650725
2021-07-11 17:24
采纳率: 100%
浏览 17

怎么用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);
            }
        }
    }
    
    已采纳该答案
    打赏 评论
  • 有个加减乘除的。

    img

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class 简单计算器  extends JFrame implements ActionListener{
        JTextField txtNum1,txtNum2,txtNum3;
        JComboBox<String> cbOpr;
        String opr[] = {"+","-","*","/"};
        JButton btnCalc;
        public 简单计算器() {
            super("简单计算器");
            
            txtNum1 = new JTextField(6);
            cbOpr = new JComboBox<String>(opr);
            txtNum2 = new JTextField(6);
            txtNum3 = new JTextField(6);
            btnCalc = new JButton("=");
            JPanel northPane = new JPanel(new FlowLayout());
            northPane.add(txtNum1);
            northPane.add(cbOpr);
            northPane.add(txtNum2);
            northPane.add(btnCalc);
            northPane.add(txtNum3);
            txtNum3.setEnabled(false);
            add(northPane);
            btnCalc.addActionListener(this);
            this.pack();
            setVisible(true);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            
            try {
                double num1 = Double.parseDouble(txtNum1.getText());
                double num2 = Double.parseDouble(txtNum2.getText());
                
                String line = num1 + "";
                int index = cbOpr.getSelectedIndex();
                switch(index) {
                case 0:
                    txtNum3.setText((num1+num2)+"");
                    line+= "+";
                    break;
                case 1:
                    txtNum3.setText((num1-num2)+"");
                    line+= "-";
                    break;
                case 2:
                    txtNum3.setText((num1*num2)+"");
                    line+= "*";
                    break;
                case 3:
                    txtNum3.setText((num1/num2)+"");
                    line+= "/";
                    break;
                }
                line+= num2 + "=" + txtNum3.getText();
                //保存到文件
                BufferedWriter br = new BufferedWriter(new FileWriter("d:\\calc.txt",true));
                br.write(line);
                br.newLine();
                br.close();
            }catch(Exception ex){
                System.out.println("出错了"+ex.getMessage());
            }
        }
        public static void main(String[] args) {
            new 简单计算器();
        }
    }
    
    
    打赏 评论

相关推荐 更多相似问题