java求和运算窗口5.29

import java.awt.*;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GridLayoutPosition extends JFrame{
	JLabel jl1;
	JLabel jl2;
	JButton jb1;
	JButton jb2;
	JTextField jt1;
	JTextField jt2;
	JTextField jt3;
	public GridLayoutPosition(){
		jl1 = new JLabel("加数1");
		jl2 = new JLabel("加数2");
		jb1 =new JButton("求和");
		jb2 =new JButton("清除");
		jt1 = new JTextField();
		jt2 = new JTextField();
		jt3 = new JTextField();
		Container c = getContentPane();
		setLayout(new GridLayout(3,3,2,2));
		c.add(jl1);
		c.add(jt1);
		c.add(new JLabel());
		c.add(jl2);
		c.add(jt2);
		c.add(new JLabel());
		c.add(jb1);
		jb1.addActionListener(new jbListener());
		c.add(jt3);
		c.add(jb2);
		jb2.addActionListener(new jbListener());	
		setSize(300,150);
		setTitle("求和计算");
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}
	class jbListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JButton jbInput= (JButton)e.getSource();
            if(jbInput.equals(jb1)) {
            	String a1=jt1.getText();
            	int i = Integer.parseInt(a1);
            	String a2=jt2.getText();
            	int j = Integer.parseInt(a2);
                jt3.setText(""+(i+j));
            }else if(jbInput.equals(jb2)){
            	jt1.setText("");
                jt2.setText("");
                jt3.setText("");
            }
            
        }
	}
	public static void main(String[] args) {
		new GridLayoutPosition();
	}

}

posted @ 2019-06-05 11:03  BKKITO  阅读(244)  评论(0编辑  收藏  举报