package image2;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo2 {
public static void main(String[]args) {
new cul();
}
}
class cul extends JFrame implements ActionListener{
JPanel jp1,jp2;
JLabel jl1,jl2;
JButton jb1,jb2,jb3,jb4,jb5;
JTextField jt1,jt2;
public cul() {
jl1=new JLabel("a:");
jl2=new JLabel("b:");
jt1=new JTextField(5);
jt2=new JTextField(5);
jp1=new JPanel();
jp2=new JPanel();
jb1=new JButton("+");
jb1.addActionListener(this);
jb2=new JButton("-");
jb2.addActionListener(this);
jb3=new JButton("*");
jb3.addActionListener(this);
jb4=new JButton("/");
jb4.addActionListener(this);
jb5=new JButton("退出");
jb5.addActionListener(this);
jp1.add(jl1);jp1.add(jt1);
jp2.add(jl2);jp2.add(jt2);
this.add(jp1);this.add(jp2);
this.add(jb1);this.add(jb2);this.add(jb3);this.add(jb4);
this.add(jb5);
this.setLayout(new FlowLayout());
this.setTitle("四则运算");
this.setSize(220,160);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb1) {
JOptionPane.showMessageDialog(null, Double.valueOf(jt1.getText())+Double.valueOf(jt2.getText()));;
}
if(e.getSource()==jb2) {
JOptionPane.showMessageDialog(null, Double.valueOf(jt1.getText())-Double.valueOf(jt2.getText()));
}
if(e.getSource()==jb3) {
JOptionPane.showMessageDialog(null, Double.valueOf(jt1.getText())*Double.valueOf(jt2.getText()));
}
if(e.getSource()==jb4) {
JOptionPane.showMessageDialog(null, Double.valueOf(jt1.getText())/Double.valueOf(jt2.getText()));
}
if(e.getSource()==jb5) {
System.exit(0);
}
}
}
Java JFrame四则运算窗口
最新推荐文章于 2021-12-23 10:38:43 发布