java有关图形界面复习--第三篇(设置一个加法计算器)

package p;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class MyWindow1 extends JFrame implements ActionListener {
	JTextField text1, text2, text3;
	JLabel lb1, lb2, lb3;
	JButton buttonEnter;
	int num1,num2,sum;
	MyWindow1() {
		Container con = getContentPane();
		con.setLayout(new FlowLayout());
		lb1 = new JLabel("请输入第一个加法数字:");
		text1 = new JTextField(8);
		lb2 = new JLabel("请输入第二加法个数字:");
		text2 = new JTextField(8);
		buttonEnter = new JButton("计算");
		lb3 = new JLabel("计算的结果为:");
		text3 = new JTextField(15);
		con.add(lb1);
		con.add(text1);
		con.add(lb2);
		con.add(text2);
		con.add(buttonEnter);
		con.add(lb3);
		con.add(text3);
		buttonEnter.addActionListener(this);
		text1.addActionListener(this);
		text2.addActionListener(this);
		setBounds(100, 100, 350, 150);
		setVisible(true);
		validate();
	}

	public void actionPerformed(ActionEvent e) {
		sum=0;
		if(e.getSource() == buttonEnter) {
			//System.out.println("1");
			num1=Integer.parseInt(text1.getText());
			//System.out.println(num1);
			num2=Integer.parseInt(text2.getText());
			sum=num1+num2;
			text3.setText(String.valueOf(sum));
		}
		}
}

public class Example7_3 {
	public static void main(String args[]) {
		MyWindow1 win = new MyWindow1();
	}
}

 java有关图形界面复习--第三篇(设置一个加法计算器)_第1张图片

 

你可能感兴趣的:(java复习)