如图:
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/3
要求: 用基本的Swing窗口程序,实现类似求和计算 这样简单的 JFrame窗口程序,会用Action事件;
如图:
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/3
要求: 用基本的Swing窗口程序,实现类似求和计算 这样简单的 JFrame窗口程序,会用Action事件;
我有写过这种swing窗体。把文体替换就好了,如果不会,我可以帮你替换。可以看一下:
如有帮助,请点击我回答右上角【采纳】按钮。
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);
}
}
}