Java 79. Các thành phần và bố cục của chương trình giao diện | 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 java.awt.FlowLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class ViDu_FlowLayout extends JFrame {
    	
    	public ViDu_FlowLayout() {
    		this.setTitle("ViDu_FlowLayout");
    		this.setSize(600, 400);
    		// căn giữa cửa sổ chương trình
    		this.setLocationRelativeTo(null);
    		
    		// Set Layout
    		FlowLayout flowLayout = new FlowLayout();
    		FlowLayout flowLayout_1 = new FlowLayout(FlowLayout.RIGHT);
    		FlowLayout flowLayout_2 = new FlowLayout(FlowLayout.CENTER, 50, 50);
    		//this.setLayout(flowLayout);
    		//this.setLayout(flowLayout_1);
    		this.setLayout(flowLayout_2);
    		
    		JButton jButton_1 = new JButton("1");
    		JButton jButton_2 = new JButton("2");
    		JButton jButton_3 = new JButton("3");
    		
    		// add thành phần
    		this.add(jButton_1);
    		this.add(jButton_2);
    		this.add(jButton_3);
    		
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setVisible(true);
    	}
    	
    	public static void main(String[] args) {
    		new ViDu_FlowLayout();
    	}
    	
    }
    
         
    
         package view;
    
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class ViDu_GridLayout extends JFrame {
    	
    	public ViDu_GridLayout() {
    		this.setTitle("ViDu_GridLayout");
    		this.setSize(600, 400);
    		// căn giữa cửa sổ chương trình
    		this.setLocationRelativeTo(null);
    		
    		// Set Layout
    		GridLayout gridLayout = new GridLayout();
    		GridLayout gridLayout_2 = new GridLayout(4, 4);
    		GridLayout gridLayout_3 = new GridLayout(4, 4, 25, 25);
    		this.setLayout(gridLayout_3);
    		
    		for (int i = 0; i < 16; i++) {
    			JButton jButton = new JButton(i+"");
    			this.add(jButton);
    		}
    
    		
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setVisible(true);
    	}
    	
    	public static void main(String[] args) {
    		new ViDu_GridLayout();
    	}
    	
    }
    
         
    
         package view;
    
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class ViDu_BorderLayout extends JFrame {
    	
    	public ViDu_BorderLayout() {
    		this.setTitle("ViDu_BorderLayout");
    		this.setSize(600, 400);
    		// căn giữa cửa sổ chương trình
    		this.setLocationRelativeTo(null);
    		
    		// Set Layout
    		BorderLayout borderLayout = new BorderLayout();
    		BorderLayout borderLayout_2 = new BorderLayout(15, 15);
    		
    		this.setLayout(borderLayout_2);
    		
    		JButton jButton_1 = new JButton("1");
    		JButton jButton_2 = new JButton("2");
    		JButton jButton_3 = new JButton("3");
    		JButton jButton_4 = new JButton("4");
    		JButton jButton_5 = new JButton("5");
    		
    		// add thành phần
    		this.add(jButton_1, BorderLayout.NORTH);
    		this.add(jButton_2, BorderLayout.SOUTH);
    		this.add(jButton_3, BorderLayout.WEST);
    		this.add(jButton_4, BorderLayout.EAST);
    		this.add(jButton_5, BorderLayout.CENTER);
    		
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setVisible(true);
    	}
    	
    	public static void main(String[] args) {
    		new ViDu_BorderLayout();
    	}
    	
    }
    
         

    2 nhận xét: