标题:在jsp计算毕业设计中如何使用swing编程计算两个商品总量
价格:0元
类型:常见毕业问题
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;public SummationForm() {labMathematics = new JLabel("商品数量1");labEnglish = new JLabel("商品数量2");labTotal = new JLabel("总量");textMathematics = new JTextField("", 10);textEnglish = new JTextField("", 10);textTotal = new JTextField("", 10);butSummation = 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(160, 180, 120, 30);add(labMathematics);add(textMathematics);add(labEnglish);add(textEnglish);add(labTotal);add(textTotal);add(butSummation);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);summationForm.add(butSummation);}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);}}}}
效果图如下:

————————————————
版权声明:本文为CSDN专家-sinjack的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/technologist_36/article/details/118353517?spm=1001.2014.3001.5502
版权所有© 帮我毕业网 并保留所有权利