Java 78. Tìm hiểu về cửa sổ chương trình JFrame | Lập trình Java



    Bản quyền thuộc về TITV.vn, 
    vui lòng không đăng tải lại nội dung từ trang này.

    Video giải thích chi tiết


     Code chi tiết 

    package view;
    
    import javax.swing.JFrame;
    
    public class ViDu {
    	public static void main(String[] args) {
    		JFrame jf = new JFrame();
    		// Gắn tên
    		jf.setTitle("Vi Du JFrame");
    		// Gán kích cỡ
    		jf.setSize(600, 400);
    		
    //		while(true) {
    //			System.out.println("Chương trình đang chạy!");
    //		}
    		
    		// Gán vị trí hiển thị
    		jf.setLocation(300, 300);
    		
    		// Thoát ra khỏi chương trình khi đóng cửa sổ JFrame
    		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		// Cho phép hiển thị
    		jf.setVisible(true);
    	}
    }
    

    package view;
    
    import javax.swing.JFrame;
    
    public class MyWindow extends JFrame{
    
    	public MyWindow() {
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    	
    	public void showIt() {
    		this.setVisible(true);
    	}
    	
    	public void showIt(String title) {
    		this.setTitle(title);
    		this.setVisible(true);
    	}
    	
    	public void showIt(String title, int width, int height) {
    		this.setTitle(title);
    		this.setSize(width, height);
    		this.setVisible(true);
    	}
    	
    	public static void main(String[] args) {
    		MyWindow m1 = new MyWindow();
    		m1.showIt();
    		
    		MyWindow m2 = new MyWindow();
    		m2.showIt("Window 2");
    		
    		MyWindow m3 = new MyWindow();
    		m3.showIt("Window 3", 600, 400);
    	}
    }
    

    2 nhận xét: