关于Java中GUI编程记录

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class WdFrame extends JFrame{
//WdFrame作为一个窗口类,可以将组件当作属性,再用方法调用
	
	public JTextField T1 = new JTextField(10);
	public JTextField T2 = new JTextField(10);
	public JTextField T3 = new JTextField(10);
	public JButton B1 = new JButton("求和");
	
	public WdFrame(String name){       //构造方法,给窗口添加名字
		super(name);
	}
	
public void init(){                
//调用方法,可以将组件添加、修改窗口、对按钮添加监听等操作
    	this.setSize(400,100);
		this.setLayout(new FlowLayout());
		this.add(T1);
		this.add(T2);
		this.add(T3);
		this.add(B1);
		this.setVisible(true);
		B1.addActionListener(new ActionListener(){
			public void actionPerformed( ActionEvent event ) //对按钮进行监听
			{ 
	T3.setText(String.valueOf(Integer.parseInt(T1.getText())+Integer.parseInt(T2.getText())));
			}	
			});
		}
	public static void main(String[] args) {       //主函数
		WdFrame f = new WdFrame("我的窗口");     //建立一个自己定义的窗口
		f.init();                     //对窗口进行初始化,完善窗口
	}

}

猜你喜欢

转载自blog.csdn.net/sunyadong_/article/details/78559425