编程实验加法计算器。界面接收用户输入两个整数,一个“计算”按钮。当用户在界面上输入整数后,点击按钮弹出计算结果。java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Author: lan
* @Description:
* @Date: 2019-12-12 19:12
*/
class LunchFrame extends JFrame implements ActionListener {
JLabel label1;
JTextField t1;
JLabel label2;
JTextField t2;
JLabel label3;
JButton button;
public LunchFrame()
{
label1=new JLabel("请输入两个数据:");
t1=new JTextField(5);
label2=new JLabel("+");
t2 = new JTextField(5);
button=new JButton("计算");
label3=new JLabel();
add(label1);
add(t1);
add(label2);
add(t2);
add(button);
add(label3);
button.addActionListener(this);
setLayout(new FlowLayout());
setSize(300,300);
setLocation(200,200);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()== button){
int x=Integer.parseInt(t1.getText());
int y=Integer.parseInt(t2.getText());
String res=String.valueOf(x+y);
label3.setText(res);
JOptionPane.showMessageDialog(this,res,"结果",JOptionPane.INFORMATION_MESSAGE);
}
}
}
public class test04 {
public static void main(String[] args) {
new LunchFrame();
}
}
运行截图:编程