GUI 封装类和抽象方法(重要重要)

封装类

public class TextActionListener {
	public static void main(String[] args) {

		MyFrame myFrame = new MyFrame(300,300,true,Color.yellow);
		Button btn = new Button("按钮");	

	}

}
//父类继承 
//初始化frame封装
class MyFrame extends Frame {
	public MyFrame(){}
	public MyFrame(int x,int y,boolean t) {
		super();
		setSize(x, y);
		setVisible(t);
		
	}
	public MyFrame(int x,int y,boolean t,Color c) {
		super();
		setSize(x, y);
		setVisible(t);
		setBackground(c);
	}
	
}

将frame。setsize等功能再次封装,

直接使用构造函数就可以一个构造函数,

实现初始化多个功能,一些默认的初始化必须的功能

    

例如
没有封装需要
Frame frame = new Frame("手动布局");
frame.setVisible(true);
frame.setSize(500,500);
frame.setBackground(Color.magenta);

封装后
 MyFrame myFrame = new MyFrame(300,300,true,Color.yellow);   

抽象方法

public static void main(String[] args) {
		Frame frame = new Frame("事件监听窗口");
		Button btn = new Button("按钮");		
    
    	//关闭程序
		WindowClosing(frame);
		
		
	}

	//抽象关闭方法
	//private是本类可以使用,是static是静态可以调静态方法
	private static void WindowClosing(Frame frame) {
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

猜你喜欢

转载自blog.csdn.net/weixin_70271498/article/details/127094897
GUI